0

I defined a data structure (Foo) that contains an array of 25 pointers (members) to itself. I want to initialize each of those pointers to NULL, but my init function isn't working correctly. When my Foo f returns from foo_init(), only some of the members are NULL, others are just populated with random values.

//in Foo.h
#include <stdio.h>
#include <stdlib.h>

typedef struct Foo {
    struct Foo * members[25] ;
} Foo ;

void foo_init(Foo * f) ;

//in Foo.c
void foo_init(Foo * f) {
    f = (Foo*)malloc(sizeof(Foo));

    for (size_t i = 0 ; i < 25 ; i++) {
        f->members[i] = NULL ;
    }
    /* Ok here, all members are NULL */
}

//in main.c
#include "Foo.h"

int main(int argc, const char * argv[])
{

    Foo f ;
    foo_init(&f) ;

    /* why isn't every index of f.members NULL? */


    /* ... */
    return 0;
}

I ran my code through LLDB. Inside foo_init(), all members are NULL. But after returning from foo_init(), f.members is full of random garbage values.

7
  • 1
    Doesn't your compiler complain about your code? Commented Jun 20, 2014 at 12:03
  • @ThomasPadron-McCarthy: I guess it should ... Commented Jun 20, 2014 at 12:03
  • It was a typo, sorry. Fixed it. Commented Jun 20, 2014 at 12:05
  • Why malloc into your class object? Commented Jun 20, 2014 at 12:06
  • You are allocating heap memory for something that was already declared on the stack in your first line in foo_init. Get rid of that line. Commented Jun 20, 2014 at 12:07

3 Answers 3

5

The address of the Foo in your main is immediately overwritten by the call to malloc. Just remove that line and your initialization should work.

If you really want foo_init to allocate memory you can convert it to :

void foo_init(Foo **out_f) {
    Foo *f = malloc(sizeof *f);

    for (size_t i = 0 ; i < 25 ; i++) {
        f->members[i] = NULL;
    }

    *out_f = f;
}

Or simply return a pointer as it is idiomatic in C :

Foo *foo_init(void);
Sign up to request clarification or add additional context in comments.

2 Comments

Did the compilator know the type of *f in moment of sizeof? Thx i learned something today :)
Can you please tell me why do you use a pointer to a pointer Foo **out_f?
2

Try this then

Foo *f ;
foo_init(&f) ;


void foo_init(Foo ** f) {
    *f = (Foo*)malloc(sizeof(Foo));

    for (size_t i = 0 ; i < 25 ; i++) {
        (*f)->members[i] = NULL ;
    }
}

@TripeHound is right

2 Comments

Whoops, that was just a typo. My actual code already looks like Foo f ; foo_init(&f) ;
Which won't work because the first thing the function does is throw away the passed-in value and malloc a new structure.
1

another option is to use a pointer to pointer if you want to allocate Foo inside foo_init(). This way f points to valid memory after returning from foo_init

void foo_init(Foo ** f) {
    *f = (Foo*)malloc(sizeof(Foo));

    for (size_t i = 0 ; i < 25 ; i++) {
        (*f)->members[i] = NULL ;
    }
}

int main()
{
    Foo *f;

    foo_init(&f);

    return 0;
}

Comments

Your Answer

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.