0

So I have to implement a stack using an array built in a class, and if the "stack" ever fills up, I am supposed to increase the size of the array which I attempted, and failed. So I am just curious as to what I need to change in order to make this work.

class AbstractStack { private: Type elements; // elements in the array Type max; Type *s; public: AbstractStack(Type num) { //CONSTRUCTOR elements= -1; this->max = num; s = new Type[max]; } /* bunch of code that does not apply to this issue */ void push ( Type e ) { if (elements + 1 == max) { cout << "Stack at max size, WIll increase size of array and add item" << endl; Type *temp = new Type[max + (max/2)]; for (int i = 0; i < elements+1; i++) { temp[i] = s[i]; } s = temp; delete temp; elements++; s[elements] ; return; } else { elements++; s[elements] = e; } } 

When I take the size of this new s, I get the correct size of 1 larger than before because this function is only called when trying to add 1 element to the full stack, but when I attempt to use the top function, it just gives me 0 then I get like 50 lines of error codes starting in:

 *** Error in `./a.out': double free or corruption (top): 0x0000000000c53cf0 *** ======= Backtrace: ========= /lib64/libc.so.6(+0x7c619)[0x7fa34a270619] ./a.out[0x400c38] ./a.out[0x400b48] /lib64/libc.so.6(__libc_start_main+0xf5)[0x7fa34a215c05] ./a.out[0x400979] 
4
  • You copy the pointer from temp to s. You should swap those two. Commented Feb 27, 2018 at 6:24
  • I don't think you need return where it is, and you probably need to delete s instead of temp, then assign temp to s. Commented Feb 27, 2018 at 6:28
  • Type elements? Type max? If your Type is string, what's a max string? Commented Feb 27, 2018 at 6:41
  • You should consider upvoting and/or accepting answers you find useful here. Commented Feb 28, 2018 at 4:26

2 Answers 2

2
Type elements; // elements in the array Type max; 

These are both just ints, or unsigneds, or size_ts, or whatever you prefer for counting. They have nothing to do with Type whatsoever.

void push ( Type e ) { if (elements + 1 == max) { cout << "Stack at max size, WIll increase size of array and add item" << endl; Type *temp = new Type[max + (max/2)]; 

After this you should increase max to max*3/2.

 for (int i = 0; i < elements+1; i++) { 

Loop condition should be i < elements. You are using element zero, and element[elements] does not exist yet.

 temp[i] = s[i]; } s = temp; delete temp; 

Last two lines should be delete[] s followed by s = temp.

 elements++; s[elements] ; 

Last two lines should be s[elements++] = e;

 return; 

return is redundant here.

 } else { elements++; s[elements] = e; 

Again, the last two lines should be s[elements++] = e;

 } } 

Corrected and simplified version:

int elements; int max; // ... void push ( Type e ) { if (elements + 1 == max) { cout << "Stack at max size, WIll increase size of array and add item" << endl; max += max/2; Type *temp = new Type[max]; for (int i = 0; i < elements; i++) { temp[i] = s[i]; } delete[] s; s = temp; } s[elements++] = e; } 
Sign up to request clarification or add additional context in comments.

Comments

1

You have to delete the old array (s) instead of the new (temp):

delete[] s; s = temp; 

Also: make sure that your class has a proper destructor (deleting s).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.