Structure of structures + linked list

This is a discussion on Structure of structures + linked list within the C Programming forums, part of the General Programming Boards category; Hi. I'm using a structure of structures, inside of which is a linked list, to pass information through a program. ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    21

    Structure of structures + linked list

    Hi. I'm using a structure of structures, inside of which is a linked list, to pass information through a program. Unfortunately, occasionally it will have a segmentation fault.

    Though more specific to Linux (I wrote the program on cygwin - which worked ok, moved onto Linux, compiled fine and then broke.. badly). I've been using Valgrind to try and narrow down the errors, and there's quite a few; of which I'm struggling to understand where the error is.

    This is the structure declaration in a globals.h header file
    Code:
    typedef struct envSettings {
         struct {
            double answer; 
            char problem[20];
        } problem;
    
         struct {
            unsigned int envID;            
            unsigned int currentPopLevel;   
            unsigned long runningTime;
            double fittestValue; 
            double fittestScore;
            short mutateOn;
            unsigned long generationNo;
            unsigned long noOfChildren;
        }env;
    
        entity solution;
    } envSettings;
    
    typedef envSettings *environment;
    The entity typedef would be:
    Code:
    typedef struct data{
        double fitnessScore;
        unsigned int charSolID;
        unsigned int parent1SolID;
        unsigned int parent2SolID;
        double value;
        char **character;
        short noOfChildren;
        struct data *next;
    } data;
    
    typedef data *entity;
    In main (main.c) I'm doing the following:
    Code:
    environment instance = NULL;
        if( (instance = (environment)malloc(sizeof(envSettings))) == NULL ) {
            MALLOCERR;
        }
        instance->solution = NULL;    
        
        /* Initialise all variables */
        instance = (environment)initVars(instance);
        if( (instance = (environment)loadSettings(instance,"defaults.ini")) == NULL ) {
            free(instance);
            exit(1);
        }
    With the call to initVars() doing the things such as:
    Code:
    environment initVars( environment instance ) {
       (void)strcpy(instance->problem.problem,"Sqrt");
        instance->problem.answer = 0;
    
        instance->env.envID = 0;
        instance->env.currentPopLevel = 0;
        instance->env.startTime = time(0);
        instance->env.endTime = 0;
    
       ...
       return instance;
    }
    The errors I'm seeing in valgrind all revolve around (though the size differs sometimes):
    Invalid write of size 4
    ...
    Address 0xnnnnnnn is not stack'd, malloc'd or (recently free'd)
    Would anyone kind enough please point out how I'm misusing structures of structures. TIA.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,049
    Are those objects (in red) part of the struct env?
    Code:
    typedef struct envSettings {
         struct {
            double answer; 
            char problem[20];
        } problem;
    
         struct {
            unsigned int envID;            
            unsigned int currentPopLevel;   
            unsigned long runningTime;
            double fittestValue; 
            double fittestScore;
            short mutateOn;
            unsigned long generationNo;
            unsigned long noOfChildren;
        }env;
    
        entity solution;
    } envSettings;
    
    typedef envSettings *environment;
    .
    .
    .
    typedef struct data{
        double fitnessScore;
        unsigned int charSolID;
        unsigned int parent1SolID;
        unsigned int parent2SolID;
        double value;
        char **character;
        short noOfChildren;
        struct data *next;
    } data;
    
    typedef data *entity;
    .
    .
    .
    environment initVars( environment instance ) {
       (void)strcpy(instance->problem.problem,"Sqrt");
        instance->problem.answer = 0;
    
        instance->env.envID = 0;
        instance->env.currentPopLevel = 0;
        instance->env.startTime = time(0); /* is startTime part of the env struct */
        instance->env.endTime = 0;         /* is endTime part of the env struct */
    
       ...
       return instance;
    }

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    Code:
    (instance = (environment)loadSettings(instance,"defaults.ini")) == NULL
    I guess loadSettings() returns null when it fails, in which case you loose the reference to instance and the following call free(instance) is in fact a free(0) which, despite allowed, does nothing. Valgrind errors probably come from the fact you're using an invalid reference to instance, besides the case I pointed out, you probably loose the reference somewhere else in your code. I don't think this is a good idea to re-assign this variable each time you call a function modifying it.
    Last edited by root4; 03-04-2009 at 03:35 PM.

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    21
    Hi, thanks for the replies. Finally found some time to go through the program in more detail. As it turns out, although the convention I was using to allocate memory to the 2-dimensional arrays wasn't wrong, I found what I believe to be a more appropriate method to allocate the memory - so that all the data were in contiguous memory (found in the C-Faq (http://c-faq.com/aryptr/dynmuldimary.html - I won't be losing this link any time soon..).

    The issue in the program was with when i was choosing to delete structures from the linked list. The logic being, if a new child entity were to be created, it would need space, and every so often a happy-go-lucky parent would be deleted just before a variable in its structure was accessed..

    Anyway, thank you both for the replies, and apologies on the ambiguous initial post.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. structure and a linked list help
    By mikecool291 in forum C Programming
    Replies: 1
    Last Post: 05-10-2009, 11:39 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  3. Duplicating value of pointer to linked list
    By zephyrcat in forum C Programming
    Replies: 14
    Last Post: 01-22-2008, 03:19 PM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21