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;
}
Discussion:
31 comments Page 2 of 4.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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
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
Satish said:
1 decade ago
Vinay absulutely you are right.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers