Thread: Program runs fine but crashes when i press enter to exit the exe. window

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    9

    Program runs fine but crashes when i press enter to exit the exe. window

    Hi everyone,

    So I'm working on a stacks project and i every time i run the following function, the program runs fine until the end but it only crashes once i press enter to exit out of the command screen. I'm thinking it may be some sort of memory access error.. but i can't figure out exactly where it goes wrong. S->data is a pointer to an array of stackItems and itemPtr is just a pointer to a stackItem.

    Please help! and thank you!!

    Code:
    int stack_peek( stackItem *itemPtr, Stack S )
    /*This function takes as arguments a Stack S and a pointer to a stackItem 
    itemPtr. If S is not empty, the top item is copied into the space pointed to by
     itemPtr. It returns 1 of S is not empty, 0 if it is empty, and -1 of any of 
     its arguments is NULL.*/
    {
         if (S){                              // check if S exists
                if (S->count==0) {            // check if S is empty
                                 return 0;    
                                 }
                else {                           // if Stack is not empty
                     *itemPtr = S->data[S->top]; // copy value of top item to itemPtr
                     return 1;  
                }
         }
         else {                 // if S is NULL, return -1
              return -1;
              }
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Probably you're feeding it an invalid pointer in itemPtr. (Possibly something weird is happening with S->top, but usually writes cause this sort of thing and reads don't.)

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    9
    Hey so i fixed the problem but i'm not really sure why it works now.
    you were right, it was something with itemPtr. in main when i had called the function stack_peek. I declared an itemPtr as: stackItem *itemPtr; but i didnt give an address or any value to it so i guess it wasnt dereferenced to anything and maybe that's why it crashed?

    because now i just made a stackItem variable and deferenced itemPtr to that variable first and then when i called the function stack_peek, nothing crashed.

    but i'm not really sure what the reasoning is behind it?..

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Because you declared a pointer variable, but didn't give it a value, that meant that it was pointing to something important (this is Murphy's Law). Then when you do *itemPtr = whatever, the value is stored where itemPtr points to. Since itemPtr pointed to something important, that something important got overwritten, which meant when you needed to do the something important (namely "close the window") it wasn't there anymore.

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    9
    ah.. i see, that makes a lot more sense now. thank you!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program runs fine, then crashes 5% of the time
    By mat1z in forum C++ Programming
    Replies: 5
    Last Post: 01-30-2011, 04:28 AM
  2. funcion runs 14 times, then program crashes
    By happyclown in forum C Programming
    Replies: 9
    Last Post: 03-03-2009, 11:58 PM
  3. Replies: 5
    Last Post: 08-05-2002, 07:14 PM
  4. Runs fine in IDE but crashes normally!
    By Hunter2 in forum Windows Programming
    Replies: 5
    Last Post: 05-07-2002, 03:47 PM
  5. Dos Window closes after I press enter
    By ProgrammingDlux in forum C++ Programming
    Replies: 4
    Last Post: 01-24-2002, 12:13 PM