04.07.2020, 12:13
This is the case for all strings (unless if you're using the dynamic memory plugin).
Strings (arrays) are basically just a bunch of variables. PAWN reads these arrays as strings. All string functions (reading strings, strings such as strcmp(), strlen()) use the null terminator (aka EOS aka \0) to find the end of the string.
So if you have the text "hello", the array needs the size of the string (in this case 5) + 1 cell (one cell is one index / memory space of the array) for the null terminator. This is what "hello" would look like:
If you have a larger array where not all space is used, the null terminator is placed after the last character so that during runtime the end of the string is found. All string functions use the null terminator. Example:
That's why strlen() in this case would return 5 and not 10, because it stops at the null terminator. And as you can see, those unused characters are a waste of space. One cell is 32 bits and thus 4 bytes (1 byte = 8 bits). In above example, 5 cells are wasted and thus 5*4 = 20 bytes.
You don't have to define the array size of the array has a default value/input. For example:
would automatically assign 13 cells.
And that's why you include one more cell than the size of the array. It is not y_va specific.
Strings (arrays) are basically just a bunch of variables. PAWN reads these arrays as strings. All string functions (reading strings, strings such as strcmp(), strlen()) use the null terminator (aka EOS aka \0) to find the end of the string.
So if you have the text "hello", the array needs the size of the string (in this case 5) + 1 cell (one cell is one index / memory space of the array) for the null terminator. This is what "hello" would look like:
pawn Code:
//Using: new myArray[] = "Hello";
//Would basically be this:
new myArray[6];
myArray = {
'H', 'e', 'l', 'l', 'o', EOS //EOS == '\0'
};
pawn Code:
new myArray[10] = "Hello";
//so, myArray is:
//'H', 'e', 'l', 'l', 'o', EOS, '', '', '', '', EOS
You don't have to define the array size of the array has a default value/input. For example:
pawn Code:
new myArray[] = "Hello world!";
And that's why you include one more cell than the size of the array. It is not y_va specific.