4

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 ..

3
  • 2
    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 the int on the heap unless the struct was defined as struct { int* i; } Commented Oct 22, 2014 at 17:35
  • stackoverflow.com/questions/18466304/… Commented Oct 22, 2014 at 17:35
  • 1
    @Klapsa2503: Oh yes, I'll edit that. Commented Oct 22, 2014 at 17:36

4 Answers 4

4

The i field of test is a primitive int, and mallocing test will take care of the memory needed by it. Assigning a result of malloc to i should produce a warning, as you're implicitly converting a pointer to an int.

EDIT:
As pointed out in the comments, the question has been edited since the above answer was posted. If your struct contains a pointer, mallocing the struct will allocate the memory to hold a pointer, but will not allocate the memory this pointer points to. For that, you'll need a second malloc call (e.g., test->i = malloc (sizeof (int))).

Sign up to request clarification or add additional context in comments.

1 Comment

FYI this answer appears to be wrong due to edits to the question.
1

Given a struct type "test", malloc(sizeof(test)) allocates space for a complete struct, including all members and any padding between. You are perhaps confused about the case where one or more of the members is a pointer:

typedef struct { char *string; char array[8]; } test; 

In that case, the pointer itself is a member, and space is allocated for it, but the thing pointed to is not a member, and no space is allocated for that.

Contrast that with the case where the struct has an array member: the whole array is a member of the struct, and sufficient space is allocated in the struct for all its elements.

2 Comments

So if I have a struct that contains a pointer to a struct of some other kind, and I allocate it using malloc, then I'd have to use malloc again on the member struct so that it is allocated dynamically too, right ?
Yes, the sizeof the container struct (determining how much memory to allocate) includes space for the inner pointer, but not for a separate "inner" struct that could be pointed to. If you don't want to point to an independent, pre-existing inner struct (as you might want in a linked list, for example) then you need to allocate memory separately for such an inner struct.
1

You would only allocate the memory for i if i was a pointer to an int. See this example:

#include <stdlib.h> typedef struct { int i; } test; typedef struct { int* i; } testp; int main() { test* foo = malloc(sizeof(*foo)); foo->i = 3; testp* bar = malloc(sizeof(*bar)); bar->i = malloc(sizeof(*bar->i)); *bar->i = 3; } 

When you allocate the struct, there is already space allocated for the int i.

Comments

1

When you call the first malloc, there is enough memory to hold the whole structure, including the '*i', which has enough place to hold a pointer to an integer. The field *i however remains uninitialized! so you must not use *i before you assigned it with the second malloc that reserves memory for an integer.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.