[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Memory size and allocation



Francesco Orsenigo <xarvh@lombardiacom.it> writes:

> 1) Is there a portable way to know the size of a malloc()ed memory block, 
> knowing just its address?

No. The only way would be to write a wrapper around it and save the
size in some struct.
 
> 2) When malloc() fails, my program frees memory up to the required size, in 
> the hope that the freed blocks were contiguos.
> The eldest memory blocks (in order of usage) are freed first.
> Is this a good way to handle data dynamically?
> How can I improve it?

IIRC you can't really handle the situation where malloc() fails in
systems which use memory over-commitment (such as linux), since the
process just gets killed or worse. So you should instead try to never
ever reach this point. Just free any memory that you no longer need
and you should be fine.

-- 
| Ingo Ruhnke <grumbel@gmx.de> | Advent:            http://advent.sf.net/ |
|==============================| Pingus:          http://pingus.seul.org/ |
| JabberID: grumbel@jabber.org | Feuerkraft:    http://feuerkraft.sf.net/ |
| ICQ: 59461927                | Home:   http://pingus.seul.org/~grumbel/ |