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 2 of 3.
Xyz 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
As size of integer is 2 bytes then it becomes totally 2*4=8 bytes
i.e sizeof(*p) = 8 bytes
Rutuja said:
1 decade ago
@Ritu.
Here sizeof (*p) indicates size of data which is pointed by p which is (2*4) =8.
And sizeof (p) indicates size of integer pointer p which is 2.
Here sizeof (*p) indicates size of data which is pointed by p which is (2*4) =8.
And sizeof (p) indicates size of integer pointer p which is 2.
Arjun said:
1 decade ago
Why does the line p = (int (*) [MAXCOL])malloc(MAXROW *sizeof(*p)); has no effect?
Ashish kumar said:
1 decade ago
Because total memory allocate is 24 byte. But p allocate the first block of memory.
Nitika mishra said:
1 decade ago
Why does the line p = (int (*) [MAXCOL])malloc(MAXROW *sizeof(*p)); has no effect?
Can anyone explain its reason in detail. Please?
Can anyone explain its reason in detail. Please?
Sonali jain said:
1 decade ago
Integer takes a 4 bytes and 2 bytes sizeof operator than
2*4 = 8 bytes.
sizeof(*p) = 8 bytes.
2*4 = 8 bytes.
sizeof(*p) = 8 bytes.
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?
Ankit said:
1 decade ago
If you compile this code you get output 4, 16. And in answer they put 2, 8.
Archana said:
1 decade ago
Sizeof (p) = 2 because in p address of allocated memory is stored which is 2 Bytes.
Sizeof (*p) = 8 because *p is pointer to array of 4 Integers 4*2 = 8.
Sizeof (*p) = 8 because *p is pointer to array of 4 Integers 4*2 = 8.
(1)
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers