Thread: Help.. segfault error

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    58

    Help.. segfault error

    I didnt get much help last time I posted this...
    please help..

    Im having trouble finding what my problem is..

    Im writing a binary tree library and when I run this test program: (cannot be changed)
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "bintree.h"
    
    void print_name(char *key, void *value){
       printf("%s\n", key);
    }
    
    main(int argc, char *argv[]) {
      if (argc <= 1) {
        fprintf(stderr, "usage: bst_test name1 name2 ... namek\n");
        exit(1);
      }
      void *tree = create_tree();
      int i;
    
      // test inserts
      for (i = 1; i < argc; i++) {
         tree_insert(strdup(argv[i]), (void *)1, tree);
      }
    
      // test print tree
      print_tree(tree, print_name);
    
      // test finds
      for (i = 0; i < argc; i++) {
        if (tree_find(argv[i], tree) == 0)
          printf("%s not in data set\n", argv[i]);
      }
    }
    I get these errors:

    Code:
    (gdb) bt
    #0  0xb7edfd2a in strcmp () from /lib/tls/i686/cmov/libc.so.6
    #1  0x0804853e in tree_insert (key=0x804a030 "name", value=0x1,
        tree=0x804a008) at bintree.c:33
    #2  0x0804878d in main (argc=3, argv=0xbfe13134) at test.c:22

    Here is the library im writing:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    
    typedef struct _node{
        struct _node *children;
        struct _node *next;
        char *name;
        int space;
        int x; //x coordinate
        int y; //y coordinate
    } NODE;
    
    typedef struct _bnode{
        char *name;
        struct _node family_node;
        struct _bnode *left;
        struct _bnode *right;
    } BNODE;
    
    BNODE *tree_insert(char *key, void *value, void *tree){
        BNODE *root = (BNODE *)tree;
        if(root == 0){
            root = (BNODE *)malloc(sizeof(BNODE));
            if(root != NULL)
            {
                root->name = strdup(key);
                root->left = NULL;
                root->right = NULL;
            }
        }else{
            if(strcmp(key, root->name) < 0){   <--- line 33
                root->left = tree_insert(key, value, root->left);
            }else if(strcmp(key, root->name) > 0){
                root->right = tree_insert(key, value, root->right);
            }else if(strcmp(key, root->name) == 0){
    	    root->name = strdup(key);
            }
        }
    
        return root;
    }
    
    
    BNODE *tree_find(char *key, void *tree){
        BNODE *root = (BNODE *)tree;
        if(root == 0){
            return 0;
        }
        if(strcmp(key, root->name) < 0){
            return tree_find(key, root->left);
        }else if(strcmp(key, root->name) > 0){
            return tree_find(key, root->right);
        }else{
            return root;
        }
    }
    
    
    void *create_tree(){
        BNODE *new_tree = (BNODE *)malloc(sizeof(BNODE));
        new_tree->left = NULL;
        new_tree->right = NULL;
        return new_tree;
    
    }
    
    void print_tree(void *tree, void(*print_fct)(char *key, void *value)){
        BNODE *current = (BNODE *)tree;
    
        if(current == NULL || print_fct == NULL){
            return;
        }else{
            print_tree(current->left, print_fct);
    
            print_fct(current->name, (void *)current->family_node.space);
    
            print_tree(current->right, print_fct);
        }
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Does your create_tree() initialise all the pointers, and all the members of a BNODE?
    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.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    58
    Quote Originally Posted by Salem View Post
    Does your create_tree() initialise all the pointers, and all the members of a BNODE?
    I think so.. well i want it to.. is it not?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    There are four members; you set two. What do you think?

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    58
    Quote Originally Posted by tabstop View Post
    There are four members; you set two. What do you think?
    Oh ya thats all I want.. I set name in my insert function..

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    58
    I just saw that last post by itcbitc.. lol.. I swear i didnt see it before..

    that fixed it..

    I get it now lol

  8. #8
    Why bbebfe is not bbebfe? bbebfe's Avatar
    Join Date
    Nov 2008
    Location
    Earth
    Posts
    27
    You have not set any value to root->name before using it within strcmp in your library function tree_insert, which is invoked in main.c after create_tree, It results an access violation error.

    Don't use strdup to duplicate string since it has been announced as obsoleted (It allocates memory inside by using malloc, and you have never free it).

  9. #9
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Root cause of the problem is that you are allocating the root node in two places ie create_tree() and in this piece of code from tree_insert().
    Code:
    BNODE *tree_insert(char *key, void *value, void *tree){
        BNODE *root = (BNODE *)tree;
        if(root == 0){
            root = (BNODE *)malloc(sizeof(BNODE));
            if(root != NULL)
            {
                root->name = strdup(key);
                root->left = NULL;
                root->right = NULL;
            }
    You need either one or the other and to be consistent keep the one from tree_insert(), after all adding the root node is the same as adding any another node.

  10. #10
    Registered User
    Join Date
    Sep 2008
    Posts
    58
    Quote Originally Posted by itCbitC View Post
    Root cause of the problem is that you are allocating the root node in two places ie create_tree() and in this piece of code from tree_insert().
    Code:
    BNODE *tree_insert(char *key, void *value, void *tree){
        BNODE *root = (BNODE *)tree;
        if(root == 0){
            root = (BNODE *)malloc(sizeof(BNODE));
            if(root != NULL)
            {
                root->name = strdup(key);
                root->left = NULL;
                root->right = NULL;
            }
    You need either one or the other and to be consistent keep the one from tree_insert(), after all adding the root node is the same as adding any another node.

    I fixed it thankyou ..

    Im not getting the segfault anymore but Im getting this output
    i wanted it to print in alphabetical order.. so thats right at least..

    unix> ./program hey there billy joe joe
    <-- Im getting this extra space?
    billy
    hey
    joe
    there
    ./test_option1 not in data set <-- and its taking on the first argument for some reason..
    unix>

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
      for (i = 0; i < argc; i++) {
        if (tree_find(argv[i], tree) == 0)
          printf("&#37;s not in data set\n", argv[i]);
      }
    Is the RED bit of any signifcance to the latter problem?

    As to the first problem, I think you are "lucky":
    Code:
    void *create_tree(){
        BNODE *new_tree = (BNODE *)malloc(sizeof(BNODE));
        new_tree->left = NULL;
        new_tree->right = NULL;
        return new_tree;
    
    }
    creates a new node at the start of the tree. It doesn't have any value in name - and your "luck" is that it actually contains a valid pointer to a string, rather than some completely random crap.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    see the code section under // test finds and you will know why its printing the program name ie argv[0].
    Last edited by itCbitC; 11-17-2008 at 01:37 PM.

  13. #13
    Registered User
    Join Date
    Sep 2008
    Posts
    58
    Quote Originally Posted by itCbitC View Post
    see the code section under // test finds and you will know why its printing the program name ie argv[0]...and post the source of print_fct().
    is there anyway to change my bintree library instead of that code? I cant edit that ..

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by scarlet00014 View Post
    is there anyway to change my bintree library instead of that code? I cant edit that ..
    Yes, I suppose you can ignore the first search :-)

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM