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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:28:31+00:00 2026-05-26T23:28:31+00:00

Deal all, I have implemented some functions and like to ask some basic thing

  • 0

Deal all, I have implemented some functions and like to ask some basic thing as I do not have a sound fundamental knowledge on C++. I hope, you all would be kind enough to tell me what should be the good way as I can learn from you. (Please, this is not a homework and i donot have any experts arround me to ask this)

What I did is; I read the input x,y,z, point data (around 3GB data set) from a file and then compute one single value for each point and store inside a vector (result). Then, it will be used in next loop. And then, that vector will not be used anymore and I need to get that memory as it contains huge data set. I think I can do this in two ways.
(1) By just initializing a vector and later by erasing it (see code-1). (2) By allocating a dynamic memory and then later de-allocating it (see code-2). I heard this de-allocation is inefficient as de-allocation again cost memory or maybe I misunderstood.

Q1)
I would like to know what would be the optimized way in terms of memory and efficiency.

Q2)
Also, I would like to know whether function return by reference is a good way of giving output. (Please look at code-3)

code-1

int main(){

    //read input data (my_data)

    vector<double) result;
    for (vector<Position3D>::iterator it=my_data.begin(); it!=my_data.end(); it++){

         // do some stuff and calculate a "double" value (say value)
         //using each point coordinate 

         result.push_back(value);

    // do some other stuff

    //loop over result and use each value for some other stuff
    for (int i=0; i<result.size(); i++){

        //do some stuff
    }

    //result will not be used anymore and thus erase data
    result.clear()

code-2

int main(){

    //read input data

    vector<double) *result = new vector<double>;
    for (vector<Position3D>::iterator it=my_data.begin(); it!=my_data.end(); it++){

         // do some stuff and calculate a "double" value (say value)
         //using each point coordinate 

         result->push_back(value);

    // do some other stuff

    //loop over result and use each value for some other stuff
    for (int i=0; i<result->size(); i++){

        //do some stuff
    }

    //de-allocate memory
    delete result;
    result = 0;
}

code03

vector<Position3D>& vector<Position3D>::ReturnLabel(VoxelGrid grid, int segment) const
{
  vector<Position3D> *points_at_grid_cutting = new vector<Position3D>;
  vector<Position3D>::iterator  point;

  for (point=begin(); point!=end(); point++) {

       //do some stuff         

  }
  return (*points_at_grid_cutting);
}
  • 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-05-26T23:28:31+00:00Added an answer on May 26, 2026 at 11:28 pm

    erase will not free the memory used for the vector. It reduces the size but not the capacity, so the vector still holds enough memory for all those doubles.

    The best way to make the memory available again is like your code-1, but let the vector go out of scope:

    int main() {
        {
            vector<double> result;
            // populate result
            // use results for something
        }
        // do something else - the memory for the vector has been freed
    }
    

    Failing that, the idiomatic way to clear a vector and free the memory is:

    vector<double>().swap(result);
    

    This creates an empty temporary vector, then it exchanges the contents of that with result (so result is empty and has a small capacity, while the temporary has all the data and the large capacity). Finally, it destroys the temporary, taking the large buffer with it.

    Regarding code03: it’s not good style to return a dynamically-allocated object by reference, since it doesn’t provide the caller with much of a reminder that they are responsible for freeing it. Often the best thing to do is return a local variable by value:

    vector<Position3D> ReturnLabel(VoxelGrid grid, int segment) const
    {
      vector<Position3D> points_at_grid_cutting;
      // do whatever to populate the vector
      return points_at_grid_cutting;
    }
    

    The reason is that provided the caller uses a call to this function as the initialization for their own vector, then something called “named return value optimization” kicks in, and ensures that although you’re returning by value, no copy of the value is made.

    A compiler that doesn’t implement NRVO is a bad compiler, and will probably have all sorts of other surprising performance failures, but there are some cases where NRVO doesn’t apply – most importantly when the value is assigned to a variable by the caller instead of used in initialization. There are three fixes for this:

    1) C++11 introduces move semantics, which basically sort it out by ensuring that assignment from a temporary is cheap.

    2) In C++03, the caller can play a trick called “swaptimization”. Instead of:

    vector<Position3D> foo;
    // some other use of foo
    foo = ReturnLabel();
    

    write:

    vector<Position3D> foo;
    // some other use of foo
    ReturnLabel().swap(foo);
    

    3) You write a function with a more complicated signature, such as taking a vector by non-const reference and filling the values into that, or taking an OutputIterator as a template parameter. The latter also provides the caller with more flexibility, since they need not use a vector to store the results, they could use some other container, or even process them one at a time without storing the whole lot at once.

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

Sidebar

Related Questions

I'm sure that all of us have had to deal with telecommuters at some
The majority of resources that I have for UI design all deal with the
I have several methods that deal with DB and all of them start by
Hey all, here's the deal... I've got a UIImage that is being loaded in
Hey all. I am using sifr 2.0.7 and here's the deal. The sifr is
I have to deal with text files in a motley selection of formats. Here's
I have found some threads that explain why C++ separates .cpp and .h files
OK so I'm looking a some code which looks roughly like this: void DoSomething(object
Suppose I have some kind of factory function which creates objects that are largely
There are a few similar questions, but nothing like this. How do you deal

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.