Peter87 is right about the first version, but the second version ( I ignored the first version, as I came in later anyway) uses the &i, which is just as bad ;)
Your stack appears to store void *. Ok, but you're pushing an integer. Seems odd, but it can be done. The problem, as I see it, you don't have an integer to push that will survive.
If you use a debugger, you'll see that every push (of this latter version) will push the same address, that of the temporary i.
If you loop through the stack AFTER the loop (another loop just to read through the stack), it will be problematic. This is because the address being pushed is from the stack. It's not a valid CHOICE.
It may happen to SUGGEST it works. Yet, it won't. Watch as you loop through just reading the stack and look at the address at each pass of the loop....it's the same address. Ask yourself, how can the same address produce a different number when dereferenced?
It can only do that because you're actually reading the stack....the loop variable, i, in the case of the latter algorithm posted.
If you must push addresses, you have to have an address that makes sense...a copy of i.
Unfortunately, that would suggest a push of:
1 2 3 4
|
iter_point = new int( i );
push_stack( iter_point );
|
But that says the stack now owns that memory....it would have to DELETE that int as it pops.
Not a good plan.
For pushing ints, you really need a stack that takes ints, not integer pointers.
For that matter, even if you intended to push pointers to objects on the stack, those objects STILL must be owned - that is, something has to be responsible for deleting those objects later...and that should NOT be the stack itself.