C Programming - Memory Allocation - Discussion

Discussion Forum : Memory Allocation - Find Output of Program (Q.No. 2)
2.
What will be the output of the program (16-bit platform)?
#include<stdio.h>
#include<stdlib.h>

int main()
{
    int *p;
    p = (int *)malloc(20);
    printf("%d\n", sizeof(p));
    free(p);
    return 0;
}
4
2
8
Garbage value
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
31 comments Page 3 of 4.

Ruchita said:   10 years ago
sizeof(pointer) always returns size of pointer i.e size of address and it varies compiler to compiler.

char *ptr;
int *ptr;
float *ptr;

For all, sizeof(ptr) will be same, for 16 bit compiler its 2 bytes and for 32 its 4 bytes.

Sarvajeet Suman said:   10 years ago
For GCC compiler it's 4 & not 2.

Muzammil said:   9 years ago
It depends on the compiler for Linux platform pointer size is 4 and in windows, it's 2.

Yash said:   8 years ago
@Vinay and @Raju Naidu are absolutely correct.

Prafull said:   8 years ago
I think in question it's already mentioned 16-bit so answer is 2.

Agree @Vinay & @Raju Naidu.

Suraj said:   8 years ago
Answer is 4 because integer pointer is size is 4byte.

Rohit Sundaram said:   7 years ago
In 32-bit machine, the sizeof pointer is 32 bits (4 bytes) , while on a 64-bit machine it's 8 bytes. Regardless of what data type they are pointing to, they have fixed size.

Aniket potabatti said:   7 years ago
P is an integer pointer and it contains 2 bytes of memory so answer is 2.

Vijay kumar said:   6 years ago
The answer is 8 because the size of a pointer is always 8.

Sripadh said:   5 years ago
But if I run this program it is showing the output as 8.
(1)


Post your comments here:

Your comments will be displayed after verification.