-5

this is my .cpp file content:

#include <iostream>
#include"1.h"
using namespace std;

Stack:: Stack(){
size=20;    
 a=new int[size];   
top=-1;
}

Stack::Stack (int si){

size=si;
a=new int[si];
top =-1;}

Stack::Stack(Stack& s){

a=new int[s.size];
for(int i=0 ; i<s.size; i++)
    a[i]=s.a[i];
size=s.size;
}

Stack::~Stack(){

delete [] a;
}

void Stack::Push(int data){

if(this->isfull())
    cout<<"stack is full!\n";
else
    a[top++]=data;
}

int Stack::Pop(){

if(this->isempty())
    cout<<"stack is empty!\n";
else
    return a[top--];
}

bool Stack::isempty(){

if(top==-1) 
     return true;
 else
    return false ;
}   

bool Stack::isfull(){

if(top==size-1 ) 
    return true ;
else
    return false ;
}

void Stack::Print(){

for(int i=top ; i>-1 ; i--)
    cout<<a[i]<<endl;
}

int main(){

Stack a(3);
a.Push(1);
a.Push(3);
cout<<a.Pop();
a.Push(5);
a.Push(7);
a.Print();  
return 0;
}

And after running the program, i get the following error: Error in `./1': double free or corruption (out): 0x000000000240a010 *** Aborted (core dumped) and I have copy constructor and any thing, what sould I do?

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182

2 Answers2

2
  • The default constructor sets top to -1, so a[top++]=data; in Stack::Push(int) is undefined behavior (attempt to write to a[-1], which is out of the array bounds). This is the corruption the error message refers to, which is only noticed by your standard library when delete [] a is invoked. Change this to a[++top] = data;.
  • Your copy constructor does not assign to top, leaving its value uninitialized in copies. Reading top from a copy is therefore undefined behavior.
  • Your copy constructor should be Stack(Stack const &).
  • You should implement a copy-assignment operator Stack & operator=(Stack const &);.
Community
  • 1
  • 1
cdhowie
  • 158,093
  • 24
  • 286
  • 300
  • is it really possible to write `Stack(Stack const &)` instead of `Stack(const Stack&)` or is it a typo? – 463035818_is_not_an_ai May 10 '17 at 20:02
  • not a typo, but for some reason I have never seen it before – 463035818_is_not_an_ai May 10 '17 at 20:04
  • 1
    @tobi303 It is possible, and I prefer it because it makes complex pointer declarations more obvious -- the `const` always refers to the thing to the left, so most declarations read naturally right-to-left. `A ** const * const` is a constant pointer to a constant pointer to a pointer to `A`. Likewise, `A const &` is a reference to a constant `A`. `A const &` and `const A &` name the same type. `const A &` is permitted, but I find that having `const` sometimes refer to the thing to the right and sometimes to the left to be a bit silly. I therefore always put it after the thing that's const. – cdhowie May 10 '17 at 20:04
  • makes sense. I mean it makes no sense to differentiate between reference to const or const reference, but I agree that it helps to be consistent – 463035818_is_not_an_ai May 10 '17 at 20:06
  • Right, and I find that consistency helps avoid mistakes. Once you get used to writing `A const &` it actually feels quite natural. – cdhowie May 10 '17 at 20:07
1

a[top++] should be a[++top] as you initialize top at -1, else you have out of bound access, so UB.

Jarod42
  • 203,559
  • 14
  • 181
  • 302