Thanks Mattias and Anton.
Ie: When you assign a constant string to a PChar then assigns the address of the string constant.
I believed that be created a PChar in the heap.

Mailing lists / fpc-pascal / 2011-05
9 messages · 5 people · –
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
Thanks Mattias and Anton.
Ie: When you assign a constant string to a PChar then assigns the address of the string constant.
I believed that be created a PChar in the heap.
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 '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
I know that you can use dynamic arrays (array of). Is better. But this does not…
Var LCad:^ansiString;
AnsiString is already a pointer. LCad is now a pointer to a pointer. That should read
Var LCad: AnsiString;getmem(LCad,5*3); //ansistring is a pointer 4 bytes
The memory allocation and freeing is done by the compiler automatically. Just assign a string or set a length with SetLength. There is no need to use getmen or new for AnsiStrings.
LCad[0]:='01234';
What should LCad[0] mean? LCad is not an array. You declared it as a pointer. If you declared LCad as AnsiString then you can write LCad[1] (which is the first character of the string) because Free Pascal does an automatic derefferencing of the AnsiString pointer but you cannot write LCad[0] because there is no character before the first one. Indexes of AnsiStrings are not zero-based.
freemem(LCad)
Again: No need to free memory for AnsiStrings. It's done automatically.
I know that you can use dynamic arrays (array of). Is better. But this does not…
Question out of curiosity:
Is there a reason why you allocate an array "by hand"? Why don't you try this:
var
LCad: array of AnsiString;
begin
SetLength(LCad, 3);
LCad[0] := '01234';
LCad[1] := '56789';
LCad[2] := '11111';
Writeln(LCad[2]);
SetLength(LCad, 0);
end.Regards,
Sven
Question out of curiosity: Is there a reason why you allocate an array "by hand"?
Yes, getmem return NIL, when no more memory is available. SetLength, no return NIL, It is work with Exceptions.
My intention was whether it could be done without using dynamic arrays and Exceptions.
But two more questions:
1) At this program:
Var A:Ansistring;
Begin
A:='Hi';
End.The literal String is at Data area.
Is the A variable points to Data Segment. ?
2) My computer is little endian. (intel)
When I do this:
a: = $ ABFE5689 / / Little endian $ 8956FEAB, it is ok Writeln (Hi (a)) / / Show $ ABFE, it is ok
Writeln (Lo (a)) / / Show $ 5689, it is ok
In a Big Endian computer. What shows Hi and Lo?
thanks.
They show same. Language constructs and RTL routines are Endian-safe
El 26/05/2011 06:15 a.m., Felipe Monteiro de Carvalho escribió:
thanks
On Wed, May 25, 2011 at 7:58 PM, Luis Fernando Del Aguila Mejía They show same. Language…
Thanks.