0

I am trying to write a c program that can print the size of a pointer variable that points to a memory location allocated by malloc().

C version I'm using is C99.

Below is my code :

#include<stdio.h> 

int main() 
{ 
int *temp; 
temp=malloc(sizeof(int)*3); 
printf("%d %d\n",sizeof(int),sizeof(temp)); 
return 0; 
} 

Output I'm getting is :

sh-4.2$ ./f1 4 8

according to the code, printf should print same values for both expressions. i.e 12. sizeof(int)=4.

Since i have allocated 12 bytes of memory for temp, sizeof(temp) should return 12. But, instead it returns the value 8.

I'm wondering what could be the reason? If anyone knows ,please answer.

RaJ
  • 129
  • 8

1 Answers1

6

sizeof does not return size of dynamically allocated memory, it just prints size of variable, 8 is size of temp pointer variable not the size of memory pointed by address in temp

Pras
  • 4,047
  • 10
  • 20