I just wrote a simplified implementation of the stack data structure in a class, but the handling of an integer array is behaving in a way that I can't seem to understand.
The same snippet of code as in push()
will give the behavior I expect, but in this program assigning a value at a certain array position will assign the value to the index variable>
#include <iostream>
using namespace std;
class stack
{
public:
stack(int size)
{
ar_size = size - 1;
array[ar_size];
index = 0;
}
void push(int value)
{
cout << "index; " << index << endl; //will output 0
cout << "value: " << value << endl; //will output 8
array[index++] = value;
cout << "index; " << index << endl; //will output 8
cout << "value: " << value << endl; //will output 8
cout << "array: " << array[index] << endl; //will output what seems to be a memory address
}
int pop()
{
cout << "index; " << index << endl; //will output 8
return array[index--];
}
private:
int ar_size;
int array[];
int index;
};
int main()
{
stack tower(64);
tower.push(8);
int r = tower.pop();
cout << "pop: " << r << endl; //will output what seemed to be a memory address
return 0;
}