tags:

views:

169

answers:

5
void func( int *p)
{
  // Add code to print MEMORY SIZE which is pointed by pointer P.
}
int main()
{
  int *p = (int *) malloc (10);
  func(p);
}

How can we find MEMORY SIZE from memory pointer P in func() ?

+7  A: 

There is no legal way to do this in C (or even C++ I believe). Yes, somehow free knows how much was malloced but it does so in a way that is not visible or accessible to the programmer. To you, the programmer, it might as well have done it by magic!

If you do decide to try and decode what malloc and free does then it will lead you down the road to proprietary compiler implementations. Be warned that even on the same OS, different compilers (or even different versions of the same compiler, or even the same compiler but using a third party malloc implementation (yes such things exist)) are allowed to do it differently.

slebetman
And even the same compiler but using different compilation flags! Good answer, but I'd also suggest wrapping `malloc()` (i.e. writing and using `my_*alloc()` and `my_free()`.)
aib
+1  A: 

If you program for Microsoft Windows, you can use the Windows API Heap* functions instead of the functions provided by your programming language (C in your case). You allocate memory with HeapAlloc, reallocate with HeapReAlloc, free memory with HeapFree, and, finally, obtain the size of a previously allocated block with the HeapSize function.

Another option, of course, is to write wrapper functions for malloc and friends, that store an index of allocated blocks and their sizes. This way you can work with your own functions for allocating, reallocating, freeing, and measuring memory blocks. Writing such wrapper functions should be trivial (although I do not know C, so I cannot do it for you...).

Andreas Rejbrand
I expect that tis would be much slower per allocation than using the heap of the C run time library, and wasteful for small allocations.
ChrisW
+3  A: 

When developing applications, to know the memory size allocated to a pointer, we actually pay attention at the moment we allocate memory for it, which in your case is:

int *p = (int *) malloc(10);

and then we store this information somewhere if we need to use it in the future. Something like this:

void func(int *p, size_t size)
{
  printf("Memory address 0x%x has %d bytes allocated for it!\n", p, size);
}

int main()
{
  int my_bytes = 10;
  int *p = malloc(my_bytes);
  func(p, my_bytes);

  return 0;
}
karlphillip
This is fine for a very simple or contrived example, but if your program has thousands or millions of memory allocations, your mechanism for storing the pointers and lengths better be *very* efficient.
Graeme Perrow
I pointed this out just because I think @siva might be looking at the wrong way to solve his problem.
karlphillip
I'm curious to know why this answer is voted down. Please? IMO, the answer reflects the reality of our day-to-day. I doubt most C programmers would give a different answer.
jweyrich
+1 because it is true that the OP might handle his problem the wrong way. If he needs the length of his memory block for whatever reason, he should include a way to memorize it, maybe even in the allocated block (think at Pascal strings).
tristopia
Change the size type to `size_t` and remove the redundant and dangerous cast to `int *`, and this is indeed how a C programmer would do it. +1
aib
+2  A: 

Many years ago, I programmed on a UNIX-like system that had a msize stdlib function that would return pretty much what you want. Unfortunately, it never became part of any standard.

msize called on a pointer returned from malloc or realloc would return the actual amount of memory the system had allocated for the user program at that address (which might be more than was requested, if it got rounded up for alignment reasons or whatever.)

Chris Dodd
MSVC supports _msize are part of the CRT as well: http://msdn.microsoft.com/en-us/library/z2s077bc(v=VS.80).aspx
Necrolis
A: 

Firstly, as all the others have said, there is no portable way to know the size in allocation unless you keep this information.

All that matters is the implementation of the standard C library. Most libraries only keep the size of memory allocated to a pointer, which is usually larger than the size your program requests. Letting the library keep the requested size is a bad idea because this costs extra memory and at times we do not care about the requested size.

Strictly speaking, recording the requested size is NOT a feature of a compiler. It seems to be sometimes because a compiler may reimplement part of the standard library and override the system default. However, I would not use a library recording requested size because it is likely to have bigger memory footprint due to the reason I said above.