Reversing a stack | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Reversing a stack

I have made this code just to reverse the stack.This code works fine but the problem is that if I enter more values than it's size,it is still running. Size of stack is 10.It is accepting upto 12. https://code.sololearn.com/cfR5jgZ3GlhK/?ref=app

18th Aug 2018, 12:02 AM
harshit
harshit - avatar
7 Answers
+ 6
The stack holds your function parameters, local variables, and most importantly the information to return from the function to it's caller. If you go beyond the array, you are playing with other things. int a,b[1],c; b can only be legally accessed with b[0]. b[1] is the same memory as a. b[-1] is c. Modifying your call frame is fatal to your correct execution of the program. It is never save to access an array outside of it's declared bounds as you do not know what you might be distroying.
19th Aug 2018, 9:47 PM
John Wells
John Wells - avatar
+ 5
you do know what happens when you write beyond the end of an array, right?
18th Aug 2018, 3:53 AM
John Wells
John Wells - avatar
+ 5
Here is an example of overwriting an array. https://code.sololearn.com/cqImm6YcHWd4
19th Aug 2018, 9:51 PM
John Wells
John Wells - avatar
+ 5
Of course, set can be removed and top made global. However, that isn't the only problem with the code.
19th Aug 2018, 9:55 PM
John Wells
John Wells - avatar
+ 1
John Wells sir, please see this code also.This is almost similar to the previous one.
18th Aug 2018, 3:40 AM
harshit
harshit - avatar
+ 1
Ok sir.In this code can I remove set() function and make top as global variable. https://code.sololearn.com/cbxCpcmvi7TE/?ref=app
19th Aug 2018, 9:50 PM
harshit
harshit - avatar
0
Sir,can you please elaborate.
19th Aug 2018, 9:22 PM
harshit
harshit - avatar