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):
Quote:
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.