Re: Pointer

From: Al Bowers (xabowers_at_rapidsys.com)
Date: 05/20/04


Date: Thu, 20 May 2004 10:51:59 -0400


Profetas wrote:

> 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.

> newnode->next=NULL;

Since function malloc can also return NULL should storage
not being allocated, newnode may also be a null pointer and
can cause the failure.

> current = newnode;
> }
>

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[i]?"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[i]?"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[i] = boolarr[i]=='t'?TRUE:FALSE;
    p->fitness = fitness;
    return 1;
}

void FreeNodes(Node **head)
{
    Node *tmp;

    for( ; *head; *head = tmp)
    {
       tmp = (*head)->next;
       free(*head);
    }
    return;
}

-- 
Al Bowers
Tampa, Fl USA
mailto: xabowers@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/


Relevant Pages