Thread: No Stack Error

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    25

    No Stack Error

    This is the first time I have encountered a "no stack." error giving me a seg fault. Anyhow, I am scanning words into a binary search tree and it is giving me a seg fault. I cannot figure out which line. Here is the file (words.txt):
    4
    bravo
    alpha
    gamma
    delta

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    typedef struct node_t{
        char *word;
        struct node_t *left;
        struct node_t *right;
    }node_t;
    
    node_t *insert(char *currWord, node_t *head){
        node_t *curr_node = head, *new_node = malloc(sizeof(node_t));
        new_node->right = NULL;
        new_node->left = NULL;
        new_node->word = currWord;
        if (head = NULL)
            return new_node;
        while (1) {
            if (strcmp(currWord, curr_node->word) > 0) {
                if (curr_node->right == NULL) {
                    curr_node->right = new_node;
                    return head;
                }else
                    curr_node = curr_node->right;
            }else{
                if (curr_node->left == NULL) {
                    curr_node->left = new_node;
                    return head;
                }else
                    curr_node = curr_node->left;
            }
        }
    }
    
    int main(){
        FILE *ifp;
        ifp = fopen("words.txt", "r");
        int numWords, i;
        node_t *head = malloc(sizeof(node_t));
        fscanf(ifp, "%d", &numWords);
        for (i = 0; i < numWords; i++){
            char *currWord = malloc(sizeof(char));
            fscanf(ifp, "%s", currWord);
            head = insert(currWord, head);
        }
        
        fclose(ifp);
        return 0;
    }

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Seg fault means that you are trying to access memory which you don't access to.
    Check to see if fopen/malloc does not return NULL before using them.
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Two problems become immediately obvious:

    1. Your head element doesn't get a word assigned to it, so every time you do a strcmp, you're comparing an uninitialized pointer.
    Code:
    node_t *head = malloc(sizeof(node_t));
    2. Your currWord only is allocating one byte to store the new word read from the file - basically, you only buffer enough space for one character, and since you're using fscanf, you have no way of knowing if you've overrun your buffer or not.
    Code:
    char *currWord = malloc(sizeof(char));
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    25
    Alright, I changed the currWord line, that makes sense but the first part doesnt matter, I assign a word to it when i run
    Code:
     head = insert(currWord, head);
    if head == NULL

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by Clayton Cuteri View Post
    Alright, I changed the currWord line, that makes sense but the first part doesnt matter, I assign a word to it when i run
    Code:
     head = insert(currWord, head);
    if head == NULL
    Well, sort of, but only because you have another bug in the code (in your if statement, you're doing = NULL instead of == NULL). Think about how this code executes:

    Code:
    node_t *insert(char *currWord, node_t *head){
    
        node_t *curr_node = head, *new_node = malloc(sizeof(node_t));
        new_node->right = NULL;
        new_node->left = NULL;
        new_node->word = currWord;
    
    
        // At this point, new_node has a word in it, and curr_node 
        // points to head, which has all of its members undefined.
        
        // Head is not null; it points to the uninitialized block of memory returned by malloc() in main,
        //  so this if statement - assuming you fix the bug with = and == - will always be false
        if (head = NULL)
            return new_node;
        while (1) {
            // Now you get to this point, with currNode equal to
            // your head node.  curr_node->word is undefined, as are all
            // other members of curr_node
            if (strcmp(currWord, curr_node->word) > 0) {
    You never pass a null value of HEAD in. malloc() will not return a null pointer, it returns a new node with uninitialized values. You pass this uninitialized head in to the insert function.

    If you want it to work the way you think it does, you'd need this in main instead:

    Code:
    node_t *head = NULL;
    Then head WOULD be null the first time through the loop, and the node would be created and assigned back to head.
    Last edited by Cat; 04-02-2013 at 11:00 PM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  6. #6
    Registered User
    Join Date
    Jan 2013
    Posts
    25
    wow...okay I see what you are talking about. Thanks, now is head still dynamically declared? even though I set it equal to NULL after i malloced it?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 11-28-2011, 11:48 AM
  2. Dynamic Stack Error
    By tarheelfan_08 in forum C++ Programming
    Replies: 1
    Last Post: 04-28-2010, 12:16 AM
  3. stack smashing error
    By spank in forum Linux Programming
    Replies: 2
    Last Post: 05-24-2007, 02:07 PM
  4. Stack Program Error
    By DarkDot in forum C++ Programming
    Replies: 12
    Last Post: 03-30-2007, 04:32 PM
  5. Stack ERROR
    By swgh in forum C++ Programming
    Replies: 4
    Last Post: 02-08-2006, 01:56 AM