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

Vikas Sharma said:   1 decade ago
Subash is absolutely right, the size of the pointer is 2 bytes in DOS environment, but 4 in the WINDOW/UNIX based platforms.

Asha said:   1 decade ago
Here P is integer pointer used for storing the address and address is always 2 bytes hence sizeof P is 2 bytes.

Srinath said:   1 decade ago
Here P is integer pointer used for storing the address and address is always 2 bytes hence sizeof P is 2 bytes.

Subash said:   1 decade ago
Yes its depends on compiler if you compile this program in online compiler it will give 4 as a output.

Muruganandam said:   1 decade ago
It depends upon the compiler if your processor is 8 bit then 2 if it 16 bit then 4 if it 64 then 8.

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

Agree @Vinay & @Raju Naidu.

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

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

Ays said:   1 decade ago
Instead, if P is a character pointer, then also the output is 4. Why ?

Nishant mishra said:   1 decade ago
it depend upon compiler.
2 byte in turbo c and 4 byte in GCC compiler.


Post your comments here:

Your comments will be displayed after verification.