C Programming - Memory Allocation - Discussion
Discussion Forum : Memory Allocation - Find Output of Program (Q.No. 6)
6.
Assume integer is 2 bytes wide. What will be the output of 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));
printf("%d, %d\n", sizeof(p), sizeof(*p));
return 0;
}
Discussion:
24 comments Page 1 of 3.
Madhu said:
10 years ago
What is the difference between the two programs given below?
1.
#include<stdio.h>
#include<stdlib.h>
#define MAXROW 3
#define MAXCOL 4
int main()
{
int (*p)[MAXCOL];
printf("%d %d\n",sizeof(p),sizeof(*p));
// p = (int (*) [MAXCOL])malloc(MAXROW *sizeof(*p));
printf("%d %d\n",sizeof(p),sizeof(*p));
return 0;
}
2.
#include<stdio.h>
#include<stdlib.h>
#define MAXROW 3
#define MAXCOL 4
int main()
{
int (*p)[MAXCOL];
printf("%d %d\n",sizeof(p),sizeof(*p));
p = (int (*) [MAXCOL])malloc(MAXROW *sizeof(*p));
printf("%d %d\n",sizeof(p),sizeof(*p));
return 0;
}
1.
#include<stdio.h>
#include<stdlib.h>
#define MAXROW 3
#define MAXCOL 4
int main()
{
int (*p)[MAXCOL];
printf("%d %d\n",sizeof(p),sizeof(*p));
// p = (int (*) [MAXCOL])malloc(MAXROW *sizeof(*p));
printf("%d %d\n",sizeof(p),sizeof(*p));
return 0;
}
2.
#include<stdio.h>
#include<stdlib.h>
#define MAXROW 3
#define MAXCOL 4
int main()
{
int (*p)[MAXCOL];
printf("%d %d\n",sizeof(p),sizeof(*p));
p = (int (*) [MAXCOL])malloc(MAXROW *sizeof(*p));
printf("%d %d\n",sizeof(p),sizeof(*p));
return 0;
}
JUBIN SHARMA said:
7 years ago
Here:
P = (int (*) [MAXCOL]) malloc (MAXROW *sizeof (*p)); is allocating a memory of 24 bytes to the pointer P but as it is a pointer to 2D array it will hold the base Row address i.e. 8.
Therefore *P holds up 8*3 bytes but will show 8 bytes i.e the size of its row in sizeof(*P).
And P is itself a pointer to an array of 2*4 bytes and its row holds 2 bytes each.
Actual memory consumed by both members is 2*4 and 8*3 bytes but a pointer holds the base address and thus the first element's address will be shown in sizeof() i.e. 2 and 8 bytes.
P = (int (*) [MAXCOL]) malloc (MAXROW *sizeof (*p)); is allocating a memory of 24 bytes to the pointer P but as it is a pointer to 2D array it will hold the base Row address i.e. 8.
Therefore *P holds up 8*3 bytes but will show 8 bytes i.e the size of its row in sizeof(*P).
And P is itself a pointer to an array of 2*4 bytes and its row holds 2 bytes each.
Actual memory consumed by both members is 2*4 and 8*3 bytes but a pointer holds the base address and thus the first element's address will be shown in sizeof() i.e. 2 and 8 bytes.
(2)
Sundar said:
1 decade ago
The given answer is exactly correct.
In Turbo C under DOS (16 bit OS), the size of the integer is 2 bytes. So, if you run the above code in Turbo C the output will be 2, 8.
If you run the same code in Linux (32 bit OS) the output will be 4, 16. Because the size of the integer in Linux is 4 bytes.
I have tested in both Turbo C (in DOS) and GCC (in Linux).
I hope this will help you. Have a nice day!
In Turbo C under DOS (16 bit OS), the size of the integer is 2 bytes. So, if you run the above code in Turbo C the output will be 2, 8.
If you run the same code in Linux (32 bit OS) the output will be 4, 16. Because the size of the integer in Linux is 4 bytes.
I have tested in both Turbo C (in DOS) and GCC (in Linux).
I hope this will help you. Have a nice day!
Arjun said:
1 decade ago
#include<stdio.h>
#include<stdlib.h>
#define MAXROW 3
#define MAXCOL 4
int main()
{
int (*p)[MAXCOL];
p = (int (*) [MAXCOL])malloc(MAXROW *sizeof(*p));
printf("%d, %d\n", sizeof(p), sizeof(*p));
return 0;
}
Could someone explain this code to me?
#include<stdlib.h>
#define MAXROW 3
#define MAXCOL 4
int main()
{
int (*p)[MAXCOL];
p = (int (*) [MAXCOL])malloc(MAXROW *sizeof(*p));
printf("%d, %d\n", sizeof(p), sizeof(*p));
return 0;
}
Could someone explain this code to me?
Souvik said:
10 years ago
#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;
}
What is the difference with this code? It gives the output 24. Please explain.
#include<stdlib.h>
#define MAXROW 3
#define MAXCOL 4
int main()
{
int (*p)[MAXCOL];
p = (int (*) [MAXCOL])malloc(MAXROW *sizeof(*p));
return 0;
}
What is the difference with this code? It gives the output 24. Please explain.
Satish Naik said:
1 decade ago
p is a pointer pointing to array of intergers of size 4
As size of integer is 2 bytes then it becomes totally 2*4=8 bytes
i.e sizeof(*p) = 8 bytes
where as
sizeof(p) = 2 bytes as p is integer as holds the address in unsigned integer form.
As size of integer is 2 bytes then it becomes totally 2*4=8 bytes
i.e sizeof(*p) = 8 bytes
where as
sizeof(p) = 2 bytes as p is integer as holds the address in unsigned integer form.
Durgam_anil said:
1 decade ago
p is a pointer pointing to array of intergers of size 4
As size of integer is 2 bytes then it becomes totally 2*4=8 bytes
i.e sizeof(*p) = 8 bytes
where as
sizeof(p) = 2 bytes as p is integer as holds the address in unsigned integer form.
As size of integer is 2 bytes then it becomes totally 2*4=8 bytes
i.e sizeof(*p) = 8 bytes
where as
sizeof(p) = 2 bytes as p is integer as holds the address in unsigned integer form.
Vinaykumar said:
7 years ago
Here p is a pointer.so sizeof(p) is either become 4 bytes or 8 bytes.But here how sizeof(p) is 2 bytes.
int is 2 bytes with 4 elements so sizeof(*p) is 8 bytes (2*4). So the answer is 4(or)8, 8.
int is 2 bytes with 4 elements so sizeof(*p) is 8 bytes (2*4). So the answer is 4(or)8, 8.
(1)
Mohini said:
1 decade ago
in the program ,this
p = (int (*) [MAXCOL])malloc(MAXROW *sizeof(*p));
has no effect in the output.
this will change the value of p,but it will have not change size of p.
p = (int (*) [MAXCOL])malloc(MAXROW *sizeof(*p));
has no effect in the output.
this will change the value of p,but it will have not change size of p.
Emanuel said:
9 years ago
I don't understand.
P = (int (*) [MAXCOL]) malloc (MAXROW *sizeof (*p));
So, sizeof (*p) 4 * 2 and malloc gives 3 * 8.
So, the sizeof (*p) will be 24.
P = (int (*) [MAXCOL]) malloc (MAXROW *sizeof (*p));
So, sizeof (*p) 4 * 2 and malloc gives 3 * 8.
So, the sizeof (*p) will be 24.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers