Program Doesnt crash in debug mode, crashes normally

This is a discussion on Program Doesnt crash in debug mode, crashes normally within the C Programming forums, part of the General Programming Boards category; Hello everyone. I'm writing a program that reads in a text file with words, 1 on each line, and reads ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    8

    Program Doesnt crash in debug mode, crashes normally

    Hello everyone. I'm writing a program that reads in a text file with words, 1 on each line, and reads it into a binary search tree.

    I am using Dev-C++. I'm not sure why i'm getting an error because whenever i run it in debug mode, it runs completely smoothly, but when I run it in normal mode my program crashes.

    The question is, why am i getting crashes? And why would it work completely fine in debug mode!?

    Without further ado, here's the code and the test file.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct node
    {
       char* data;
       struct node* left;
       struct node* right; 
    };
    
    struct node* insert(struct node* root, char* value);
    
    struct node* insert(struct node* root, char* value)
    {
        struct node* newNode;
        if(root == NULL)
        {
            newNode = malloc(sizeof(struct node));
            newNode->data = malloc(sizeof(value));
            strcpy(newNode->data,value);
            newNode->left = NULL;
            newNode->right = NULL;
            
            return newNode;
        } 
        
        if(strcmp(root->data,value) == 0)
            return root;
    
        if(strcmp(value,root->data) < 0)
        {
            root->left = insert(root->left, value);
            return root;
        }
        else
        {
            root->right = insert(root->right, value);
            return root;
        }
    }
    
    
    int main(void)
    {
        char filename[256];
        char tempstr[100];
            
        printf("Whats the dictionary file you're using?\n");
        scanf("%s",filename);
        FILE *dictionaryfp;
        dictionaryfp = fopen(filename,"r");
        struct node* root = NULL;
        
        while (!feof(dictionaryfp))
        {
              fgets (tempstr , 100 , dictionaryfp);
              root = insert(root,tempstr);
        }
        
            
        printf("What do you want to search for?");
        char tempstring[256];
        scanf("%s",tempstring);
        printf("found?: %d",find(root,tempstring));
        
        system("PAUSE");
        
    }
    Text file:

    Code:
    nemy
    gossamer
    foreign
    Shearer
    flounder
    wordy
    pellet
    Sagittarius
    conformation
    husky
    intersect
    Kaplan
    birthright
    ordinary
    knight
    cytoplasm
    detestation
    aureomycin
    omen
    pasteboard
    Abidjan
    aromatic
    august
    Katowice
    executive
    Jessie
    digress
    sequestration
    satiric
    abate
    Semite
    rigid
    anthropomorphism
    ongoing

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your program is flawed, but debug versions include more info, and different settings. Don't struggle with the why. It's not uncommon, and the program is indeed in need of fixing.

    One thing, right off, is your int main() doesn't include a return 0 statement, which it should.

    What errors or warnings do you see when you're compiling or running it in release mode? Are your warning levels turned all the way up?

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    8
    I get 0 errors, 0 warnings when I compile. Good call on the return statement.

    This is why i hate dev-c++, but i have to use it for my class. It has to compile with Dev C++. Do you see any other problems?

    The program specifically cuts out after the third loop .

    Is the read-in-loop syntactically correct?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    feof is not a robust way to check for eof, but that's not your trouble, because you're not getting that far.

    Let me run it through the old compiler one time, and see what happens.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Where does the program crash? It doesnt just "crash", it "crashes" at a specific point--use printf statements to manually debug the code to find out the last print statement executed before the crash, so somewhere up to there is where (one of) the bug(s) are.

    I imagine you arent seeing various errors/warnings due to compiler "fixing" it in the background. You need to:
    - "free" anything you "malloc", currently it has memory leaks
    - Check return value of "malloc" (i.e. not NULL)
    - Check the return value of "fopen", as your program assumes the file opens fine, when if it fails it returns NULL
    - Your using some function called "find" that doesnt exist (compiler extension?)
    - Check return value of "fgets", if it returns EOF or NULL your using it anyway
    - Unlikely any issue for what your input is, but when "scanf"ing a string, you should make sure it doesnt buffer overflow--i.e. read up to the max characters that the destination buffer can store
    - Use "strncpy" instead of "strcpy" to copy up to a certain number of characters, preventing buffer overflows

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The size of value is questionable to me. I'm no expert with mallocing for struct members, but the sizeof(value) is always 2 bytes.

    That doesn't seem like it's enough memory for a name like "gossamer", etc.

    I just commented out the code lines that referred to find().

  7. #7
    Registered User
    Join Date
    Aug 2009
    Posts
    8
    strncpy solved it,

    Thank you for your wisdom, good sir

  8. #8
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Another thing thats likely causing problems is
    Code:
    newNode->data = malloc(sizeof(value));
    You should do this instead
    Code:
    newNode->data = malloc(strlen(value) + 1);
    As "sizeof" is getting the size of the char*, not the length of the string that it points to. Instead, use "strlen" to allocate the length of that string, plus one for the terminating null character (which strlen doesnt include).

  9. #9
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Quote Originally Posted by Adak View Post
    The size of value is questionable to me. I'm no expert with mallocing for struct members, but the sizeof(value) is always 2 bytes.

    That doesn't seem like it's enough memory for a name like "gossamer", etc.

    I just commented out the code lines that referred to find().

    Hey Adak Bro i if you are talking about
    Code:
    newNode->data = malloc(sizeof(value));
    then on 32 Bit linux OS and windows visual studio it will be 4 bytes as pointers are treated as ints and an int is always have 4 bytes of memory

    And about structure sizeof as it is having 3 pointers then it will be 3 * 4 = 12

    Mr. Adak are you talking about Turbo C i think on that int is having 2 bytes of mem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debug win32 C program on linux
    By wwwnuwan in forum C Programming
    Replies: 1
    Last Post: 08-31-2009, 07:27 AM
  2. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 02:42 AM
  3. Can not debug a crash
    By hannibar in forum Windows Programming
    Replies: 2
    Last Post: 06-30-2007, 11:02 AM
  4. DEV-C++ made program crashes when run from outside the ide
    By rainmanddw in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2006, 10:27 PM
  5. counting program worked, but test file CRASHES IT.. WHY?
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 05-19-2002, 03:29 AM

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