I have this structure:
typedef struct
{
int data[10];
} small_structure;
and this code:
small_structure *s_struct;
void * chunk;
chunk = malloc(1000);
s_struct = chunk;
Is it ok to do something like this? Ignore the fact that this is wasting memory.
Yes, that’s fine.
mallocwill return you suitably aligned memory. Just assigning any arbitraryvoid *pointer to asmall_structure *variable is not OK, however. That means your specific example is fine, but something like:is not! If
pisn’t suitably aligned for asmall_structure *pointer, you’ve just caused undefined behaviour.