On 24/5/11 4:22, Luis Fernando Del Aguila Mejía wrote:
I know that you can use dynamic arrays (array of). Is better.
But this does not understand and confuse me.
Why to create an array of 3 elements (ANSIString) with GetMem, I have to
put a size of 15 bytes (5*3) and not 12 bytes (4*3).?
The program works with 15 bytes, but do not understand why not work with
12 bytes.
Thanks.
{$codepage utf8}
Var LCad:^ansiString;
Begin
getmem(LCad,5*3); //ansistring is a pointer 4 bytes
LCad[0]:='01234';
LCad[1]:='56789';
LCad[2]:='11111';
Writeln(LCad[2]);
freemem(LCad)
End.
I'm surprised this compiles at all.
You're mixing up manual allocation of memory on the heap for variables
such as arrays with compiler allocation of heap memory for ansistrings
(which are reference counted and deallocated automatically).
You're safer using an array of shortstrings whose size is known at
compile time:
type s5 = string[5];
LCad = array[0..2] of s5;
PLCad = ^LCad;
Var pArr: PLCad;
begin
getmem(pArr, sizeof(LCad));
pArr^[0]:='01234';
pArr^[1]:='56789';
pArr^[2]:='11111';
Writeln(pArr^[2],' ',pArr^[1],' ',pArr^[0]);
writeln('Sizeof(LCad) has value ', sizeof(LCad));
freemem(pArr);
end.
yours
Howard
_______________________________________________
fpc-pascal maillist - fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal