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.
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.
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)
new[] and delete[] are evil because there's no renew[], completely removing one of the advantages of dynamic memory. (and causing unnecessary copying.)
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:
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.
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?
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.
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");
}