Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2011-05

Re: getmem

3 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Luis Fernando Del Aguila Mejía
Show quoted text 5 lines
>I'm surprised this , Does my program does not compile on your computer ? >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).

Yes something like that. I'm trying to emulate something like this:

VarLCad = array[0..2] of ansistring;
Begin
   LCad[0]:='01234';
   LCad[1]:='56789';
   LCad[2]:='11111';
   Writeln(LCad[2]);
End.

But using GetMem(LCad,4*3) for the array. (4 bytes for the ansistring pointer).
This can be done with: TList. But I write these simple programs, to know how compiler works.

Reply by email

Jonas Maebe
to Luis Fernando Del Aguila Mejía
Luis Fernando Del Aguila Mejía wrote 12 lines
Yes something like that. I'm trying to emulate something like this: VarLCad = array[0..2] of ansistring; Begin LCad[0]:='01234'; LCad[1]:='56789'; LCad[2]:='11111'; Writeln(LCad[2]); End. But using GetMem(LCad,4*3) for the array. (4 bytes for the ansistring pointer). This can be done with: TList. But I write these simple programs, to know how compiler works.

Using automated types in combination with manual memory management is hard. Figuring out "how the compiler works" by writing simple programs is extremely unlikely to work out in this context, because the reference counting of the automated types causes lots of extra code to be generated behind the scenes. Additionally, the compiler makes lots of assumptions which you are breaking if you use getmem and don't add the necessary initialization/finalization code. I don't think there is currently any documentation about how to combine the two.

Jonas

PS: using a TList is just as unsafe as using getmem. Use at least a tstringlist, or a dynamic array as others have suggested (and dynamic arrays themselves are also automated types, so make sure you don't put those either into a tlist/getmem area)

Reply by email

Claudio Lo Gaffo
to Luis Fernando Del Aguila Mejía: Yes something like that. I'm trying to emulate something like this: VarLCad =…
Luis Fernando Del Aguila Mejía wrote 35 lines
>> I'm surprised this compiles at all. Ok, Does my program does not compile on your computer ? >> 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). Yes something like that. I'm trying to emulate something like this: VarLCad = array[0..2] of ansistring; Begin LCad[0]:='01234'; LCad[1]:='56789'; LCad[2]:='11111'; Writeln(LCad[2]); End. But using GetMem(LCad,4*3) for the array. (4 bytes for the ansistring pointer). This can be done with: TList. But I write these simple programs, to know how compiler works. _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

You don't explain what is exactly the error you're getting but getmem don't initialize the memory allocated, and when you assign a pointer to ansistring the compiler decrements the reference counter of the pointer's current value, if isn't nil. Since you have garbage in the pointer, you'll get an access violation. If you don't get it, somewhere the memory becomes corrupted. Sorry by my english, I expect you understand me.

Claudio

Reply by email