Reputation: 19
Here short int
memory is allocated but in my 64 bit computer it return size 4 instead of 2 ? But How I should create short int
memory allocation and how display it?
int *ptr;
ptr = (int * ) malloc(sizeof(short int));
*ptr = 7;
printf("%d\n", sizeof(*ptr)); // It returns 4 instead of 2
Upvotes: 0
Views: 124
Reputation: 89314
You should be using a pointer to short
.
short *ptr;
ptr = malloc(sizeof(short));
printf("%zu\n", sizeof(*ptr));
Note: a common idiom when allocating memory is to use *ptr
as the expression for sizeof
to use as that will give the correct number of bytes that should be allocated for a variable of this type.
short *ptr = malloc(sizeof(*ptr));
printf("%zu\n", sizeof(*ptr));
Upvotes: 2