search
Log In
0 votes
69 views
#include<stdio.h>
int main() {
     int *p; 
     p = (int *)malloc(20); 
     printf("%d\n", sizeof(p)); 
     free(p); 
     return 0; 
}

malloc(20) reserves 20 bytes, why output is 8?

in Programming
edited by
69 views
0
p is an integer pointer that stores addresses. In your system, the compiler uses an 8 byte pointer.

In C, you need to manually keep track of the data lenth.

Even though you try to find the sizeof(*p) you will obtain the size of the first integer(in this case) = sizeof(int).
0
Size of int and size of int pointer should not be equal?
3
Size of every type of pointer is same because they all store addresses.  The only reason we have different type of pointer is because thats how the compiler knows how much data should be accessed at once.

There is no relation between size of int and size of int pointer. It can  be different or same depends on the platform (which includes processor, OS, compiler, compiler-options, etc).
0
Thanks a lot

1 Answer

0 votes
sizeof(int *)  is storing address of memory which is pointing to integer data but at last it is storing memory address so it depends on your machine 64 bit or 32 bit machine

sizeof(int ) is size of integer your compiler is supporting

Related questions

0 votes
1 answer
1
449 views
#include<iostream> using namespace std; int i=0; void a() { i+=1; cout<<i<< ".hello"<<endl; a(); } int main() { a(); } For this above code the output is only upto → 64891.Hello Does this mean that that the stack can hold only 64891 recursive calls? (I am using dev c++)
asked Jun 2, 2019 in Programming Hirak 449 views
0 votes
1 answer
2
613 views
#include<stdio.h> #include<stdlib.h> int main(void) { int maxLineCount = 500, maxCharCount = 500, index, j, count; char *line = NULL; size_t size; char *a[maxLineCount]; for (index = 0; index < maxLineCount; index++) a[index] = (char *)malloc(maxCharCount * ... of the code is doing. suppose we have 3 string given as input in 3 different lines then how can we access each character of the string?
asked Jun 1, 2019 in Programming Satbir 613 views
1 vote
1 answer
3
297 views
Implement Linked list using stack.
asked May 19, 2019 in Programming shraddha priya 297 views
2 votes
2 answers
4
258 views
Can someone explain the output of this code? and what (char*) is doing actually? #include<stdio.h> struct Ournode{ char x, y, z; }; int main() { struct Ournode p={'1', '0', 'a'+2}; struct Ournode *q=&p; printf("%c, %c", '*((char*)q+1)', '*((char*)q+2)'); return 0; }
asked May 13, 2019 in Programming srestha 258 views
...