I'm currently trying to implement my own version of a stack, which will be an array that contains structs. Here is the code:
struct some_struct{
std::string name;
};
ExStack::ExStack(){
cout << "Please enter a capacity for the stack: ";
cin >> capacity;
stack = new some_struct[capacity];
size = 0;
}
void ExStack::create_new_array(some_struct item){
size ++;
if (size == capacity){
some_struct* new_stack = new some_struct[2 * capacity];
for (int i = 0; i < size; i++){
cout << stack[i];
new_stack[i] = stack[i];
}
delete[] stack;
stack = new_stack;
capacity *= 2;
delete[] new_stack;
}
For debugging purposes, I'm currently entering 10
for the capacity of the ExStack, and then calling create_new_array until size == capacity
. However, my code is crashing right when it gets to the for loop, as it tries to copy everything over to the new stack and delete the old stack. Any ideas?