hi guys, im trying to figure out why my program is not working, it's not finished yet, but i cant understand why its not working until now
i think that the problem is in this lineCode://stack with integers
#include <iostream>
using namespace std;
class stack{
int pos;
int *a;
int check;
public:
stack();
void freestack();
void add(int);
};
stack::stack(){
check = 1;
a = new int[10];
cout<<"array of 10 has been created..."<<endl;
pos = 0;
}
void stack::freestack(){
delete[] a;
}
void stack::add(int n){
if (pos<check*10){
*(a+pos) = n;
pos++;
cout<<"New integer added"<<endl;
}
else {
a = (int *)realloc(a,++check*10*sizeof(int)); // idont know how to do this in c++
if (a!=NULL){
cout<<"More memory assigned\n"<<endl;
pos++;
a[pos]=n;
cout<<"New integer added"<<endl;
}
}
}
int main(void){
stack newstack;
newstack.freestack();
newstack.add(1);
system("pause");
return 0;
}
because when i comment this line then the program works fine, i cant understand why its not working thereCode:a[pos] = n;
I mean I have the pos variable which is private, so is the array, i have allocated 40 bytes in the constructor and the a variable points to the first element of the allocated memory in heap
but why doesnt this work?
thanks in advance