0

I am new to C++ and would like to access the values in a subclass. When I am trying to access the values, my program is crashing and returning stack-dump.

For example:

class test{ 
protected:
    std::string name;
    int points;
    object** inventory;

public:
    test(const std::string name, int points) : name(name), points(points), inventory(new object*[10]()) {
        for(int i = 0; i < 10; i++) {
            this->inventory[i]->setValid(false); 
        }
    }



class object {
    protected:
        bool isValid;
        std::string name;
        int value;
    public:
        object(const std::string name, int value) : name(name), value(value), isValid(false) {}
        const std::string getName();
        bool getValid();
        void setValid(bool isValid);
};

In the header file.:

void object::setValid(bool isValid) {
    this->isValid = isValid;
    //std::cout << isValid; returning of isValid is possible, but not of this->isValid
}

The necessary header files and declarations are included. While debugging it stops while trying to get the value of this->isValid in my class object with the following error message:

Failed to execute MI command:
-data-evaluate-expression ((this)->isValid)
Error message from debugger back end:
Cannot access memory at address 0xc

Do I use an incorrect pointer? How can I solve the issue?

XavierStuvw
  • 1,294
  • 2
  • 15
  • 30
keita063
  • 77
  • 6
  • you mean a child class? There are no subclasses in your code – Garr Godfrey Mar 03 '20 at 21:19
  • most likely, you didn't initialize your inventory array correctly, if at all – Garr Godfrey Mar 03 '20 at 21:21
  • The close reason is a guess, at what the asker's trying to do just like the only answer. This question should have been closed as unclear or lacking [mcve]. If I didn't think it would promptly relocked, hopefully for a better reason, I'd unlock it. – user4581301 Mar 03 '20 at 21:45
  • @user I disagree. They say they're getting a SegFault and asking for the solution. Their program has UB because they've initialized `inventory` as `new object*[10]()` and they're trying to dereference an uninitialized pointer with `this->inventory[i]->setValid(false);`. I think the dupe perfectly explains their UB and will help them resolve the SegFault. What makes you say this is unclear? – scohe001 Mar 03 '20 at 22:22
  • @scohe001 `this->inventory[i]->setValid(false);` is not the behaviour of someone who is trying to use a 2D array. I think the asker smurfed up at `object** inventory;` by putting in an extra level of indirection. The rest proceeds from flailing around trying to clear up the compiler errors without really understanding why they got compiler errors. Garr's answer is probably the right idea. It's unclear because there is no way to be sure without more input from the asker. – user4581301 Mar 03 '20 at 22:33
  • Ahh. I see what you're saying @user. You're probably correct. I wasn't looking at the broader picture here. – scohe001 Mar 03 '20 at 22:37

1 Answers1

3

This is a pointer to a pointer to an object. You allocated an array of pointers to objects, but not the objects themselves.

object** inventory;

Once you do this:

inventory(new object*[10]())

You can now access inventory[0] through inventory[9]. But they aren't set to anything yet. They may not even be null, it's just garbage memory.

You can allocate the objects in the loop:

for(int i = 0; i < 10; i++) {
    inventory[i] = new object();
    inventory[i]->setValid(false); 
}

But, you'll need to remember to free all of those objects. You might consider using array allocators to allocate an array of objects instead of an array of pointers to objects. But since this is C++, better to use a vector.

std::vector<object>  inventory
user4581301
  • 33,082
  • 7
  • 33
  • 54
Garr Godfrey
  • 8,257
  • 2
  • 25
  • 23