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

  • Home
  • SEARCH
  • 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 8028847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:15:03+00:00 2026-06-05T00:15:03+00:00

I want to create a vector of data, but I want to both set

  • 0

I want to create a vector of data, but I want to both set it’s size and fill its elements in a sub-function. Is this an appropriate time to use the new operator? Is there a better way to do it? It seems like an appropriate time, but I’m hesitant because of Why should C++ programmers minimize use of 'new'?

int main()
{
    vector<double> *array1;
    vector<double> *array2;
    OtherArgs otherArgs;
    FillArrays(array1,array2,otherArgs);
    //Do other stuff 
    delete array1;
    delete array2;
}

void FillArrays(vector<double> *&array1, vector<double> *&array2, OtherArgs &otherArgs)
{
    int size=GetSize(otherArgs);
    array1 = new vector<double>(size);
    array2 = new vector<double>(size);
    //Other code to fill the arrays
}

Thank you

  • 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-05T00:15:06+00:00Added an answer on June 5, 2026 at 12:15 am

    Here are a couple of reasons why the original sample is troublesome

    • The code leaks memory in the face of an exception because the delete calls aren’t protected
    • If FillArrays is passed a non-NULL vectory<double> value it will leak memory because it didn’t delete the previous value. It couldn’t reliably call delete even if it wanted to because the value may have been stack allocated.

    The easiest way to do this is to just declare the values on the stack and pass them by reference that way.

    int main()
    {
        vector<double> array1;
        vector<double> array2;
        OtherArgs otherArgs;
        FillArrays(array1,array2,otherArgs);
        //Do other stuff 
    }
    
    void FillArrays(vector<double> &array1, vector<double> &array2, OtherArgs &otherArgs)
    {
        int size=GetSize(otherArgs);
        //Other code to fill the arrays
    }
    

    The vector<T> will initialize themselves to an empty list when declared in this manner. The FillArrays method can then populate them as necessary.

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

Sidebar

Related Questions

I want to create a hash table that relies on an independent vector data
I want to create a vector out of a row of a data frame.
I want to create a vector of cv::Matrix in a c++ program and on
I have a std::vector. I want to create iterators representing a slice of that
I have a vector that I want to use to create a heap. I'm
i want create a custom json data from the mssql 2008 results so that
i want create Dynamic Slideshow in Jquery i'm Write this code var ctx =
I want to create a big std::vector so operator[] should receive long long rather
i have a std::vector, namely vector<vector<vector> > > mdata; i want pass data from
following is the structure and I want to create array of this structure in

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.