Question

I have the following main function which declares a World structure and reads data from a Xml file, which is stored in the structure:

int main(){
    World *w;
    w = create_world();
    world_load("xmlfile", w);

    return EXIT_SUCCESS;
}

The memory for the World structure is allocated by calling the function create_world():

World *create_world(){
    World *w = NULL;
    if(!(w = (World *)malloc(sizeof (World)))) return NULL;

    return w;
}

The World structure is the following:

struct _World {
        Space *space[MAX_SPACES + 1];
        Object *object[MAX_OBJECTS + 1];
        Link *link[MAX_LINKS + 1];
        Player *player;
};

When I execute the program, a segmentation fault occurs. I've tried passing valgrind and it keep reporting that a "Conditional jump or move depends on uninitialised value". It may be obvious, but I just can't figure out what is the problem... Thanks in advance.

Was it helpful?

Solution

My guess is that you are using the fields of your World struct without actually initializing them. Since those fields are only pointers, they are not allocated when you allocate memory for your World object. This would result in an access violation when accessing w->space[0]. You should either allocate the memory for your arrays, or remove the pointer notation from your arrays in the World struct, depending on how the fields are used.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top