Is it possible to allocate memory that's enough to hold only a specific union member? I mean something like this:
#include <stdio.h>
#include <string.h>
#include <malloc.h>
union u;
union u{
int a;
long b[4096];
};
int main(){
union u tmp = {.a = 42};
union u *u = (union u*) malloc(sizeof(int)); //not malloc(sizeof(union u))
memcpy(u, &tmp, sizeof(int));
printf("u::a = %lu\n", u -> a);
}
sizeof(union u) equals to sizeof(long[4096]) and it would be wasting of memory to allocate 4096 * sizeof(long) to hold a single int.
Is it possible to do such a thing like the one above? Or is it undefined behavior?