Profetas said:
why do I get segmentation Fault here?
#include <stdio.h>
#include <malloc.h>
The Standard C header file for function malloc
is stdlib.h
typedef enum {FALSE=0, TRUE=1} boolean;
typedef struct {
boolean base[16];
unsigned int fitness;
}individual;
typedef struct{
individual first;
individual second;
}pair;
struct node{
pair member;
struct node* next;
};
typedef struct node Node;
struct node *head=NULL;
struct node *newnode;
struct node *current;
/*--------=========GENERATE POPULATION========---------------*/
generate_population(int size)
{
int loop_counter=0;
/* Initialize the population */
printf("the size is %d\n",size);
newnode=(Node *) malloc (sizeof(Node));
head->next = newnode;
This is likely the source of the seq fault since head is
appears to be a null pointer.
Since function malloc can also return NULL should storage
not being allocated, newnode may also be a null pointer and
can cause the failure.
I would design the function to return a value indicating whether or
not there was a success allocation and node creation.
The general practice in C is not to use global variables, i.e. head,
instead pass around the pointer as arguments in the function.
Using your datatypes, here is an example of possible function
definitions and use.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define BOOLARR 16
typedef enum {FALSE=0, TRUE=1} boolean;
typedef struct
{
boolean base[BOOLARR];
unsigned int fitness;
}individual;
typedef struct
{
individual first;
individual second;
}pair;
typedef struct node
{
pair member;
struct node* next;
} Node;
/*Prototypes */
Node *CreateNode(individual first, individual second);
int AddNode(Node **head, Node *newnode);
void PrintNodes(Node *head);
int AssignIndividual(individual *p, const char *boolarr ,
unsigned fitness);
void FreeNodes(Node **head);
int main(void)
{
individual adam = {{FALSE},0},
bill = {{TRUE,TRUE},16};
Node *head= {NULL};
AddNode(&head,CreateNode(adam,bill));
AssignIndividual(&adam,"ffffttttttttffff",32);
AssignIndividual(&bill,"ttttffttfffffftt",45);
AddNode(&head,CreateNode(adam,bill));
PrintNodes(head);
FreeNodes(&head);
return 0;
}
Node *CreateNode(individual first, individual second)
{
Node *tmp;
tmp = malloc(sizeof *tmp);
if(tmp != NULL)
{
tmp->member.first = first;
tmp->member.second = second;
tmp->next = NULL;
}
return tmp;
}
int AddNode(Node **head, Node *newnode)
{ /* Add to end of list -- newnode->next must be NULL */
Node **tmp;
if(newnode == NULL) return 0;
for(tmp = head; *tmp; tmp = &(*tmp)->next) ;
*tmp = newnode;
return 1;
}
void PrintNodes(Node *head)
{
int i,count;
for(count = 1 ; head; head = head->next,count++)
{
printf("List Pair %d\n\tfirst\n\t\tboolean: ",count);
for(i = 0; i < BOOLARR;i++)
printf("%s ",head->member.first.base
?"T":"F");
putchar('\n');
printf("\t\tfitness: %u\n\n",head->member.first.fitness);
printf("\tsecond\n\t\tboolean: ");
for(i = 0; i < BOOLARR;i++)
printf("%s ",head->member.second.base?"T":"F");
putchar('\n');
printf("\t\tfitness: %u\n\n\n",head->member.second.fitness);
}
return;
}
int AssignIndividual(individual *p, const char *boolarr ,
unsigned fitness)
{
int i;
if(!p ||!boolarr) return 0;
if(BOOLARR != strspn(boolarr,"tf")) return 0;
for(i = 0; i < BOOLARR;i++)
p->base = boolarr=='t'?TRUE:FALSE;
p->fitness = fitness;
return 1;
}
void FreeNodes(Node **head)
{
Node *tmp;
for( ; *head; *head = tmp)
{
tmp = (*head)->next;
free(*head);
}
return;
}