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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:13:33+00:00 2026-05-30T16:13:33+00:00

This is not a practical question but it is only for educational curiosity. In

  • 0

This is not a practical question but it is only for educational curiosity.

In some forum I’ve just found this piece of code:

std::vector<MyClass*> myvec;
for(unsigned int i = 0; i < 100; ++i) {
  myvec.push_back(new MyClass( foo1 ));
}

// somewhere in the code inside a particular if statement
MyClass* replacement = new MyClass( foo2 );
delete myvec[0];
myvec[0] = replacement;

I have a vector of instances of MyClass and somewhere in the code I have to do substitution of some instances with others.

Why do I have to call the delete? Is replacing the pointer not enough?

WHAT I’VE LEARNED:

  • there are some devil outside called memory leaks @larsmans answer
  • “for each call to new you should have a corresponding call to delete somewhere in the code.” @Dirk answer
  • I can use shared pointer.. @CashCow answer
  • I can use valgrind
  • 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-30T16:13:35+00:00Added an answer on May 30, 2026 at 4:13 pm

    A standard container of pointers will never delete the target of any pointers that it contains. It has no way of knowing that they point to objects created with new – they could have been created with new[] (in which case delete[] is needed), or they could be pointers to static or automatic objects, which must not be deleted, or there may be something else responsible for deleting them.

    Usually, you would store objects in a container, rather than pointers. If you really need pointers (perhaps because the objects need to be different types), and you really need those pointers to manage the object lifetime, consider using a smart pointer such as std::unique_ptr, which automatically deletes its target, or boost::ptr_vector if that is not available.

    Otherwise, if you really must use raw pointers to manage object lifetimes, you’ll have to be careful to use delete yourself at the right time. In that case, a tool such as Valgrind can help to identify the inevitable memory leaks.

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

Sidebar

Related Questions

I know this sort of code is not best practice, but nevertheless in certain
Sorry for this not being a real question, but Sometime back i remember seeing
I realize that this question is impossible to answer absolutely, but I'm only after
This may not be a git specific question, but it comes up in the
Ok I know this practically sounds elementary, but I don't understand what I'm not
First of all I alwyas use one #ID. just asking this question to know
This is a very standard newbie question, but I am looking for an expert
This is not exactly a technical question, since I know C kind of enough
I recently learned that Unicode is permitted within Java source code not only as
I understand that this question may seem somewhat ungrounded, but if someone knows anything

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.