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 1 of 4.

Balaramana said:   2 years ago
As we all know that, the size of the data type depends on the compiler and I also got the Output as 8 when I run it on the online compiler. In this case, the Compiler size is less so that the Output is 2.
(1)

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

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

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

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.

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

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

Agree @Vinay & @Raju Naidu.

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

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

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


Post your comments here:

Your comments will be displayed after verification.