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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:58:59+00:00 2026-06-12T09:58:59+00:00

I have a class called Foo which has a member that is a pointer

  • 0

I have a class called Foo which has a member that is a pointer to a vector of pointers to another class called Bar. I initialise it in the constructor but I’m not sure how to deallocate it in the destructor. I’m still learning. Would appreciate your help. The reason for having this member is so that the scope persists beyond that method i.e. beyond the stack. Thanks.

#include <iostream>
#include <vector>

using namespace std;

class Bar {};

class Foo {
public:
    Foo() {
        bars = new vector<Bar*>*[10];

        for (int i = 0; i < 10; i++) {
            bars[i]->push_back(new Bar());
        }
    }

    ~Foo () {
        for (int i = 0; i < 10; i++) {
            // TODO: how to clean up bars properly?
        }
    }

private:
    vector<Bar*>** bars;
};

int main () {
    new Foo();
    return 0;
}

Update: I appreciate the feedback on all fronts. I’m new to C and C++. Basically I wanted a 2d structure as a class member that would persist for the lifetime of the class. The reason the outer structure is an array is because I know how big it needs to be. Otherwise I was previously using vector of vectors.

  • 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-12T09:59:00+00:00Added an answer on June 12, 2026 at 9:59 am

    The number of pointers is a bit ridiculous, as all they are doing is causing confusion and leaks, as evident from non-proper initialization and the question’s title. You don’t actually need any pointers at all, and don’t have to worry about any cleanup.

    For a 2D array with the first dimension passed into the constructor, you can use a vector of vectors:

    std::vector<std::vector<Bar>> bars; 
    

    To initialize the outer vector with the passed in size, use an initializer:

    Foo(size_t size) 
        : bars(size) {}
    

    When the object is destroyed, bars and all of it elements are as well, so there’s no chance of forgetting to clean up or doing so improperly.

    If performance is an issue, this can be translated into a sort of Matrix2D class that acts like a 2D array, but really only has an underlying 1D array.

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

Sidebar

Related Questions

I have a class called Flamethrower which naturally has its own ammunition that is
I have a base class Foo that has an Update() function, which I want
My situation is essentially this: I have a class called Foo which has dependencies
I have a class called Property which has nothing but get -methods. All the
I have object called Foo. Right now it implements IFoo which has a lot
I have a model called Foo which has a property called MyProp of type
I have an entity on the server called foo which has a list of
I have a class Foo, which has a (simple) destructor. Some other class contains
I have a class called: ComplexNumber and I have a string that I need
I have a class called Trial which has_many results. Now What I want to

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.