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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:43:40+00:00 2026-05-16T10:43:40+00:00

I am storing dynamically allocated class pointers in a vector like below. vector<Test *>

  • 0

I am storing dynamically allocated class pointers in a vector like below.

     vector<Test *> v;
    Test *t1 = new Test;
    Test *t2 = new Test;
    v.push_back(t1);
    v.push_back(t2);

Now, if i have to free Test objects, i have to loop through entire vector and free one by one and then do a clear.

for(int i = 0; i<v.size(); i++)
     {
         delete v[i];
     }
     v.clear();

Is there any function in vector to free all internal objects. That function should call Test class destructor for each object.
I understand that it is difficult for Vector class whether the pointer is address of a local object or dynamically allocated.
But still, whenever developer is confident that all are dynamically allocated he could have used freeing function if provided.

Although vector is treated as advanced Array, freeing objects in an array is much easier like below.

Test  *a = new Test[2];
delete []a;

Is there any function in vector to free all internal objects ?

  • 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-05-16T10:43:40+00:00Added an answer on May 16, 2026 at 10:43 am

    Your examples don’t do the same thing at all.

    If you want the vector equivalent of this array code:

    Test  *a = new Test[2];
    delete []a;
    

    it is simply

    std::vector<Test> a(2);
    

    If you have a vector of pointers to dynamically allocated objects, you need to delete every one of them, as in your example:

    for(int i = 0; i<v.size(); i++)
     {
         delete v[i];
     }
    

    but the same thing is true for an array:

    Test *t1 = new Test;
    Test *t2 = new Test;
    Test **a = new Test*[2];
    a[0] = t1;
    a[1] = t2;
    

    also has to be deleted with such a loop:

    for(int i = 0; i<2; i++)
     {
         delete a[i];
     }
    delete[] a;
    

    In the first case, you have two objects stored in a container of some sort. That container may be a vector, or it may be an array.

    In both cases, because the objects are stored directly in the container, their destructors are called when the container is destroyed. You don’t need to do anything to destroy or delete individual members, you just have to destroy the container (which is simpler with the stack-allocated vector, where you don’t even need to call delete[])

    But if your container stores pointers to dynamically allocated objects, then it is your responsibility to delete those objects at some point. And that is true whether your container is an array or a vector.

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

Sidebar

Related Questions

I have a vector declared as: vector<Curve> workingSet; Curve is a class I've created
I have to learn memory management in class and how to dynamically allocate memory
What happens when I have the following situation: class A: holds on to dynamically
I like to implement linked list in c++ ,while adding new node I dynamically
Okay, I have a multi-dimensional array which is statically-allocated. I'd very much like to
I read that pointers passed by malloc() & calloc() get allocated memory dynamically from
I'm dynamically storing information in the database depending on the request: // table, id
I'm having trouble looping through a set of dynamically created UITextFields and storing those
I have a need to build the data string dynamically. This is not working,
I am trying to dynamically display an image that I am storing in the

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.