-2

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?

Tyler
  • 365
  • 2
  • 4
  • 15
  • what is the value of `capacity` when you enter that `if` statement? – sfletche May 09 '15 at 01:56
  • Your code does not conform to any visible structure, making it hard to follow (global variables? ewww)... or just meaningless (for example `ExStack()` is never called). Anyway, your crash might be cause by your incorrect iteration of elements in `stack` in your `for` loop as you are exceeding the bounds of the array. – Dai May 09 '15 at 01:56
  • @sfletche If the user enters 10 when the `ExStack` is created, capacity would be 10. It can hold 10 items at most. – Tyler May 09 '15 at 01:57
  • @Tyler - did you put a `cout` statement in there to test your assumption? – sfletche May 09 '15 at 01:59
  • @sfletche Yes I did. Capacity stayed at 10. – Tyler May 09 '15 at 02:00
  • How about `cout << stack[i];`...is that outputting anything? – sfletche May 09 '15 at 02:02
  • 1
    This is terrible code, I find it hard to call this C++. I would *highly* recommend that you get a [decent book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and actually learn the language. – Baum mit Augen May 09 '15 at 02:04
  • @sfletche That is actually giving me an error. Operand types are std::ostream << some_struct – Tyler May 09 '15 at 02:05
  • 1
    @Tyler - The code is very hard to follow as to what you're doing. You have a class called `ExStack` but you also have a stand-alone function called `create_new_array`. Shouldn't that function be a member of your ExStack class? – PaulMcKenzie May 09 '15 at 02:14
  • 1
    @Tyler Please post your main() program that you're testing. – PaulMcKenzie May 09 '15 at 02:30

1 Answers1

2

You are deleting the new stack that you created at the end of your function.

delete[] new_stack;

You only need delete[] stack; to get rid of the old memory. The new memory that you allocated in new_stack needs to be kept.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • But the program is crashing even during the for loop. – Tyler May 09 '15 at 02:07
  • 1
    Then you really need to post an [MCVE](http://stackoverflow.com/help/mcve) or [SSCCE](http://sscce.org/). As is your `create_new_array()` function is missing a closing curly brace. – NathanOliver May 09 '15 at 02:29
  • Sorry, I tried out your solution and it worked! Thank you very much! – Tyler May 09 '15 at 06:42