I'm trying to understand the below, how these 3 function calls are working with no problems, I'm not sure how it works internally, I can understand the first one just fine, but the second time I called malloc with the sizeof the struct then print it, the third time I called malloc with the sizeof the pointer of the structure and also print it. how both can work with no problems? The second malloc allocates the size of 2*2*int=16 bytes and the third malloc allocates be 2*pointer=8. And also I'm getting core dump when trying to free pt2, this is on Linux using C & gcc
#include<stdio.h>
#include<stdlib.h>
struct test{
int field1;
int field2;
};
struct test func(int a, int b) {
struct test t;
t.field1 = a;
t.field2 = b;
return t;
}
int main()
{
struct test t;
struct test pt[2];
pt[0] = func(1,1);
pt[1] = func(2,2);
printf("%d %d\n", pt[0].field1,pt[0].field2);
printf("%d %d\n", pt[1].field1,pt[1].field2);
printf("\n");
struct test *pt1;
pt1 = malloc(sizeof(struct test) * 2);
pt1[0] = func(2,2);
pt1[1] = func(3,3);
printf("%d %d\n", pt1[0].field1,pt1[0].field2);
printf("%d %d\n", pt1[1].field1,pt1[1].field2);
printf("\n");
struct test *pt2;
pt2 = malloc(sizeof(struct test*) * 2);
pt2[0] = func(4,4);
pt2[1] = func(5,5);
printf("%d %d\n", pt2[0].field1,pt2[0].field2);
printf("%d %d\n", pt2[1].field1,pt2[1].field2);
free(pt1);
free(pt2);// I'm getting core dump when trying to free pt2
}
output is below
1 1
2 2
2 2
3 3
4 4
5 5