Ask your own question, for FREE!
Computer Science 4 Online
OpenStudy (anonymous):

Any reason whenever i try to pop the stack and print it, the program crashes

OpenStudy (anonymous):

class stack { private: stack* link; int data; public : stack* push (stack *top, int x){ stack* temp = new stack; temp->data = x; temp->link = top; top = temp; return top; } void pop(stack *top){ stack*save; if (top == NULL){ return; } else{ save = top; top = top->link; delete save; } } void print (stack *top){ stack* temp = top; while(temp != NULL){ cout<<temp->data <<" "; temp = temp->link; }cout <<endl; } };

OpenStudy (anonymous):

@e.mccormick

OpenStudy (e.mccormick):

Does it give an error?

OpenStudy (anonymous):

it gives strange numbers on the compiler then crashes. it does the push just fine, but the pop, not so well

OpenStudy (e.mccormick):

Are you sure it is nul terminated? If not, you would go out of range.

OpenStudy (anonymous):

it should be null terminated if i didn't make any mistakes

OpenStudy (e.mccormick):

Well, "strange numbers" generally means some unallocated memory or some other address that can cause garbage to pop up. Following this with a crash is a second indication of out of range.

OpenStudy (anonymous):

i see.. i can't figure out where the problem might be

OpenStudy (anonymous):

Do you need a return statement in a void function?

OpenStudy (anonymous):

it is not returning any values to the void function

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!