Does allocating a struct instance using malloc have any effect on the allocation of its members? Meaning that, is doing something like this:
typedef struct { int *i; } test; int main() { test *t = malloc(sizeof(test)); t->i = malloc(sizeof(int)); } ... is meaningless because i should be already on the heap, right ?
Or, the struct is just conceptual to help the programmer group two completely separate variables floating in memory? Meaning that: test *t = malloc(sizeof(test)) just allocates the memory for storing pointers to the members on the heap?
I'm quite baffled about this ..
t->i = malloc( sizeof(int) );should give you a compiler warning that you are casting a pointer to an int. You do not need to allocate the memory for theinton the heap unless the struct was defined asstruct { int* i; }