Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

does vector still exist?

Tags:

c++

visual-c++

std::vector<float> someOp(void)
{
    using namespace std;
    vector<float> results;
    // some operations done to results
    return results;
}

int main(void)
{
    using namespace std;
    vector<float> &results = someOp();
}

Does the vector returned by someOp exist in the someOp() stack space or in the main() stack space?

I'm inclined to believe that it doesn't get copied/moved to the main() stack space since the results vector has the same address inside of both methods.

like image 614
helloworld922 Avatar asked Jun 23 '26 10:06

helloworld922


1 Answers

Neither, that's not valid C++ (and doesn't get compiled by g++).

It seems you're trying to store a reference to the returned results, but that's impossible as the returned results exists in the stack frame of someOp, and, while it will still be there just after someOp() returns, will get overwritten at some point in the future.

like image 120
slugonamission Avatar answered Jun 24 '26 22:06

slugonamission



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!