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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:38:41+00:00 2026-06-07T22:38:41+00:00

I have a vector of pointers to vectors: main(…) { //… std::vector< std::vector<double> *

  • 0

I have a vector of pointers to vectors:

main(...)
{
  //...
  std::vector< std::vector<double> * > ds = getDS(...)
  //...
}

std::vector<std::vector<double> * > getDS(int m, ...)
{
  std::vector<std::vector<double> * > wavefunctions = *(new std::vector<std::vector<double>*>(m));
  int n = int( params.rmax() / params.dr() );
  std::ifstream input_wf;
  input_wf.open(filename.c_str());
  input_wf.setf(std::ios::showpoint | std::ios::scientific);
  for(int i=0; i < nbasis; i++)
  {
    std::vector<double> *wf = new std::vector<double>(n);
    //(wavefunctions[i]) = new std::vector<double>(n);
    for (unsigned int ir=0; ir < wf->size(); ir++)
      input_wf >> ( *wf )[ir];
    wavefunctions.push_back(wf);
  }
  input_wf.close();
  return wave functions;
}

However, I keep getting a EXC_BAD_ACCESS error when I try to access wavefunctions[0]->at(some legal value) after going through the loop once, during debugging. (There should be something there, but I’m not sure why there isn’t… Any ideas?

  • 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-07T22:38:44+00:00Added an answer on June 7, 2026 at 10:38 pm

    The following line,

      std::vector<std::vector<double> * > wavefunctions = *(new std::vector<std::vector<double>*>(m));
    

    is problematic in your case for two reasons –

    1. It causes a memory leak, as the object created by new is copied into wavefunctions, and then the pointer to it is lost. This is not Java…
    2. It allocates m entries in your vector. Subsequent push_back‘s add to that m entries, so when you try to access wavefunctions[0] you actually access an entry which was created in this line, not the first one to be pushed in the for loop.

    To solve the problem, change the line to

    std::vector<std::vector<double> * > wavefunctions;
    wavefunctions.reserve(m);
    

    The reserve method makes sure you will not have reallocations during the push_back‘s.

    As a last note, depending on the circumstenses, the compiler may or may not be able to optimize away the inherent copy of vectors that is performed on return from the function. To be sure, you might want to learn more about r-value references (&&) or simply return the vector by address (that is, as another parameter of type vector<...> * and return type void).

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

Sidebar

Related Questions

I have a problem creating a std::map<int, int> from a vector of pointers, called
I have a vector of pointers to class objects. These class objects invoke new
I have a vector for storing the pointers of all objects (created by new
I have a vector of pointers to a class. I need to call their
I have a vector of pointers to objects. I'd like to remove objects from
I have a vector of pointers. I would like to call a function for
I have a vector of pointers to Mouse objects called 'mice'. I'm passing the
I have a vector that holds pointers to abstract type Rock : vector<Rock*> rocks;
I have a class with a vector of pointers to objects. I've introduced some
I have a vector of Linked list pointers. Each LinkedList has a head pointer

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.