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

Re: Memory size and allocation


> I've got two questions:
> 1) Is there a portable way to know the size of a malloc()ed memory block,
> knowing just its address?

There is no function that tells the size of a malloced block. But you
can use a simple trick: write a simple wrapper around the allocation
routines. For example a "void *myalloc(size_t n)" that allocates a
block which is a bit larger and saves the block size into the block
itself. However you cannot use something like:

|  block data                  | size |

because you wouldn't know where to look for size. Instead you must

myalloc(n) allocates n+sizeof(size) bytes of memory.

| size |  block data                  |

"myalloc" does not return the pointer to the beginning of the
allocated block, but instead a pointer to the beginning of the data
within the block.

I used this in one of my programs for debugging purposes (to count
allocated bytes and blocks and trace missing deallocations), here's
my piece of code:

void *myalloc(size_t n) {
  size_t *p;

  if(!p) {
    printf("myalloc failed\n");


(  m_bytes_used+=n;

  return p+1; 	/* points to the beginning of the data */

void myfree(void *p) {
  size_t *q=p;

  if(!p) {
    printf("error: tried to free NULL pointer\n");
  q--;			/* get pointer to allocated block */

(  m_bytes_used-=*q;


And now I will add the routine for block size determination:

void mysize(void *p) {
  size_t *q=p;

  if(!p) {
    printf("error: queried a NULL pointer\n");

  q--;			/* get pointer to allocated block */

  return *q;

However I don't think this is necessary. You can always keep memory
block sizes in your program's data structures, when needed.

> 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?

When a program(process) terminates, the operating system
automatically frees all memory that was allocated by the program. You
don't have to care about anything.

(You may still want to free any unused memory as soon as possible.
Then you should free the blocks using the program's data structures.
As a last resort, you could use a wrapper that stores a list of all
allocated blocks and hence is able to free them.
Note that I don't use any such things in my programs)

One final remark. Always think twice before using dynamic, linked
structures thoughtlessly. They are:
1) slow
2) complicated
  and hence
    a) needlessly take too much work to implement
    b) you can easily make a mistake ->buggy programs

In many cases data structures (link-lists, trees, heaps) can be
effectively implemented in arrays. That is easier and faster too.

> Thank you,
> Francesco Orsenigo, Xarvh Project

Jiri Svoboda

Seznam Prace - Hledate praci nebo menite zamestnani? http://prace.seznam.cz