0
#include<stdio.h>
int main(){
    int * ptr=(int *)malloc(40);
    printf("%d",sizeof(ptr));
}

When I'm running this, output is coming as 8...what is happening here...why is output 8?

user195661
  • 35
  • 5
  • 1
    Because that's the size of `ptr`. What did you expect? – JasonD Feb 26 '13 at 07:03
  • but when I give sizeof(int), system prints 4; but why is here size of pointer became 8..??? – user195661 Feb 26 '13 at 07:05
  • 1
    Just as a note: what you're doing is officially undefined behavior, because sizeof doesn't yield an `int`, but you've used `%d`, which tells `printf` that what you've passed is an int. Probably makes no difference though. – Jerry Coffin Feb 26 '13 at 07:05
  • So that means, in a 64bit machine int size remains 4, but pointer size is 8...and in 32bit machine pointer size becomes 4...am i right..correct me if i am wrong.. – user195661 Feb 26 '13 at 07:12
  • You're wrong to assume the size of either of those things depends only on the 'bit-ness' of the machine. Though in most cases your assumption is correct for pointers, 'int' sizes are less consistent. – JasonD Feb 26 '13 at 07:26
  • @user195661, Please check http://stackoverflow.com/a/2331768/67381 and http://stackoverflow.com/a/10197339/67381 for your understanding. – Siddiqui Feb 26 '13 at 07:33

5 Answers5

2

The sizeof operator is a compile time operator, and the size of any pointer is 8 bytes on your machine.

There is no way to retrieve the dynamic size of the malloc-ed memory zone. You have to know it otherwise, or to keep it somewhere.

You might consider using Boehm's conservative garbage collector then you'll call GC_malloc instead of malloc, you won't need to call free (or GC_free), and you could use GC_size(p) to get the approximate size of the previously GC_malloc-ed memory zone starting at p (but I don't recommend using GC_size).

If using malloc on Linux, learn how to use valgrind to hunt memory leak bugs, and compile with gcc -Wall -g

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
2

malloc() returns a pointer to 40 bytes of memory (probably 10 ints) and assigns it to ptr. The reason sizeof(ptr) is 8 is because you are using a 64 bit machine and pointers are 8 bytes in size.

You should use sizeof() inside the malloc() because it's good form and avoids problems if the type ever changes size (crossing platforms or whatever). If you really want space for 10 ints then use:

int *ptr = malloc(10 * sizeof *ptr);

This allocates 10 lots of the size of the type ptr points to, in this case int. The benefit of doing this is you can change the type without changing the malloc()

SpacedMonkey
  • 2,725
  • 1
  • 16
  • 17
0

The variable ptr is a pointer to int, an your system it happens to have the size of 8 byte.

Mithrandir
  • 24,869
  • 6
  • 50
  • 66
0

ptr is a pointer.its printing the size fo the pointer on your system. as ptr holds an address, 8 bytes is the size required to hold an address on a 64 bit machine. This is compiler specific.You cannot retrieve the size allocated to a pointer.

if you getting this doubt,then you should also get a doubt of how does free() will free the memory without knowing the size that has been allocated to the pointer.

Vijay
  • 65,327
  • 90
  • 227
  • 319
0

I have tried this on my Windows 32bit machine, i am getting ans 4 bytes, i think as ptr is a pointer to int show its showing the corre