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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:06:40+00:00 2026-06-15T01:06:40+00:00

I haven’t been working with C++ for a long time and now I need

  • 0

I haven’t been working with C++ for a long time and now I need to do some small “project”.
I reviewed few topics trying to find solution for my problem with no results…

Here is “pseudo-code” (Pair is my class):

Pair** pairs;

I get size of table from input and create table of pointers:

pairs = new Pair*[size];

I have a loop which creates some object and puts it’s reference into table of pointers. More or less:

while (...)
{
    Pair pair(...); // calling constructor, creating object type of Pair.
    pairs[i] = &pair;
    i++;
}

The problem is that &pair is the same number everytime (every step of loop).
Let’s say the address of first created object is 1234.
So with every step in loop it overrides this object at 1234. So each pointer in pairs points to the same address -> same object.
I would like to force creating this object at new place in memory.

I’ve been trying to put those objects into other table and then pass their reference to table of pointers:

Pair* clearPairs = new Pair[size];
while (...)
{
    clearPairs[i] = Pair(...);
    pairs[i] = &clearPairs[i];
    i++;
}

But problem still occurs.

Any tips?, mistakes/errors in my code (or thinking)? Shall I implement some “copying constructor”?

  • 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-15T01:06:42+00:00Added an answer on June 15, 2026 at 1:06 am
    while (...)
    {
        Pair pair(...); // calling constructor, creating object type of Pair.
        pairs[i] = &pair;
        i++;
    }
    

    pair is allocated with automatic storage duration and goes out of scope upon each iteration of the loop. You are saving a pointer to an invalid object (a bunch of them).

    You should use a collection of smart pointers if you really need pointers (this is true when objects cannot be copied or copies are expensive), which may not be necessary at all. Try this instead:

    vector<Pair> pairs;
    // and if you need pointers...
    vector<unique_ptr<Pair>> pairs;
    
    while(whatever) {
        pairs.push_back(Pair(...));
        // or...
        pairs.push_back(unique_ptr<Pair>(new Pair(...)));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I haven't been able to get this working and all of the sample code
Haven't been programming in JS for a while. Now, I have following thing: <html>
Haven't done ASP.NET development since VS 2003, so I'd like to save some time
Haven't been coding in a bit, but took on a project for a friend.
I haven't programmed in a few years and I'm creating a small quiz to
I haven't designed a website for about 3 years now, so I am quite
I have a jquery bug and I've been looking for hours now, I can't
Haven't done as much of Javascript as I should have done. I am trying
Haven't touch javascript for 3 years. Just got a javascript project and wanted to
I haven't been able to find a tutorial on how to properly setup a

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.