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 1 of 4.
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.
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
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.
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.
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)
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.
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.
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.
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?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers