Board index » delphi » FPC: ^longint type, memory question

FPC: ^longint type, memory question

suppose a variable V is declared as ^longint, and I perform a getmem twice
on V, what happens to the memory that is reserved for V with the first
getmem after the second getmem, is it freed or will you, when performing a
lot of getmems, eventually get a stack overflow?

greetings
wal

 

Re:FPC: ^longint type, memory question


Quote
Walied Othman wrote:

> suppose a variable V is declared as ^longint, and I perform a getmem twice
> on V, what happens to the memory that is reserved for V with the first
> getmem after the second getmem, is it freed or will you, when

The first on is not freed. Because the specified pointer is unreliable.
But you can use reallocmem() to reallocate a block. When the pointer
passed to reallocmem is nil the the block is normally allocated.

This kind of memory leaks can easily be check using the heaptrc unit
which is automaticly included when you compile with -gh

Quote
> performing a
> lot of getmems, eventually get a stack overflow?

Not a stackoverflow, maybe an out of memory error. But since FPC has a
growing heap you it's dependent on the amount of memory in your machine.

Peter

Re:FPC: ^longint type, memory question


In article
<Pine.LNX.4.21.0001272148400.280-100...@machine.iverlek.kotnet.org>,

Quote
Walied Othman <Walied.Oth...@Student.KULeuven.ac.be> wrote:
> suppose a variable V is declared as ^longint, and I perform a getmem twice
> on V, what happens to the memory that is reserved for V with the first
> getmem after the second getmem, is it freed or will you, when performing a
> lot of getmems, eventually get a stack overflow?

You'll get a memory leak (which won't cause a stack overflow, but it
will cause an out-of-memory error or an enormous swap file after a
while), just as you would get one when doing twice a getmem for another
pointer variable without freemem'ing first.

If you get fpc versino 0.99.14 (just released), you can use reallocmem
instead, that will save you the hassle of freemem/getmem every time.

Note that in FPC, you don't have to specify the size when doing a
freemem, so

var l: ^longint;

begin
  getmem(l,10000);
  reallocmem(l,15000);
  freemem(l);
end.

is completely legal and will free the correct size.

To everyone: the reason version 0.99.14 of FPC hasn't been widely
annoucned, is because we plan on releasing version 1.0 in a couple of
weeks, so you can save yourself the hassle of downloading it two times.

Jonas

Other Threads