+ 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
7 Antworten
+ 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.
+ 5
you do know what happens when you write beyond the end of an array, right?
+ 5
Here is an example of overwriting an array.
https://code.sololearn.com/cqImm6YcHWd4
+ 5
Of course, set can be removed and top made global. However, that isn't the only problem with the code.
+ 1
John Wells sir, please see this code also.This is almost similar to the previous one.
+ 1
Ok sir.In this code can I remove set() function and make top as global variable.
https://code.sololearn.com/cbxCpcmvi7TE/?ref=app
0
Sir,can you please elaborate.