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.

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

Priyanka said:   1 decade ago
Why do a pointer need 2 bytes of memory? will it be possible to make a pointer occupy 1 byte if some body makes its own compiler?

Satish said:   1 decade ago
Vinay absulutely you are right.

Raju Naidu said:   1 decade ago
16 Bit compiler:
It may be any type of pointer(integerm,character,float,double....)it takes the size is 2 bytes.Because pointer holds the address,address always an integer value so the size of any pointer in 16Bit is 2 bytes
32 Bit compiler:pointer size is 4 Bytes(integer size is 4 Bytes)
64 Bit compiler:Pointer size is 8 Bytes(integer size is 8 Bytes)
Thank You
Bye

Dev said:   1 decade ago
Yes it depends upon compiler if it is gcc compiler then the size of p is 4 bytes. If it is tcc compiler then size of p is 2 bytes.

Pranay said:   1 decade ago
compiler dependency

Arun said:   1 decade ago
There are two things. First is that sizeof() function returns the size of variable at compile time. So here the sizeof() function will return the size of pointer not the size of memory chunk the pointer is pointing to.

The second one is the size of a pointer, the size of a pointer is the actual word size of the computer and that depends on machine and for a 16 bits it is 2, for 32 bits it is 4. So here the Answer is 2 bytes.

Srikanth said:   1 decade ago
Here in this programm malloc means it allows block of space for datatype it means int carries 2 bytes free traveres the other memory.

No Fear said:   1 decade ago
It is not possible to identify the memory allocated by malloc, since malloc gives the starting address of the memory allocated. The ending address is no where referred.

Ajinkya said:   1 decade ago
For 32 bits it is 4.
(1)


Post your comments here:

Your comments will be displayed after verification.