C Programming - Memory Allocation - Discussion
Discussion Forum : Memory Allocation - Point Out Correct Statements (Q.No. 1)
1.
Point out the correct statement will let you access the elements of the array using 'p' in the following program?
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i, j;
int(*p)[3];
p = (int(*)[3])malloc(3*sizeof(*p));
return 0;
}
Discussion:
17 comments Page 1 of 2.
Zdd said:
8 years ago
I know why some people think that sizeof(*p)=6 bytes while others consider 12 bytes
because sizeof(*p) = 3 * sizeof(int),
sizeof(int) = 2 bytes in 16 bit OS
sizeof(int) = 4 bytes in 32 bit OS
sizeof(int) = 8 bytes in 64 bit OS
so, total memory allocated are different.
because sizeof(*p) = 3 * sizeof(int),
sizeof(int) = 2 bytes in 16 bit OS
sizeof(int) = 4 bytes in 32 bit OS
sizeof(int) = 8 bytes in 64 bit OS
so, total memory allocated are different.
Ajith said:
8 years ago
As it is 3x3 matrix have created the block b is used.
Saleh Tarek said:
8 years ago
-The trick here is all about the difference between sizeof(p) and sizeof(*p).
-p is a pointer to array of 3 integers --> sizeof(p)=4 bytes AND p=&(array of 3 integers) . On the other hand,
-*p= *(&(array of 3 integers))= array of 3 integers --> sizeof(*p)=12 bytes.
Consequently, 3*12 bytes=36 bytes would be allocated on the Heap. Each 12 bytes includes 3 integers
p = ( int(*)[3]) malloc(3*sizeof (*p) ); ----> This statement means, take the address that malloc function would return and treat with it as it were an address of an array of 3 integers.
So, p+1=p+ 12 bytes.
That is why we use i to jump from an array to another.
p+i= &p[i] AND *(p+i)=p[i].
p[i] is an array of 3 integers ---> so it is a pointer to its 1st element (which is an integer)----->
So, p[i]+1=p[i]+ 4 bytes.
That is why we use j to jump from an integer to another inside a p[i] array.
I hope this detailed explanation helps.
-p is a pointer to array of 3 integers --> sizeof(p)=4 bytes AND p=&(array of 3 integers) . On the other hand,
-*p= *(&(array of 3 integers))= array of 3 integers --> sizeof(*p)=12 bytes.
Consequently, 3*12 bytes=36 bytes would be allocated on the Heap. Each 12 bytes includes 3 integers
p = ( int(*)[3]) malloc(3*sizeof (*p) ); ----> This statement means, take the address that malloc function would return and treat with it as it were an address of an array of 3 integers.
So, p+1=p+ 12 bytes.
That is why we use i to jump from an array to another.
p+i= &p[i] AND *(p+i)=p[i].
p[i] is an array of 3 integers ---> so it is a pointer to its 1st element (which is an integer)----->
So, p[i]+1=p[i]+ 4 bytes.
That is why we use j to jump from an integer to another inside a p[i] array.
I hope this detailed explanation helps.
Emanuel said:
9 years ago
I don't understand. P points to p[0] which is a pointer to an integer and this is a (int**) of malloc. And how to use the additional memory? I don't understand.
Vikrant lakhera said:
1 decade ago
@Vikrant.
There is total memory allocated is 18 bytes.
There is total memory allocated is 18 bytes.
Atul said:
1 decade ago
Its like 3x3 array so total 9 elements and pointer variable always occupies 2 byte therefore it will take 9x2=18 bytes.
Faizan said:
1 decade ago
@Sharadha it allocates 18 bytes.
Sharadha said:
1 decade ago
Can anyone specify the correct answer for this and specify the total amount of memory allocated to it.
Rishi said:
1 decade ago
Total memory allocated is 18 bytes.
Bandna said:
1 decade ago
int *p[3] & int (*p)[3] are two different things.
First one is array of 3 integer pointers and second one is pointer to an array of 3 integers.
First one is array of 3 integer pointers and second one is pointer to an array of 3 integers.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers