Thread: Segmentation Fault

  1. #1
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187

    Segmentation Fault

    Hey guys,
    I'm getting a stupid segmentation fault on a piece of code.

    My .h is the following :

    Code:
    typedef struct sFicheiro{
        char *ficheiro;
        int nbits;
        struct sFicheiro *proximo;
    } Node, *Ficheiro;
    
    
    typedef struct Ficheiros {
        int numFicheiros;
        Ficheiro lista;
    } NFicheiros;
    
    
    NFicheiros cria(void);
    NFicheiros insereFicheiro(char *ficheiro, NFicheiros aux);
    void showLib(NFicheiros lib);
    And my .c is as following :

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include "array.h"
    
    
    NFicheiros cria (void)
    {
    NFicheiros aux;
    aux.numFicheiros=0;
    aux.lista=NULL;
    return aux;
    }
    
    
    NFicheiros insereFicheiro(char *ficheiro, NFicheiros aux) {
    printf("1\n");
    	Ficheiro novo = (Ficheiro*)malloc(sizeof (Node));
    printf("2\n");
    	novo->ficheiro=(char*)malloc(sizeof(ficheiro)+1);
    printf("3\n");	
    	novo->nbits= read(ficheiro,stdin,strlen(ficheiro));
    	novo->proximo=NULL;
    	novo->ficheiro=strdup(ficheiro);
    printf("4\n");
    	aux.lista->ficheiro=strdup(ficheiro);
    printf("5\n");
    	if (aux.numFicheiros == 0)
    	{
    printf("5\n");
    		//aux=(NFicheiros)malloc(sizeof(NFicheiros));
    		aux.numFicheiros=1;
    		aux.lista=novo;
    printf("6\n");		
    		return aux;
    	}
    	else
    	{
    		novo->proximo=aux.lista;
    		aux.lista=novo;
    		return aux;
    	}
    	
    }
    
    
    
    
    void showLib(NFicheiros aux)
    {
    	int j = 0;
    	if(aux.numFicheiros == 0) printf("Não existem ficheiros a ser monitorizados.\n");
    	else
    	{
    	while (aux.lista!=NULL) 
    	{
    	printf("%s",aux.lista->ficheiro);
    	aux.lista->proximo;
    	}
    	}
    }
    
    
    int main ()
    {
    NFicheiros aux;
    aux = cria();
    aux=insereFicheiro("ola",aux);
    //showLib(aux);
    printf("%d \n",aux.numFicheiros);
    }
    And the output is :

    Code:
    1
    2
    3
    4
    Falha de segmentação
    Why am I getting an error here :

    aux.lista->ficheiro=strdup(ficheiro);

    ?

    Thanks a lot.

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    505
    aux.lista is set to null in function cria. Then you are passing it to function insereFicheiro and dereferencing it, which gives you the segmentation fault.

  3. #3
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187
    I'm passing it to function insereFicheiro.
    Since it is already set as NULL there shouldn't be a problem should there ?
    Or am I to allocate memory for it ?

    thanks.

  4. #4
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    If the pointer is NULL, you're trying to access an invalid part of memory, hence the segmentation fault. You need to allocate memory first and assign its address to aux.lista before dereferencing.
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  5. #5
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187
    I've worked it out.
    Thanks a lot everyone who answered!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault! Why?
    By cvrcak in forum C Programming
    Replies: 4
    Last Post: 06-25-2008, 07:24 PM
  2. Segmentation fault
    By (Slith++) in forum C++ Programming
    Replies: 4
    Last Post: 05-06-2007, 03:47 AM
  3. segmentation fault??
    By snappleapple in forum C Programming
    Replies: 9
    Last Post: 04-27-2007, 11:56 PM
  4. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM