Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why does this work (stack vs heap allocation)?

51 views
Skip to first unread message

Erdoeban Zsukloff zu Brecher Zhuang

unread,
Jul 31, 2016, 9:40:58 PM7/31/16
to
using namespace std;
vector<int> build_vector(void)
{
vector<int> result;
result.push_back(1);
return result;
}
int main(void)
{
auto V = build_vector();
// V should be broken but isn't !
for (auto vv : V) cout << vv ;
}

Isn't result allocated on stack and thus should be destroyed right upon
scope's "}" ? I distinctly recall getting compiler warnings and nasty memory
errors, so I got into habit of always using heap when returning anything
non-trivial, yet I lately see stuff like the snippet above in legitimate
authors ...

Thank you in advance !


---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus


--- news://freenews.netfront.net/ - complaints: ne...@netfront.net ---

Richard Damon

unread,
Jul 31, 2016, 10:37:38 PM7/31/16
to
On 7/31/16 9:40 PM, Erdoeban Zsukloff zu Brecher Zhuang wrote:
> using namespace std;
> vector<int> build_vector(void)
> {
> vector<int> result;
> result.push_back(1);
> return result;
> }
> int main(void)
> {
> auto V = build_vector();
> // V should be broken but isn't !
> for (auto vv : V) cout << vv ;
> }
>
> Isn't result allocated on stack and thus should be destroyed right upon
> scope's "}" ? I distinctly recall getting compiler warnings and nasty
> memory errors, so I got into habit of always using heap when returning
> anything non-trivial, yet I lately see stuff like the snippet above in
> legitimate authors ...
>
> Thank you in advance !
>

Because you are returning the vector by value, so it is copied. The
problem is when you return a pointer to an auto value.

woodb...@gmail.com

unread,
Aug 1, 2016, 12:08:30 AM8/1/16
to
I think it would be better to say a pointer to a
local variable. If you go by the standard, the
meaning of the word auto changed a few years ago.

Brian
Ebenezer Enterprises - In G-d we trust.
http://webEbenezer.net

Ike Naar

unread,
Aug 1, 2016, 1:29:29 AM8/1/16
to
Returning a pointer to a local variable with static storage duration
would be okay, though.
Most likely Richard meant 'object with automatic storage duration'
when he used 'auto value'.

leigh.v....@googlemail.com

unread,
Aug 1, 2016, 7:59:27 AM8/1/16
to
Most optimisers will elide the copy using NRVO or move semantics.

/Leigh

Erdoeban Zsukloff zu Brecher Zhuang

unread,
Aug 11, 2016, 12:43:44 PM8/11/16
to


"Richard Damon" wrote in message news:IBynz.64968$Cu5....@fx36.iad...
You're absolutely right - changed function to return pointer and lo

%g++ var.cc -o var
var.cc: In function ‘VS_or_pc* build_vec()’:
var.cc:14:11: warning: address of local variable ‘result’ returned
[-Wreturn-local-addr]
VS_or_pc result;
^~~~~~
%./var.exe
zsh: segmentation fault (core dumped) ./var.exe

Thank you.
0 new messages