On 19.06.2014 16:37, joha...@nacs.net wrote:
On Thu, Jun 19, 2014 at 03:28:42PM +0200, Sven Barth wrote:
On 19.06.2014 13:29, Karoly Balogh (Charlie/SGR) wrote:
So either you use mypointer:=GetMem(size); or GetMem(mypointer, size);
both are valid. If you need to allocate heap space for a record,
use New/Dispose, or GetMem(myptr, sizeof(Tmyrecord));
I wouldn't use GetMem() in case of record pointers just in case a
managed type happens to be in there. Unlike GetMem() New() does
initialize those fields correctly. Alternatively one should use
Initialize(myptr^) after the call to GetMem().
I seem to remember being warned in the old days of Turbo Pascal not mix
use of getmem()/freemem() and new()/dispose() within a program, due to
differences in the memory allocation strategies. Are there still any
compatibility or efficiency/performance issues with alternating between
these two families, or can the most simple or appropriate call be used
interchangeably? In other words, now that objects are so much more
prevalent in Pascal code, should getmem() and freemem() be avoided?
I think even back then it was meant that you shouldn't freemem() memory
allocated with new() and vise versa.
Nevertheless in FPC you are free to use whatever you deem suitable (and
afterall there are situations when you *can't* use a new(), namely on
untyped pointers) the only thing you should keep in mind is that you
should dispose() memory allocated by new() and freemem() memory
allocated getmem(), but no mixing on the same pointer. It *might* work
(and indeed if the type pointed to doesn't contain managed types it will
in fact be the same), but we do not guarantee anywhere that it will stay
that way.
Regards,
Sven
_______________________________________________
fpc-pascal maillist - fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal