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.

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

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.

Abani said:   1 decade ago
It depends on the compiler which you are using if you are working with turbo C then pointer size is 2 byte and if you are working with GCC(LINUX) then pointer size will be 4 byte irrespective of any datatype.

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

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.

Vinay said:   1 decade ago
If your processor is 16 bit, then 2 if 32 bit then 4, if 64 bit 8.

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.

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.

Sumedh said:   1 decade ago
Its depend upon a compiler

Sahikha said:   1 decade ago
P is integer pointer and it stores 2 bytes so sizeof P is 2 bytes


Post your comments here:

Your comments will be displayed after verification.