0

I have a class and in there I have a struct which contains a string. In main I declare the class, then create a struct to reach that string. How it looks like:

Class *myClass = new Class;
Class::Struct *myStruct;

myStruct = &myClass->getStruct(); // the struct is in a vector so I get it through a function

cout << myStruct->string << "\n";

So, with this method I can't reach the string in the struct, but I can the int's, float's, etc. If I change the code to this:

Class::Struct myStruct;

myStruct = myClass->getStruct();

cout << myStruct.string << "\n";

then it works.

My question is, why is the second one working, and the first not, and why only the string? It's not about life and death, because I don't really need that string, I just tested the program, but I'm really curious what's going on, because I'm still learning the language, and especially pointers.

Thanks in advance!

EDIT: Ok, so the definitions

class Class {
public:
    struct Struct {
        string name;
        float data;
    };

    Class();
    Struct getStruct( int i );

private:
    vector<Struct>  tempStruct;
};

Class::Class() {
    Struct str;
    str.name = "test";
    str.data = 1.0f;

    tempStruct.push_back( str );
}

struct Class::getStruct( int i ) {
    return tempStruct[i];
}

EDIT2: That was a little mistake, but I forgot the &. But you helped me to find it.

2
  • 1
    Show the class and struct definition. It's hard to guess with so little info. Commented Oct 26, 2012 at 14:04
  • 1
    It almost certaintly the case that the getStruct() returns a copy of the struct from the vector, meaning in the first case myStruct has the address of a temporary object resulting in myStruct being a dangling pointer. Commented Oct 26, 2012 at 14:06

1 Answer 1

1

This is because getStruct() returns a copy of the struct. This is called a "temporary". The temporary dies immediately. So now you have a pointer to a dead temporary. It doesn't exist anymore so the pointer to it is invalid. Either return a pointer or reference to the struct in getStruct(), or hold a reference to its return value like this:

const Class::Struct& myStruct = myClass->getStruct();
Sign up to request clarification or add additional context in comments.

6 Comments

the function should return a reference, not a copy (or a (usually) pointer)
@Zharf Well, we don't know for sure what it should return since we can't see the code of the class. I added it to the answer though, to be more complete.
But as I wrote before, the problem is only with the string. I can write out for example the value of the float, but not the value of the string. And that's what I can not understand.
@matthew3r This is due to the memory still containing remnants of the data. In other words, you're accessing corpses. In programming, corpses decay very quickly though.
@matthew3r it's technically undefined behaviour, don't count on it
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.