Thread: Seg fault error.. no idea why???

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    23

    Unhappy Seg fault error.. no idea why???

    hey all,
    having trouble with a part of my program that puts a node into a binary tree. i havent put the whole code here b/c that would be to big.

    im using gcc on unix.. After using gdb it tells me that there is an error on the line ive marked in the strcmp() function.

    ive put in a fprintf statement to see if the current word is null.. but its not. and the "newWord" variable is definatly not null..
    (its a copy of the string value of the node im adding.. it could be replaced with "tempWord->string")

    it works for about the first 200 nodes then core dumps for some unknown reason.

    nobody i know seems to have any idea.. anyone got any ideas?
    by the way.. how do u do that code thing again?
    ------------------------------------------------------------------------------

    if(headOfTree->firstWord == NULL)
    {
    headOfTree->firstWord = tempWord;

    } else {

    currentWord = headOfTree->firstWord;

    while(loop && currentWord != NULL)
    {
    fprintf(stderr,"%p\n", currentWord);

    if(strcmp(currentWord->string, newWord) < 0)/*error*/
    {

    if(currentWord->childTwo == NULL)
    {
    loop = FALSE;
    currentWord->childTwo = tempWord;

    } else
    currentWord = currentWord->childTwo;

    } else if(strcmp(currentWord->string, newWord) > 0)
    {

    if(currentWord->childOne == NULL)
    {
    loop = FALSE;
    currentWord->childOne = tempWord;

    } else
    currentWord = currentWord->childOne;

    } else {
    loop = FALSE;

    }
    }
    }
    --------------------------------------------------------------------------------
    ActionMan

    "THE DAY IS MYNE!!!!
    I'll take famouse titties for $400"
    -Sean Connery, Saturday Night Live

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > ive put in a fprintf statement to see if the current word is null.. but its not
    But that by itself doesn't mean that you can dereference it. Look carefully at the value of the pointer - does it for instance look like it could contain ASCII text (some/all the hex digits in the range 0x20 to 0x7f). If this is the case, then a previous strcpy has gone astray.

    > nobody i know seems to have any idea.. anyone got any ideas?
    Posting the tree insert function and the structure declaration for your tree nodes would help.

    > by the way.. how do u do that code thing again?
    http://www.cprogramming.com/cboard/m...bbcode#buttons
    &#91;code]
    // paste your code here
    &#91;\code]

    Though the "Attach file: Maximum size: 102400 bytes" option would be advisable if there is a lot.

    > it works for about the first 200 nodes then core dumps for some unknown reason.
    Such is the nature of these problems.
    Does your malloc provide any debug? It might give you earlier warning of memory overwrites.
    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 2001
    Posts
    23

    thanks

    hey,
    thanks for your advice/help..
    I worked out that I hadn't initialised the childOne and childTwo
    pointers to NULL.. when I did that everything came together.

    thanks again,
    ActionMan

    "THE DAY IS MYNE!!!!
    I'll take famouse titties for $400"
    -Sean Connery, Saturday Night Live

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unknown seg fault after malloc
    By seaking1 in forum C Programming
    Replies: 4
    Last Post: 02-25-2009, 07:51 PM
  2. Getting a seg fault
    By ammochck21 in forum C Programming
    Replies: 11
    Last Post: 01-23-2009, 05:27 AM
  3. next_permutation seg fault
    By zxcv in forum C++ Programming
    Replies: 9
    Last Post: 12-14-2008, 07:40 AM
  4. Found a seg fault, but can't explain why i get it
    By misplaced in forum C++ Programming
    Replies: 11
    Last Post: 08-29-2005, 02:58 AM
  5. Simple seg Fault Problem
    By ccoder01 in forum C Programming
    Replies: 5
    Last Post: 05-02-2004, 10:28 PM