2

Possible Duplicate:
determine size of dynamically allocated memory in c
newbie questions about malloc and sizeof
How can I get the size of an array from a pointer in C?

Malloc -> how much memory has been allocated?

int **arrofptr;
arrofptr = (int **)malloc(sizeof(int *) * 2);
arrofptr[0] = (int *)malloc(sizeof(int)*6144);
arrofptr[1] = (int *)malloc(sizeof(int)*4800);

Now i have to know that how many bytes are allocated in arrofptr,arrofptr[0],arrofptr[1]? is there any way to know the size?

if we will print

sizeof(arrofptr);
sizeof(arrofptr[0]);
sizeof(arrofptr[1]);

then it will print 4

Community
  • 1
  • 1
Dixit Singla
  • 2,540
  • 3
  • 24
  • 39
  • It's implementation specific at this point. For example when you're in the Linux kernel you can get this information for memory allocated with kmalloc: `stuff = kmalloc(1,GFP_KERNEL); printk("I got: %zu bytes of memory\n", ksize(stuff));` Obviously that only works within the Linux kernel, you'd need to get an answer tied to the malloc implementation you're using. – Mike Oct 09 '12 at 16:09

2 Answers2

4

No, there is no way to find how much memory a pointer is referring to.

At least not on any system, so no portable way.

alk
  • 69,737
  • 10
  • 105
  • 255
2

No. Not without using extra data somewhere that stores the allocated sizes.

HonkyTonk
  • 1,961
  • 11
  • 11