reseting a stack

Pages: 12
Feb 17, 2011 at 4:02am
How can I reset a stack to be empty?
Is it like this?
1
2
3
4
void reset()
{
count=0;
}


Feb 17, 2011 at 4:10am
Possibly. It depends how your stack is implemented.

What would indicate your stack as being empty?
Feb 17, 2011 at 4:29am
i would say -1

so i think i should do say this right?

1
2
3
4
void reset()
{
count= -1;
}
Feb 17, 2011 at 4:39am
Possibly. It depends on how your stack is implemented.

Hint: without seeing how your stack is implemented, it's impossible to know whether or not that's correct.
Last edited on Feb 17, 2011 at 4:39am
Feb 17, 2011 at 4:49am
I dont have a specific code with me we disscussed this in class today and i didnt get it . what i am saying is there is a int count;
Feb 17, 2011 at 4:54am
What is the difference between an empty stack with 0 elements and an empty stack with -1 elements?
Clearing a stack (or almost any other container structure) usually involves deleting all its elements and setting the element count to 0.
Feb 17, 2011 at 4:54am
With no specific code, we can't give you a specific answer. Sorry. It all depends on what count means for the stack.
Feb 17, 2011 at 4:59am
....
private:
int count;
..

i dont remember the rest of the code but this was the count part of the code
Feb 17, 2011 at 5:18am
That's not specific enough, as it doesn't show how count is used at all.
Feb 17, 2011 at 5:42am
can you give me an example?
Feb 17, 2011 at 5:57am
Ok. There are 2 types of memory in C++: automatic and dynamic. Automatic memory is declared normally and is destroyed when the current function exits. Its size is fixed (there is an exception but this is too advanced). Dynamic memory is created using the new operator or the malloc() function, and is destroyed using the delete operator or the free() function. Dynamic memory can change size. If your stack is stored in automatic memory, then indeed, setting the count back to the start will be equivalent to clearing the stack. If, however, your stack is implemented using dynamic memory, you need to free this memory.
Feb 17, 2011 at 5:57am
OP: "Does a red shirt match this outfit?"
Me: "Maybe. It depends what else is in the outfit."
OP: "What about a blue shirt?"
Me: "Maybe. What else is in the outfit?"
OP: "I don't know. I saw a picture of it in class. It had some kind of colors."

=P

We need to see the stack in order to give you a real answer.

However, if count is used logically, then having a count of 0 should reset the stack. Whether or not that's the only thing you have to do, or if that's even right depends on how the stack is set up.

So again -- there's no way for us to definitively answer this unless you can show us the stack.

EDIT:
side note regarding rocketboy's post: don't use malloc/free in C++. Use new[]/delete[] instead (or better yet, use a container class, but that's probably not allowed for this assignment if you're writing your own stack)
Last edited on Feb 17, 2011 at 5:59am
Feb 17, 2011 at 6:15am
A well-made analogy, my friend...I laughed.
Feb 17, 2011 at 6:21am
new[] and delete[] are evil because there's no renew[], completely removing one of the advantages of dynamic memory. (and causing unnecessary copying.)
Last edited on Feb 17, 2011 at 6:32am
Feb 17, 2011 at 6:48am
new[] and delete[] are evil because there's no renew[]


malloc/free are evil because:

1) They don't call ctors or dtors, which makes them unsuitable for use in templates, or with any non-trivial type (read: templates and non-trivial types are a huge portion of C++)
2) They require explicit casting, which makes them type unsafe
3) They require you to check return values (or risk a null pointer return)
4) They're more confusing/error prone:

1
2
3
int* foo = (int*)malloc(5 * sizeof(int));
//vs
int* foo = new int[5];


(and causing unnecessary copying.)


realloc copies most (all?) of the time anyway. Sure it doesn't have to in theory, but it does in practice.

Besides, intelligent management of dynamic memory avoids repeated reallocations. If you're mallocing/new[]ing only what you need and not anticipating future growth (where applicable), you're doing it wrong.

But these are all really reasons to use vector over new/malloc.
Last edited on Feb 17, 2011 at 6:51am
Feb 17, 2011 at 7:02am
1) Use placement new. (which is also stupid, why can't you just call the damn constructor?)
2) This is a fake problem.
3) Riiight, and new requires you to put a try catch around it. What are you going to do when to run out of memory? Except for simple things like printing an error, you might need memory to do it. And of course, with NULL you can just store the NULL and handle it later, whereas exceptions always need try catch if you don't want to go down to the bottom.
4) What about that code is more confusing or error prone?
Last edited on Feb 17, 2011 at 7:11am
Feb 17, 2011 at 10:30am

I thought I'd seen every language war there was, but insisting on malloc and chums over new and delete is a new one to me.
Feb 17, 2011 at 2:08pm
new[] and delete[] are evil because there's no renew[], completely removing one of the advantages of dynamic memory. (and causing unnecessary copying.)


This exact discussion was had in a previous topic a little while back and OP there lost. There is no debate, since the parenthetical statement applies to realloc() as well.

EDIT:
Here was the discussion I was remembering:
http://www.cplusplus.com/forum/beginner/34728/

And here's another where rocketboy wants a renew[]
http://www.cplusplus.com/forum/general/33528/


Last edited on Feb 17, 2011 at 2:13pm
Feb 17, 2011 at 2:25pm
closed account (z05DSL3A)
Disch wrote:
4) They're more confusing/error prone:
int* foo = (int*)malloc(5 * sizeof(int));
...
rocketboy9000 wrote:
4) What about that code is more confusing or error prone?

I have seen you post incorrect code will malloc() and sizeof() in this forum, I think it was the last time you complained about new/delete.
Feb 17, 2011 at 4:11pm
There is no debate, since the parenthetical statement applies to realloc() as well.

Wrong. Imagine I had a heap of 20 bytes, with 4-byte tags. (FR or US, plus a byte count shown; obvously it would actually be a little more complicated, but bear with me) Before allocating anything, it looks like this:
FR160000000000000000
Now I allocate two 6-byte arrays:
US06000000US06000000
Now I free the second:
US06000000FR06000000
Now, if I wanted to extend the first array to 16 bytes, a new[], copy, delete[] cycle would cause me to run out of memory, but a call to realloc could find that free block right there and give it to me:
US160000000000000000
That is why there should be a renew[].

EDIT: To test this, I made a program:
1
2
3
4
5
6
7
8
9
10
#include "stdlib.h"
#include "stdio.h"
int main(){
	char *a=malloc(200);
	char *b=malloc(200);
	free(b);
	b=realloc(a,400);
	printf("The answer is: %s\n",
              a==b?"a==b":"a!=b");
} 
The answer is: a==b


Last edited on Feb 17, 2011 at 4:22pm
Pages: 12