yoniyes
yoniyes

Reputation: 1020

valgrind error Invalid read of size 8

So I have valgrind complaining with this error in this function:

int getMembersNum(Party party){
    assert(party != NULL);
    int i = 0;
    while (party->members[i] != NULL && i < party->maxMembersNum) <-- HERE
            i++;
    return i;

}

Party is a pointer to a struct and party->members is of type char**. When initialized with malloc all of party->members cells are set to NULL.

What am I missing?

Upvotes: 0

Views: 4872

Answers (1)

user3458
user3458

Reputation:

You need to test before you use.

while (party->members[i] != NULL && i < party->maxMembersNum)

should be

while (i < party->maxMembersNum && party->members[i] != NULL)

Upvotes: 2

Related Questions