[fpc-pascal] getmem
Claudio Lo Gaffo
clogaffo at gmail.com
Tue May 24 23:28:41 CEST 2011
El 24/05/2011 03:57 p.m., Luis Fernando Del Aguila Mejía escribió:
>>> 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 - fpc-pascal at lists.freepascal.org
> 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
More information about the fpc-pascal
mailing list