Sign in Join

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

Mailing lists / fpc-pascal / 2014-06

replacement of MARK and RELEASE function in free pascal

11 messages · 6 people ·

Reply by email Subscribe to this list Your subscription

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

Marco van de Voort
to mokashe.ram

In our previous episode, mokashe.ram said:

Show quoted text 10 lines
Can any one help me on raplacement of MARK and RELEASE function in free pascal... var markexam: pointer; // untyped pointer begin ... * mark(markexam); ..... Release(markexam);* end;

This eighties style of memory management has no 1:1 substitutions. Reengineer your memory management on top of getmem and freemem.

Дмитрий Померанцев
to mokashe.ram: Can any one help me on raplacement of MARK and RELEASE function in free pascal... var…

Forget it. Mark and Release are very DOS, real mode functions. Use New/Dispose or GetMem/FreeMem instead and make proper control of dynamically allocated blocks.

Dmitriy Pomerantsev.

19.06.2014, 14:50, "mokashe.ram" :

Show quoted text 22 lines
Hi,   Can any one help me on raplacement of MARK and RELEASE function in free pascal...    var           markexam: pointer; // untyped pointer   begin              ...            * mark(markexam);             .....             Release(markexam);* end; Thanks, Sud -- View this message in context: http://free-pascal-general.1045716.n5.nabble.com/replacement-of-MARK-and-RELEASE-function-in-free-pascal-tp5719624.html Sent from the Free Pascal - General mailing list . _______________________________________________ fpc-pascal maillist  -   http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Marco van de Voort
to mokashe.ram

In our previous episode, mokashe.ram said:

Show quoted text 5 lines
Ok thanks, but using untyped pointer how to mention size in GETMEM()? * GETMEM(markexam,'' ....");*

You misunderstood. You don't substitute getmem/freemem (or new/dispose) of just the markexam pointer, but for ALL pointers allocated between mark and release statements.

So basically you must delete the mark, release and markexam declaration lines, and make sure that every allocated pointer is freed (-gh can help with that).

Mark/release save/restore the stack top which allowed a release to release all dynamically allocated memory since the last mark on that pointer. This option (even the principle) is not available anymore in modern memory managers.

Mark and release were already declared legacy in Turbo Pascal versions from the early nineties.

Karoly Balogh (Charlie/SGR
to mokashe.ram: but using untyped pointer how to mention size in GETMEM()? * GETMEM(markexam,''…

Hi,

mokashe.ram wrote 5 lines
Ok thanks, but using untyped pointer how to mention size in GETMEM()? * GETMEM(markexam,'' ....");*

It's detailed in the documentation, it's detailed there along with the rest of the memory management functions, please refer to that:

http://www.freepascal.org/docs-html/rtl/system/getmem.html

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));

This changed little since the times of Turbo Pascal, works the same way.

Charlie

Sven Barth
to Karoly Balogh (Charlie/SGR: It's detailed in the documentation, it's detailed there along with the rest of the memory…
Karoly Balogh 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().

Regards,
Sven

Unknown author
to Sven Barth
Sven Barth wrote 10 lines
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?

Karoly Balogh (Charlie/SGR
to Unknown author

Hi,

Show quoted text 7 lines
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?

Not in Free FPC New/Dispose are inline functions
generated by the compiler (because they need to do other things aside the memory allocation), but the memory handling part internally just wraps to GetMem and FreeMem.

(See pinline.pas, new_dispose_statement() in the compiler sources for all the dirty details.)

However, while mixing New/Dispose with GetMem/FreeMem in the same code shouldn't have any ill. effects, you still should *not* mix New/Dispose on the *same* pointer I think. (Eg. allocate with New, but free with FreeMem, and so on.)

Charlie

Sven Barth
to Unknown author: I seem to remember being warned in the old days of Turbo Pascal not mix use of…
Show quoted text 19 lines
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