malloc for struct and pointer in C
No, you’re not allocating memory for y->x twice. Instead, you’re allocating memory for the structure (which includes a pointer) plus something for that pointer to point to. Think of it this way: So you actually need the two allocations (1 and 2) to store everything. Additionally, your type should be struct Vector *y since it’s a pointer, and you should never cast the return value … Read more