PDA

View Full Version : Arrays


sent161
29/08/2011, 10:09 PM
Why when using a format not valid limit of an array?
Sorry for my English, unnecessarily I live in Russia and use ******;



new SeNT[16];//Global
public OnGameModeInit()
{
format(SeNT,512,"qwertyqwertyqwertyzxcvbbnmqwertyqwertyqwertyzxcvbb nm");
printf("%d",strlen(SeNT));
print(SeNT);
}


After checking the code, We can see in the console that the value in the format of the size of the array.

I await your replys with respect, Dmitry

Backwardsman97
29/08/2011, 10:51 PM
What does the console print? 16?

PowerPC603
29/08/2011, 11:19 PM
Lol, just checked this.
It prints:


52
qwertyqwertyqwertyzxcvbbnmqwertyqwertyqwertyzxcvbb nm
Run time error 6: "Invalid instruction"

sent161
29/08/2011, 11:20 PM
Well .. I do not know, I have everything well. no problems found
http://cs10963.vkontakte.ru/u42251762/95035174/x_8bbdb08a.jpg

Backwardsman97
30/08/2011, 12:12 AM
Yeah I don't really understand your problem here.

Pghpunkid
30/08/2011, 12:18 AM
I think your getting an error because there is no main(). Open a new file copy the code for main, paste it in and check again.

Why are you trying to cram 512 characters into a 16 character array?

sent161
30/08/2011, 01:08 PM
I have no problem, just wondering why it was the format size of 512 cells with a variable works fine in 16 cells

JaTochNietDan
30/08/2011, 01:12 PM
It's because you have specified 512 cells in the format function, so it continues writing to memory even though you don't have any memory allocated, which is a buffer overflow.

http://en.wikipedia.org/wiki/Buffer_overflow

sent161
30/08/2011, 07:47 PM
Sorry, I disagree with you. Because, in the Pascal format has no arguments to the variable in the. ... Below is an example



var
text : string;
begin
// Just 1 data item
ShowMessage(Format('%s', ['Hello']));

// A mix of literal text and a data item
ShowMessage(Format('String = %s', ['Hello']));
ShowMessage('');

// Examples of each of the data types
ShowMessage(Format('Decimal = %d', [-123]));
ShowMessage(Format('Exponent = %e', [12345.678]));
ShowMessage(Format('Fixed = %f', [12345.678]));
ShowMessage(Format('General = %g', [12345.678]));
ShowMessage(Format('Number = %n', [12345.678]));
ShowMessage(Format('Money = %m', [12345.678]));
ShowMessage(Format('Pointer = %p', [addr(text)]));
ShowMessage(Format('String = %s', ['Hello']));
ShowMessage(Format('Unsigned decimal = %u', [123]));
ShowMessage(Format('Hexadecimal = %x', [140]));
end;



If you think about. then we get a variable is needed to define the format for future use ...

Or am I wrong?

Pghpunkid
31/08/2011, 09:57 AM
Pascal is not pawn.. so trust JaTochNietDan with what hes saying.

EDIT: After further reviewing your Pascal code, it appears as if Pascal is not looking for a determined length of the string. So Pascal i presume can handle that. Its the same as apstring in C++. But for arrays, in Pawn, you are exceeding the memory of the array, as JaTochNietDan said.