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 9142745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:51:12+00:00 2026-06-17T09:51:12+00:00

I come from Java, where filling containers did not involve thinking. My problem now

  • 0

I come from Java, where filling containers did not involve thinking. My problem now that i work with c++ is, that filling a container in a function with data which is declared in function scope can lead to errors, where the data doesn’t exist any more when I want to access it.
I could not find tutorials addressing the problem, so I went the Java way and only made containers getting pointers declared with “new”. But now I am forced to return a

std::list<Vertex<float> >

from a function and thought this might be a good point to learn how I would fill and return such a thing. Would this

{
std::list<Vertex<float> > myList;
Vertex<float> v(0.0, 0.1, 0.2);
myList.push_back(v);
myList.push_back(Vertex<float>(1,0, 1.1, 1.2));
return myList;
}

actually be fine as a sample function body? And if yes, why would v still exist outside of the scope? Does each insertion in a container also imply a copying?

  • 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-17T09:51:13+00:00Added an answer on June 17, 2026 at 9:51 am

    This would work “fine” since every operation there creates a copy.

    • myList.push_back(v); creates a copy of v so that visiblity of v is now irrelevant.
    • return myList; returns a copy of the list to the calling function so the visiblity of myList is now irrelevant. The calling function should make a copy of this list to keep it in scope, else it will be destroyed at the end of execution of the line that calls this function.

    The reason that fine is quoted is that copies are typically expensive. In your case they are quite small so it might be irrelevant, and in many cases they might be optimised away, but it is still something to keep in mind.

    Old C++ way of optimizing is to pass a list by reference and use that to construct your list, instead of returning by value.

    void MakeMeAList(std::list<Vertex<float> >& aList){
        ....
    }
    
    std::list<Vertex<float> > aList;
    MakeMeAList(aList);
    

    As @billz suggests, Return Value Optimization should optimize away the copies even if this is not done.

    New C++ (c++11) –
    The use of emplace_back to construct the list, would be more efficient that copying, as long as the input variables are not going to be used any more. ( thanks @Troy)

    My C++11 is weak, I am almost sure even returning by value is OK since Move semantics would optimize it away, but I am only 95% sure.

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

Sidebar

Related Questions

I come from a Java SE background, and I did some servlets tutorials and
My problem is understanding is the syntax of Scala. I come from a Java
I come from Java background, where we have data structures with interfaces that if
I come from a Java background and I am now trying to study PHP
I come from a Java/Eclipse background and I fear that I am spoiled by
I come from the Java EE world but now I'm working on a .Net
Firstly, I will say that I come from the Java world (this is important,
I come from the Java world so I was shocked to discover that arrays
I come from a Java background, where packages are used, not namespaces. I'm used
I come from Java and see that package in Java is very convenient. When

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.