C Programming - Memory Allocation - Discussion
Discussion Forum : Memory Allocation - Find Output of Program (Q.No. 5)
5.
Assume integer is 2 bytes wide. How many bytes will be allocated for the following code?
#include<stdio.h>
#include<stdlib.h>
#define MAXROW 3
#define MAXCOL 4
int main()
{
int (*p)[MAXCOL];
p = (int (*) [MAXCOL])malloc(MAXROW *sizeof(*p));
return 0;
}
Discussion:
21 comments Page 2 of 3.
Mohini said:
1 decade ago
Here why is sizeof(*p) taken as 2,but not 4*2=8?.
no no i think viraj is not correct,
and we are taking sizeof(*p) as 8 and then multiplying it with 3 to get 24
no no i think viraj is not correct,
and we are taking sizeof(*p) as 8 and then multiplying it with 3 to get 24
Subrata Bishal said:
9 years ago
@Mohini is correct.
So, sizeof(*p), it will take 8 bytes. Then multiplying with row number it gets (8 * 3 = 24) i.e 24 bytes.
So, sizeof(*p), it will take 8 bytes. Then multiplying with row number it gets (8 * 3 = 24) i.e 24 bytes.
(3)
Harshad said:
9 years ago
I am using Dev C++ and after compilation, it's not showing anything. Can anyone help to recover this?
(1)
Swetha said:
10 years ago
In array memory can be define we use.
No. of rows*no. of column*datatype memory.
3*4*2 = 24.
No. of rows*no. of column*datatype memory.
3*4*2 = 24.
Shadab said:
10 years ago
p is a pointer to array of MAXCOL integer.
So sizeof(p) will be sizeof(int) * sizeof pointer.
So sizeof(p) will be sizeof(int) * sizeof pointer.
Viraj said:
1 decade ago
p is pointer to integer array of size 4
no of rows= 3
no of cols= 4
size of int=2
3*4*2=24
no of rows= 3
no of cols= 4
size of int=2
3*4*2=24
Chandru said:
9 years ago
@Sunil and @Robert.
Thanks for your answer.
Thanks for your answer.
(1)
Hrishikesh Jagtap said:
3 years ago
int 2.
So *p size is 8.
And hence 8*3.
24.
So *p size is 8.
And hence 8*3.
24.
(2)
Shivshankar said:
9 years ago
@Tarun.
You are right.
You are right.
(1)
Vikram said:
1 decade ago
Mohini is correct
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers