Segmentation fault

This is a discussion on Segmentation fault within the C Programming forums, part of the General Programming Boards category; Hi all, I've got a segmentation fault occurring that I don't understand, and I'd appreciate your help. First, the relevant ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    40

    Segmentation fault

    Hi all,

    I've got a segmentation fault occurring that I don't understand, and I'd appreciate your help. First, the relevant structure

    Code:
    typedef struct node
    {
    	//sequence for this node
    	amino_acid_list *sequence;
    
    	// not currently used- in case sequence length changes
    	int sequence_length;
    	
    	// distance (in amino acid substitutions) 
    	int mutation_distance_from_parent;
    	int mutation_distance_to_left;
    	int mutation_distance_to_right;
    
    	// distance (absolute? not yet used)
    	int distance_from_parent;
    	int distance_to_left;
    	int distance_to_right;
    	
    	// depth of this node
    	int depth;
    	
    	// debugging
    	long int data;
    	
    	// pointers
    	struct node *parent_ptr;
    	struct node *left_ptr;
    	struct node *right_ptr;
    } node
    Here is the function definition
    Code:
    node *build_tree_with_constraints(node *n, node* nprev, int depth, int maxdepth, amino_acid_list *parent_sequence, int sequence_length, float ........r, float **tpm, int dist, float random_param_one, float random_param_two, int burnin, incompatability_matrix **icm, int num_amino_acids, int type, int *num_mutations);
    And here is the offending piece of code within the function build_tree_with_constraints
    Code:
    			n->left_ptr= build_tree_with_constraints(n->left_ptr, n, n->depth+1, maxdepth, n->sequence, sequence_length, ssr, tpm, dist, random_param_one, random_param_two, burnin, icm, num_amino_acids, type, num_mutations);
    
                            // make sure that we get the correct output here
    			printf("LEFT- number of mutations is %d\n", *num_mutations);
    
    			// set the distance to the left offspring node
    			n->mutation_distance_to_left= *num_mutations;
    			printf("one\n");
    			n->left_ptr->mutation_distance_from_parent= *num_mutations;
    			printf("two\n");
    Here is the output

    Code:
    number of mutations is 48
    LEFT- number of mutations is 48
    uno
    Segmentation fault
    I don't understand why
    Code:
    n->mutation_distance_to_left= *num_mutations;
    works, but
    Code:
    n->left_ptr->mutation_distance_from_parent= *num_mutations;
    Doesn't. I've also tried

    Code:
    (n->left_ptr)->mutation_distance_from_parent= *num_mutations;
    But that doesn't help either. So obviously I'm missing something fairly basic, but I don't know what it is.

    Thanks a lot,
    Brad

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,655
    Run the code in a debugger. When the segfault occurs the debugger will stop. Investigate the state of the program at the point of the failure.

  3. #3
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115
    Try this:
    RMS's gdb Tutorial

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmentation fault... first time with unix...
    By theMethod in forum C Programming
    Replies: 16
    Last Post: 09-30-2008, 03:01 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 06:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 01:53 PM

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