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.
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.
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.
Tofik Kacchi said:
1 decade ago
Here initially *p[3] will statically create an array of p[3].
Then again reallocating using malloc will create another array of size 3.
Therefore, its p[3][3].hence we require 2 nested for loop.
Then again reallocating using malloc will create another array of size 3.
Therefore, its p[3][3].hence we require 2 nested for loop.
Pragya said:
1 decade ago
@ Teju
Yes. It allocates 54 bytes because
sizeof(*p)=6 bytes
So 3 * 6 = 18 bytes
and then int (*)[3] points to an array of 3 blocks each having 18 bytes.
So p = (18 + 18 + 18) = 54 bytes.
Yes. It allocates 54 bytes because
sizeof(*p)=6 bytes
So 3 * 6 = 18 bytes
and then int (*)[3] points to an array of 3 blocks each having 18 bytes.
So p = (18 + 18 + 18) = 54 bytes.
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.
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.
Gopi said:
1 decade ago
Here, int(*p)[3]; //its like p[3]
p = (int(*)[3])malloc(3*sizeof(*p));
and again we are allocating more memory to it so like p[3][3] .
p = (int(*)[3])malloc(3*sizeof(*p));
and again we are allocating more memory to it so like p[3][3] .
Bhargav said:
1 decade ago
Here type casting and it is array of pointers. So we have to access by using double pointer. So option C p[i][j] is similar to that.
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.
Sharadha said:
1 decade ago
Can anyone specify the correct answer for this and specify the total amount of memory allocated to it.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers