Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8430561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:37:27+00:00 2026-06-10T05:37:27+00:00

Is the following code really dangerous? doSmth returns object of vector, which should be

  • 0

Is the following code really dangerous? doSmth returns object of vector, which should be copied from cont(doSmth) and then stored in a stack in the scope of function test. Thus, I believe that it will be desctructed only after returning from test.

struct MyData
{
  double m_i;
};

std::vector<MyData> doSmth()
{
  std::vector<MyData> cont(10);
  return cont;
}

void test()
{
  MyData& oneElement = doSmth()[0];
  std::cout << oneElement.m_i << std::endl;
}

Yet valgrind thinks differently:

Invalid read of size 8
<line 1 in test function oneElement>
Address 0x101281db8 is 8 bytes inside a block of size 72 free'd
std::vector<BlockInfo, std::allocator<BlockInfo> >::~vector() (stl_vector.h:314)
<line 2 in test function>

Is it a problem in my logic or valgrind lies?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T05:37:28+00:00Added an answer on June 10, 2026 at 5:37 am

    The return value is destroyed at the end of the full expression, i.e. at the first semicolon in the function test. At that point, oneElement becomes a dangling reference.

    You can prolong the lifetime of the return value by binding it to a const reference:

    void test()
    {
        std::vector<MyData> const & r = doSmth();
        MyData const & oneElement = r[0];
        std::cout << oneElement.m_i << std::endl;
    }
    

    In the above case, the return value is destroyed at the end of the function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code which I'm really struggling to compile: #include <stdio.h> #include
The following code shows a class which privately inherits from an interface having its
I'm getting an error I don't really understand from the following code: public IList<Store>
I feel really stupid right now. The following code should output 'If you can
Hi I have the following code which is really long of course, how can
I am trying to get the following code to work (which looks really straightforward
I really don't understand the striping behaviour resulting from the following code. I had
The following code seemed really confusing to me since it provided two different outputs.The
I'm really stuck on why the following code block 1 result in output 1
Boy, this one is really weird. I expect the following code to print 1990,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.