A simple problem?

This is a discussion on A simple problem? within the C Programming forums, part of the General Programming Boards category; I, for whatever reason, get a seg fault when ever I do Code: soil->root = NULL I also find that ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    19

    Question A simple problem?

    I, for whatever reason, get a seg fault when ever I do

    Code:
    soil->root = NULL

    I also find that when I try the following I get a seg fault as well.

    Code:
    soil -> root = (struct entry *) malloc (sizeof(struct entry));

    My current hypothesis is that the server I am using will not allow me to access its memory for whatever reason. Though I have tried other programs, and they work just fine, even though they need access to memory. But before my server went down for about 6 hours, everything was just fine upto linking a Binary tree (which I'll have another thread for).

    Here is what my code looks like up to the seg fault:

    Code:
    /////////////////////////////////////////////////////////////////////
    // Program      : Lab05
    // File         : lab05h.c
    // Function     : header
    // Description  : Header file for Lab05
    /////////////////////////////////////////////////////////////////////
    
    # include <ctype.h>
    # include <stdio.h>
    # include <string.h>
    # include <stdlib.h>
    
    # define TRUE 1
    # define FALSE 0
    # define YES 1
    # define NO 0
    # define WORD 32
    # define MAX 81
    # define REMOVE "\\\"\%/`~!@#$^&*()-+={}|[];<>?: "  
        //Used for strtok  ",.' " not included 
    
    
    struct entry{
        char name [WORD];
        char *line[5] ;
        struct entry *left;; 
        struct entry *right;; 
    };
    
    struct head{
        struct entry *root;
    };
    
    int promptFile( char scriptFile[] );
    int buildTree ( struct head *soil, FILE *fp );
    int fileOpen ( char fileName[], FILE **fpr );
    FILE *skipEntry( FILE ***fp );
    void upperAll ( char word[] );
    FILE *leafAdd ( char name [ ], FILE **fp, struct head *soil );
    FILE *addLoop( struct entry **current, FILE ***fp, char line[]);
    void displayTree ( struct entry *current );
    
    /////////////////////////////////////////////////////////////////////
    // Program      : Lab05
    // File         : main.c
    // Function     : main
    // Description  : This program will hold company names and details
    //              :  within a binary tree.
    // Variable Ref : soil - holds root of tree
    //              : fpr - the file to be read in
    //              : fileName - name of file to use
    //              : argc - number of arguments read in
    //              : argv - arguments read in
    /////////////////////////////////////////////////////////////////////
    
    # include "lab05.h"
    
    int main( int argc, char *argv[] ){
    
        struct head *soil;
        struct entry *current;
        char fileName [WORD];
        FILE *fpr;
    
      // If file name supplied in command line, build from that file, else
      //  build from defult file
    
        soil -> root = NULL;
    
    /* REST OF CODE */

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Well before you do soil->root = NULL, you first need to do something like

    soil = malloc( sizeof *soil );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    19
    Ah I see. >< Gah, when did I take that out?!

    Thanks

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    and dont cast the malloc.

    Code:
    soil -> root = (struct entry *) malloc (sizeof(struct entry));
    
    soil->root = malloc(sizeof(struct entry));
    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A simple file I/O problem
    By eecoder in forum C Programming
    Replies: 10
    Last Post: 10-16-2010, 11:00 PM
  2. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 01:45 AM
  3. Problem in very simple code
    By richdb in forum C Programming
    Replies: 22
    Last Post: 01-14-2006, 08:10 PM
  4. Simple Initialization Problem
    By PsyK in forum C++ Programming
    Replies: 7
    Last Post: 04-30-2004, 07:37 PM
  5. Simple OO Problem
    By bstempi in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2004, 05:33 PM

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