1

I have the following structs as example:

#define MAX_PEOPLE 16
typedef struct {
 int age;
}Person;
typedef struct {
 Person *people;
 int numPeople;
}Team;

I'm trying to allocate an array of persons in a function, passed by parameters. My Team is supposed to store an array of 16 pointers of Person. I can't figure out what I'm doing wrong.

void initiateTeam(Team * team){
 team->numPeople = MAX_PEOPLE;
 Person *p[MAX_PEOPLE];
 for(int i=0; i<MAX_PEOPLE;i++){
 p[i] = malloc(sizeof(Person);
 }
 team->people = &p[0];
}

I printed out the addresses of my team->people[i] and I'm getting random junk. Why is the assingment team->people = &p[0] wrong? Shouldn't it get the first address of my array then perform pointer arithmetic?

asked Oct 27, 2014 at 18:59
9
  • 1
    for one thing you need to change your malloc to be sizeOf(Person) Commented Oct 27, 2014 at 19:01
  • How are you using that array? You allocated space for pointers, not structures Commented Oct 27, 2014 at 19:02
  • Oh yeah! I see why, for the array, we need the size of there persons not the size the pointer Commented Oct 27, 2014 at 19:03
  • @MarcoA. I will be adding/deleteing People from my Team, Commented Oct 27, 2014 at 19:04
  • The array is allocated on the stack... you are in deep poo poo Commented Oct 27, 2014 at 19:05

5 Answers 5

2

You are pointing team->people to a statically defined array of person pointers. Once the function ends, the stack pointer moves back to where main left off, erasing all previously local memory in the addPeople function. You need to malloc p, and return it from the function

answered Oct 27, 2014 at 19:03
Sign up to request clarification or add additional context in comments.

3 Comments

the array (p) is on the stack, not 'statically'.
@Javier I mean static as in Constant size, not the key word static.
the phrase "a statically defined array" has a definite meaning, and it's not for arrays in the stack, constant size or not.
1

Since in the comments you stated that you're trying to allocate an array of Person objects and not pointers, you should rather do:

void addPeople(Team * team){
 team->numPeople = MAX_PEOPLE;
 team->people = malloc(sizeof(Person) * MAX_PEOPLE);
}

mind that there's no * in sizeof since you don't want an array of pointers but of objects. You will later be able to access the single elements (i.e. each Person object) with

team->people[2].age = 25; // The third person in the array

Finally, remember to free your memory.

answered Oct 27, 2014 at 19:03

Comments

1

your variable p is allocated in the stack of the addPeople() function. The assignment team->people = &p[0] (which is equivalent to team->people = p) is valid but dangerous because that address will be invalid as soon as the function is finished.

Better create p with malloc(sizeof (Person *) * MAX_PEOPLE) instead of using the stack.

answered Oct 27, 2014 at 19:05

Comments

1

The problem is here:

Person *p[MAX_PEOPLE];

This allocates a local variable in the function to hold the array of people pointers.

You get the address of this local variable and send it back. As soon as you are not in the function the local data is freed. It was just allocated locally. It is no longer valid. I might work for a while or it might not depending on the the program does next.

You want this:

Person **p = (Person **)malloc(sizeof (Person *) * MAX_PEOPLE);
answered Oct 27, 2014 at 19:08

Comments

1

I think you are getting the thing with pointer and memory management in C a little bit wrong. Consider reading a little bit more about it, before you continue coding your application.

Beside, I think you do not need an array of pointers to persons, but an array of persons. Your struct is correct, but I would implement your function like that:

void addPeople(Team * team){
 team->numPeople = MAX_PEOPLE;
 team->people = malloc(sizeof(Person) * MAX_PEOPLE);
}

And do not forget to free() your team->people.

But if MAX_PEOPLE is an pre processor define, then it is totally unnecessary to use memory from the heap. If you are not storing too many people in your struct, the stack can easily fulfill your requirements.

answered Oct 27, 2014 at 19:10

2 Comments

Suppose if I allocate the full extent of people memory, how can I access then second, third, person in my people's memory when it's not an array.
You can use the [] operator on a Person *people; variable. Just make sure, that the pointer points to a big enough space of memory, and that you do not access memory outside of its borders.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.