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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:31:00+00:00 2026-06-08T03:31:00+00:00

Funky title, but honestly I couldn’t think of anyone better, sorry :( While experimenting

  • 0

Funky title, but honestly I couldn’t think of anyone better, sorry 🙁

While experimenting with pointers I came across this and I need help understanding it. Basically, I create a vector of a pointer to an object. When deleting the pointer that’s in the vector, I expect the original ovject to be deleted as well. They are one and the same no?

Here’s what I think I do in the code that follows I create a dynamically allocated array, I then put the pointer to half of the elements of the array in a vector of pointers. Each step of the way every object of the class testDestructor knows which ‘order’ it was created in, they all get assigned an incrementing number via a static integer.

I then pop_back and delete all the pointers in the vector. I only use pop_back because I wanted to see if they get deleted by the vector class, apperantly they don’t, but idk if I’m missing something there. The important bit is I delete it.

Now, what I expect to happen is that this deletes the corresponding elements of the array as well. This is what SHOULD happen, because the vector and the array element point to the same place in the memory. ie, the destructor should get called.

So when I then delete the array, I expect that either only 5 of the elements get deleted, or a run-time error occurs (I’ve had this happen before when I try deleting pointers that already are deleted, but that might have been a different scenario idk). Namely, I expect that only the destructor should only be called five times.

HOWEVER, that’s not what happens. The constructor gets called 10 times but the destructor gets called 15 times. What am I missing? Am I missing a constructor (I only know about default constructors and copy constructors, are there more), or is it something else? Because, in my mind, when the destructor is gone the object is gone.

Sorry if it’s too much code:

testDestructor.h:

#ifndef TESTDESTRUCTOR_H
#define TESTDESTRUCTOR_H

class testDestructor
{
public:
   testDestructor();
   testDestructor(const testDestructor &);
   ~testDestructor();

   static int maxTest;
   int test;
};

#endif

testDestructor.cpp:

#include "testDestructor.h"
#include <iostream>

using namespace std;

int testDestructor::maxTest = 0;

testDestructor::testDestructor()
{
   test = maxTest++;
   cout << "testDestructor nr " << test << " created successfully\n";
}

testDestructor::testDestructor(const testDestructor &destructorToCopy)
{
   test = maxTest++;
   cout<< "testDestructor nr " << test << " created successfully (using the copy constructor\n";
}
testDestructor::~testDestructor()
{
  //maxTest--;
   cout << "testDestructor " << test << " destroyed successfully\n";
}

main.cpp:

#include <iostream>
#include "testDestructor.h"
#include <vector>
using namespace std;

int main()
{
   cout << "   creating pointer array:\n\n";
   testDestructor *testPtr = new testDestructor[10];

   cout << "   intitiating vector\n\n";
   vector<testDestructor*> testVct;


   for (int i = 0; i < 5; i++)
   {

      cout << "   pushing back vector " << i << ":\n";
      testVct.push_back(testPtr + i);
      cout << "testDestructor " << testVct[i]->test << " pushed back\n";
   }
   cout << "\n";

   for (int i = 4; i >= 0; i--)
   {
      cout << "   popping back vector " << i << ":\n";
      cout << "testDestructor " << testVct[i]->test << " popped back\n";

      delete testVct[i];
      testVct.pop_back();

   }
   cout << "\n";

   cout << "   deleting pointer array\n\n";
   delete [] testPtr;
}
  • 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-08T03:31:02+00:00Added an answer on June 8, 2026 at 3:31 am

    When deleting the pointer that’s in the vector, I expect the original
    ovject to be deleted as well. They are one and the same no?

    No, they’re not the same, and your terminology “deleting the pointer” implies you need to go and re-read some basic stuff about pointers in C++.

    new creates things, and returns a pointer to what it creates. delete destroys things and is passed a pointer to the thing to destroy. The pointer is unchanged – it just doesn’t point anywhere useful following a delete.

    What you’re trying to do is delete individual objects in an array of objects created with new[]. That simply cannot be done. You can only delete the whole lot with delete[]. If you want to have individually deleteable objects, use a std::vector<TestDestructor>.

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

Sidebar

Related Questions

Okay, I know that is a funky-sounding title, but I couldn't think of a
sorry about the funky title, I was having trouble coming up with one. First
sorry for the title but I'm not sure how I should call it. I'm
Sorry for the vague title, but not quite sure how to summarize this one.
I know the title sounds funny, but I found this snippet somewhere: my MyPackage
Extremly confusing title, i know. Hello, i am facing some funky issues here. I
Sorry for the funny title. Prior to C++0x, there are restrictions in the use
I know the title may be funny for you but I am actually facing
I'm doing some funky authentication work (and yes, I know, open-id is awesome, but
I'm trying to do accented character replacement in PHP but get funky results, my

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.