From: John Fine <johnsfine@verizon.net>
Cc: gcc-help <gcc-help@gcc.gnu.org>
Subject: Is this code wrong?
Date: Sat, 09 Aug 2008 01:09:00 -0000 [thread overview]
Message-ID: <489CC728.5040603@verizon.net> (raw)
In-Reply-To: <489C4C8F.4060804@verizon.net>
While investigating an unrelated (really) problem, I came across a bunch
of examples of a construct that I'm pretty sure shouldn't work, yet it
seems to be working. Am I misunderstanding this (is there some reason
this code should work)? This is all in code belonging to my employer,
written by another employee, so I can't quote a large enough chunk for
you to test. But I think the concepts are simple enough for someone to
answer based on the info I can provide.
An inline function declares a std::vector, initializes it, then returns
it. The calling function assigns that return value to a const&.
My understanding is that the return of the local object makes a
temporary copy of that object, which exists only during the calling
statement. So the reference is to that temporary object, which no
longer exists.
inline std::vector<int> get_vector()
{
std::vector<int> result;
... code to put contents into result ...
return result;
}
in some other function
std::vector<int> const& local_vector = get_vector();
unrelated code
read the contents from local_vector
What is the scope of the temporary vector that catches the return value
from the function? I thought that scope should be just that statement.
Could the scope be the rest of the {} containing the statement?
Is this code working just because of a lazy destructor (frees the
memory, but leaves the pointer and contents intact and nothing happens
to reallocate that memory soon enough to trash it)? Or is the
destructor really not called until later?
next prev parent reply other threads:[~2008-08-08 22:23 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-08-08 6:55 odd behavior with Character Arrays Rohit Arul Raj
2008-08-08 7:07 ` Mateusz Loskot
2008-08-08 9:38 ` Rohit Arul Raj
2008-08-08 12:10 ` Jędrzej Dudkiewicz
2008-08-08 13:00 ` Mateusz Loskot
2008-08-08 13:40 ` Mateusz Loskot
2008-08-08 15:01 ` John Fine
2008-08-09 1:09 ` John Fine [this message]
2008-08-09 2:44 ` Is this code wrong? Eljay Love-Jensen
2008-08-09 11:23 ` corey taylor
2008-08-09 14:57 ` John Fine
2008-08-08 15:38 ` odd behavior with Character Arrays Bob Plantz
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=489CC728.5040603@verizon.net \
--to=johnsfine@verizon.net \
--cc=gcc-help@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).