0

I'm working on a program to stack numbers independent from their type and print them. Everything works but unfortunately it seems the first array element comes out incorrectly.

Expected output:

6 5 4 3 2 1

Actual output:

6 5 4 3 2 6

Does anybody have any suggestions?

StackI.hpp

#ifndef StackI_hpp
#define StackI_hpp

template <typename T>

class StackI{

public:
    virtual void push(T t) = 0;
    virtual void print()=0;
};
#endif

Stack.hpp

#ifndef Stack_hpp
#define Stack_hpp    
#include <iostream>
#include "StackI.hpp"

using namespace std;

template <typename T>

class Stack:StackI<T>{
protected:
   int arraySize;
   int elements; 
   T arr[]; 
   int position; 
 
public:

    Stack(int arraySize){
        this->arraySize = arraySize; //let user pass array size
        arr[arraySize];
        elements = 0;
        position = 0;
    }   
//insert new element
    void push(T t){
        if (elements <arraySize) {
                arr[position++] = t;
                elements++;
        }
    }    
// print result
    void print(){
            for (int i = position-1; 0<=i;i--){
                cout<< arr[i];
            }
        } 
};
#endif // Stack_hpp

Main.cpp

#include "Stack.hpp"
#include <iostream>

int main(){  
    Stack<int> d = Stack<int>(6);
    d.push(1);
    d.push(2);
    d.push(3);
    d.push(4);
    d.push(5);
    d.push(6);  

    d.print();

    return 0;
}
user7778287
  • 367
  • 1
  • 11
pacours
  • 33
  • 6
  • 5
    Is `arr[arraySize]` used to allocated memory? wrong syntax, you have to use `new` (or better `std::vector`). – Jarod42 Feb 16 '21 at 17:08
  • 1
    I'd suggest you first extract a [mcve] and provide that instead. This implies merging the files and stripping the `template` code, unless of course that changes the unexpected behaviour. – Ulrich Eckhardt Feb 16 '21 at 17:09
  • @Jarod42 that was it, easy as that. Thank you very much! Still trying to get my head around this language.. Highly appreciated – pacours Feb 16 '21 at 17:15
  • What happens if in `print()` outside the `for` loop, you add `cout << arr[0]`? Do you get 1 or 6? – user7778287 Feb 16 '21 at 17:21
  • 2
    @user7778287 It doesn't matter, the code is already in *undefined behavior* land. There was never any allocation for the non-standard flexible array member (which I'm pretty sure the OP didn't even know they had). Changing the logic of the management of that to either manual management (new), smart management (unique_ptr + make_unique) or a std::vector are all ways to solve that, and ultimately the OPs issue. – WhozCraig Feb 16 '21 at 17:25
  • 1
    Gee wiz @WhozCraig if every question on SO was simply "OP's issue" this website probably wouldn't exist. – user7778287 Feb 16 '21 at 17:30
  • Yes I definitely agree that the issue is my lack of understanding of the langage and my missing skillset but as user7778287 mentionned it all comes from these struggles I guess. And solving the problem with a pointer & new solved my issue perfectly. Thanks for your time! – pacours Feb 16 '21 at 17:36
  • @user7778287 That's not what I was stating and sorry you took it that way. The first comment in this list pointed out the problem; the third confirmed it, and the OP already fixed it (making the very question moot at that point, granted). The problem with the code was a misunderstanding of syntax and improper memory management leading to undefined behavior. Once in that realm of UB, drilling into details deeper is a specter chase until the UB is fixed first. – WhozCraig Feb 16 '21 at 17:37

0 Answers0