#UB in member-variable reference

33 messages · Page 1 of 1 (latest)

high sierra
#

Hey guys, thanks for all the help thus far. Let's tackle this one.

Here is my struct I'm trying to create:

    struct Map {
        int32_t width;
        int32_t height;

        std::vector<Coordinate> map;

        Coordinate& sPos;
        Coordinate& ePos;
    };

And here is the function that creates the struct:

    Map parse_input(std::istream& stream) {
        std::vector<Coordinate> map; 
        int32_t height = 0;
        int32_t width = 0;

        Coordinate sPos;
        Coordinate ePos;

        for(std::string line; std::getline(stream, line); ) {
            std::istringstream iss(line);
            char temp;
            width = 0;
            while(iss >> temp) {
                if(temp == 'S') {
                    sPos = Coordinate{ width, height, 0, temp };
                    map.push_back(Coordinate{ width, height, 0, temp });
                }
                else if(temp == 'E') {
                    ePos = Coordinate{ width, height, 100, temp };
                    map.push_back(Coordinate{ width, height, 100, temp });
                } else {
                    map.push_back(Coordinate{ width, height, temp-'a', temp });
                }
                width++;
            }
            height++;
        }

        auto& test = map[(sPos.y * height) + sPos.x];
        auto value = (sPos.y * height) + sPos.x;
        return Map{ width, height, map, test, map[(ePos.y * height) + ePos.x] };
    }

The goal here is that I want to create my struct in place in the return with references to those objects in the map vector. REMEMBER map is not a <map> it is a VECTOR!!!!

For some fucking reason, after the return Map, the object after inspection has a correct reference to ePos but some random fucking UB data for sPos. Guess what? test returns the correct reference, value evaluates to 0. How the fuck can ePos work and not sPos?

rich adderBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

jolly spoke
high sierra
#

no, I did that to ensure that the map[selectionCalculation] actually returns the correct object

#

It's weird because ePos reference works fine... just not sPos

#

it's really odd

sour wind
#

you are returning a struct that contains references to elements of a local vector

high sierra
#

I want to know why

sour wind
#

that vector is destroyed the moment your function returns

high sierra
#

but that local vector is passed to the struct

#

by value

sour wind
#

you are making a copy

high sierra
#

Ok, so my references are broken then?

sour wind
#

the references do not refer to elements of that copy

high sierra
#

Why is it that ePos still works

#

ePos will work, it points to the right object everytime

sour wind
#

you can std::move the vector into your Map

#

that'll probably fix the problem unless i'm overlooking something

jolly spoke
#

yeah it does fix it

#

you just have to create the references to the map before you move it

high sierra
#

Ok, I need to jet here to poop, but I will try when I get back

#

thanks guys

rich adderBOT
#

@high sierra Has your question been resolved? If so, run !solved :)

sour wind
#

np ^^

high sierra
#

I'm doing std::move(map) in the initialization list

#

and it now breaks my sPos and ePos in the list \

#

it moves the values though

sour wind
#

wdym?

high sierra
#

nvm I got it working

#

my b

sour wind
#

k^^

rich adderBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.