Can somone please tell me why this works?
#include <stdio.h>
#include <stdlib.h>
typedef struct _vertex
{
float x;
float y;
float z;
} Vertex;
int main()
{
Vertex *vertex = malloc(sizeof(Vertex));
vertex->x = 10;
vertex->y = 20;
vertex->z = 30;
printf("x = %f, y = %f, z = %f\n", vertex->x, vertex->y, vertex->z);
printf("the memory allocated for the vertex data is %d bytes.\n", sizeof(vertex));
printf("shouldn't the above be %d bytes?\n", sizeof(Vertex));
free(vertex);
return 0;
}
First it prints that the vertex is 4 bytes in size, but the size of the Vertex struct is 12 bytes (which it should be). I've also changed
malloc(sizeof(Vertex))
to
malloc(0);
and it worked perfectly fine. Is my compiler automatically 'fixing' this? This seems very odd to me.