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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:35:10+00:00 2026-06-05T05:35:10+00:00

I create a vector of pointers to double array. Are these pointers valid after

  • 0

I create a vector of pointers to double array. Are these pointers valid after function (i.e. InsertIntoVector) exits? If I later fetch the pointer as in GetVecElement, are the pointers still guaranteed to point to the same memory location they were assigned?

class A { 
   vector<double*> vec; 

   void insertIntoVector(double x, double y); 
   void GetVecElement(int i, double& x, double& y); 
}; 

A::insertIntoVector(double x, double y) { 
   double* xy = new double[2]; 
   xy[0] = x; xy[1] = y; 
   vec.push_back(xy);       
}

A::GetVecElement(int i, double& x, double& y)
{
    x = vec[i][0];       // will the reference to the double array still be valid? 
    y = vec[i][1];
}
  • 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-05T05:35:12+00:00Added an answer on June 5, 2026 at 5:35 am

    Those are not references! Those are pointers…

    If I later fetch the pointer as in GetVecElement, are the references still guaranteed to point to the same memory location they were assigned?

    You are not deleting the pointer, and std::vector won’t do it for you, so yes the pointers will remain valid. It will also be leaked unless you manually delete each of them at A‘s destructor. But for that you will need a copy-constructor and an assign-operator as well, to clone the contents of the vector.

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

Sidebar

Related Questions

I am trying to create a vector that contains pointers, each pointer points to
I need to create a vector which holds the pointers to some static objects
Have a homework assignment in which I'm supposed to create a vector of pointers
So I have a vector of pointers holding a baseclass. I create two elements
I would like to create something like a pointer to a 2 dimensional array
Ok so what I'm trying to do is create an array of pointers that
Im allocating memory to a double pointer in another function, therefore I need to
I am trying to create a custom sort for a vector of class pointers
I have a vector for storing the pointers of all objects (created by new
How do I create a vector of Sprites using SFML and vector header? I

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.