Thread: I think i found a bug

  1. #1
    Registered User
    Join Date
    Sep 2015
    Posts
    19

    I think i found a bug

    So i wrote a very simple program which includes a linked list,
    the nodes within the list are dynamically allocated using malloc(), the program had 2 functions, add a new node to the list and print the nodes in the list.
    Every time i added more than 2 nodes to the list(including the start node) and used the print function the program crashed.
    With some if and print statements i found that the third (or which ever was last) node's *next was not NULL even though obviously it was not pointing at anything since it was the last node in the list.
    The program crashed because there is a while loop that depended on that pointer being NULL to stop.

    Anyway, changing the size of the array to 12 fixed the problem.
    My question is why? I included the entire program if your interested
    but it's kinda messy.

    Code:
    typedef struct example{
        
        char array[52];
        struct example *next;
    
    }typeExample;

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    //http://cboard.cprogramming.com/c-programming/167968-please-need-help-linked-list.html#post1239483
    typedef struct LSexample{
        
        char name[12]; // name was too big that was the problem
        float decimals;
        struct LSexample *forward;
    }typeLS;
    
    typeLS *returnPointerToAllocatedMemory(typeLS *MainPointers){
        char UserInput5[16];
        fgets(UserInput5, 15,stdin);
        typeLS *PointertoAllocatedMemory = malloc(sizeof(typeLS));
    
        sscanf(UserInput5,"%s %f",PointertoAllocatedMemory->name,&PointertoAllocatedMemory->decimals );
    
        printf("Name:%s float:%f\n",PointertoAllocatedMemory->name,PointertoAllocatedMemory->decimals);
        if(MainPointers != NULL){//if there is somethign behind then set
                                    //    whats behind pointer to point at the newly created structure 
            MainPointers->forward = PointertoAllocatedMemory;//mainpointers is the previous structure
                                                            // if there isn't one it will be NULL
        }
        return PointertoAllocatedMemory;
    }
    void cleanUp(typeLS *FirstStructure1){
        
        
        typeLS *freeThisAllocatedMemory = NULL;
        typeLS *HoldPointerForward = NULL;
        freeThisAllocatedMemory = FirstStructure1;
        
        while(freeThisAllocatedMemory != NULL){// if ? REALLY? LOOOOOOOL
            printf("Free name:%s float %f\n",freeThisAllocatedMemory->name,freeThisAllocatedMemory->decimals);
            HoldPointerForward = freeThisAllocatedMemory->forward; //if ->forward was bellow
            free(freeThisAllocatedMemory); //remember its cleaning the pointer not the struct
            freeThisAllocatedMemory = HoldPointerForward; //i mean here, then the cleaned pointer woudl get dirty again
    
        }
    }
    void printfunc(typeLS *FSpointer){
        
        typeLS *holder3 = FSpointer;
        
        while(holder3 != NULL){
    
            printf("name:%s float:%f\n",holder3->name,holder3->decimals);
            
            holder3 = holder3->forward;
            if(holder3 != NULL){
    
            }
        }
    
        
    }
    
    
    int main(){
        typeLS *Firststructure = NULL;//this pointer will point at the first structure in the list 
                                        
        typeLS *holderr = NULL; //this pointer will point at multiple things which is the trick of this logic
        char UserInput[16];
        char translation[16];
        while(fgets(UserInput, 15,stdin)){
            sscanf(UserInput,"%s",translation);
            if(strncmp(translation, "add",3)==0){
    
                if(Firststructure == NULL){
                    Firststructure = returnPointerToAllocatedMemory(NULL);
                    holderr = Firststructure;
                }else{
                    holderr = returnPointerToAllocatedMemory(holderr);
                }
            }else if(strncmp(translation, "print",5)==0){
                printf("Printing...\n");
                printfunc(Firststructure);
            }else if(strncmp(translation, "quit",4)==0){
                printf("Breaking...\n");
                break;
            }
        }
        cleanUp(Firststructure);
        return 0;
    }

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    @Moderators: Please Need help with a linked List

    I think i found a bug
    O_o

    No. You are only aware that a bug exists.

    The bug is in your code.

    You still need to find the bug.

    Your post, the parts which aren't code, even explain the occurrence of the bug in terms of the assumptions your code makes.

    You just need to change the code to account for the assumption you have explained.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  3. #3
    Registered User
    Join Date
    Sep 2015
    Posts
    19
    can you help me find it, i been trying to for the last 5 hours this is really bugging me (no pun intended)

  4. #4
    Registered User
    Join Date
    Sep 2015
    Posts
    19
    I think the large array maybe overwriting the next pointer in the memory from NULL so it can't be a problem with my code

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    it can't be a problem with my code
    O_o

    The call of every crappy programmer with which I've had the displeasure of working.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by Anonymouss View Post
    it can't be a problem with my code
    Yes, it can be.

    In fact, you already identified the problem - the last node in the list has a non-null forward pointer.

    Question: Where in your code are you setting that last node's forward pointer to null?
    Answer: You aren't.
    Hint: You should be.
    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.

  7. #7
    Registered User
    Join Date
    Sep 2015
    Posts
    19
    yes pink horse I'm crappy programmer I just started learning not so long ago,i say it can't be the problem with my code because i couldn't find the flaw in my logic not because i have a big ego
    Thanks cat i was thinking the same thing i just follow a stupid tutorial that got me thinking that can't be it

  8. #8
    Registered User
    Join Date
    Sep 2015
    Posts
    19
    and thank you pink horse too

  9. #9
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    i say it can't be the problem with my code because i couldn't find the flaw in my logic
    O_o

    I know. Where do you think my problem with the attitude originates?

    You asked for help understand the source of an error.

    Great. I'm not faulting you for needing help finding a silly mistake. We all need fresh eyes on the occasion.

    I find that people who repeat on "not my code" are always the worst members of any team.

    You should consider that, regardless of your reasons or excuses, your code was the source of the problem.

    I assume you've now fixed the error because Cat was gracious enough to have solved the problem for you.

    Do you see the issue?

    A person who isn't responsible for the code had to solve the problem in order for the problem to be fixed.

    The fact is that you will be at fault for the overwhelming majority of bugs you will ever encounter in code you have written.

    I stated that the "not my code" person makes a crappy programmer.

    I know you are new, and I'm telling you don't be the "not my code" person.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  10. #10
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by Anonymouss View Post
    i just follow a stupid tutorial that got me thinking that can't be it
    Never think "that can't be it" because it usually is

    Anyhow, the two main ways I debug code:

    1. I read code and try to identify the problem. Start from what you know to be true and work backwards. I usually do this first because chances are, unless I'm debugging something I am in the process of writing, I need to (re)familiarize myself with the code, and thinking through things in my head prepares me for deeper debugging if necessary.

    For example, in this situation, I would think:
    a) Why is there a crash? Because it's moving past the end of the linked list.
    b) Why is it moving past the end of the linked list? Because the loop didn't terminate.
    c) Why is the termination condition of the loop not met? The last node's "forward" pointer must not have been null.
    d) How could the last node's forward pointer not be null? Either the code is somehow (intentionally or not) setting this to non-null, or it has a random value because it wasn't initialized.

    I never really entertain the notion, especially at first, that there could be anything wrong with the compiler, operating system, etc. Chances are very, very, very good the problem is in the code in front of me.

    2. If reading code doesn't illuminate the problem, and I at least know how to reproduce the issue on demand, I use the debugger. I set breakpoints, watch points, single-step through important code, etc. This is one of the most reliable ways to track down any issue, as long as you have a test case that can reproduce the problem. Sometimes you don't know how to trigger the problem, and then it's usually just gathering as much data as you can and thinking it through.

    I tend to use the debugger particularly heavily on code I'm in the process of writing, because I already have the right code branch checked out and open in my IDE, and it's fresh in mind, which on the plus side means I don't need to familiarize myself with the code, and on the minus side means I find it harder to spot bugs because I sometimes tend to read the code as I intended it to work rather than what is actually written in front of me.
    Last edited by Cat; 09-03-2015 at 08:32 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.

  11. #11
    Registered User
    Join Date
    Sep 2015
    Posts
    19
    yeah pink horse your right i should have solved it on my own, thank you guys

  12. #12
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by Anonymouss View Post
    yeah pink horse your right i should have solved it on my own, thank you guys
    I don't think that was what he was saying. He's saying you approached this with an attitude of "I can't see a problem in my code, so there must be a bug somewhere else", while an experienced coder would approach this from an attitude of "I can't see a problem in my code, so there must be a bug in my code that I'm not seeing".
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dll not found
    By marclurr in forum C Programming
    Replies: 8
    Last Post: 04-04-2010, 12:33 PM
  2. Look what I found!
    By face_master in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-14-2002, 05:34 AM
  3. EOF not found
    By bif22 in forum C++ Programming
    Replies: 12
    Last Post: 09-29-2002, 04:35 PM
  4. -lqt not found?
    By f0d in forum Linux Programming
    Replies: 2
    Last Post: 06-03-2002, 10:17 AM

Tags for this Thread