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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:17:58+00:00 2026-05-26T03:17:58+00:00

template<typename T> someclass<T>& operator=(const someclass<T>& other) { typename std::vector<T *>::const_iterator rhs; typename std::vector<T *>::iterator

  • 0
template<typename T>
someclass<T>& operator=(const someclass<T>& other)
{
    typename std::vector<T *>::const_iterator rhs;
    typename std::vector<T *>::iterator lhs;

    //identity test
    //this->data is std::vector<T *>

    for(lhs = this->data.begin(); lhs != this->data.end(); lhs++)
    {
        delete *lhs;
    }

    this->data.clear(); // this is what I forgot

    this->data.reserve(other.data.size());
    for (rhs = other.data.begin(); rhs != other.data.end(); rhs++)
    {
        if (NULL == *rhs)
        {
            this->data.push_back(NULL);
        }
        else
        {
            this->data.push_back(new T(**rhs));
        }
    }
}

As you can see in the comments, I forgot to clear out the old pointers in the array. When I invoked the assignment operator for the second time, I got glibc error complaining about double free. The only information provided was the deleted address.

This make me thinking about what to do with such class of deleted pointers – when you don’t want to delete them again, and when you do, it is certainly an error. You cannot set them to NULL, because another delete would be correct then. You don’t want to keep the value as the memory location can be assigned to newly created object.

What would be good for debugging is some value, like INVALID, which you assign to these pointers saying “invoking delete on this pointer is an error”, instead of NULL, which say “invoking delete on this pointer does nothing”. Is there something like this?

  • 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-26T03:17:59+00:00Added an answer on May 26, 2026 at 3:17 am

    The solution to this problem is to write code that does not contain any deletes. Use shared_ptr where possible. When you have a container that owns polymorphic objects, you can also use Pointer Container.

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

Sidebar

Related Questions

Consider this template function: template<typename ReturnT> ReturnT foo(const std::function<ReturnT ()>& fun) { return fun();
template<typename T> class ClassVariantVisitor : public boost::static_visitor<T> { public: T operator()(int& i) const {
template<typename ForwardIterator, typename StringType> inline bool starts_with(ForwardIterator begin, ForwardIterator end, const StringType& target) {
There is such code: template <class T> class SomeClass{ typedef boost::shared_ptr<T> sPtr; typedef std::vector<sPtr>
template<typename T, size_t n> size_t array_size(const T (&)[n]) { return n; } The part
template <typename T> int custom_memcmp(const T* a, const T* b, std::size_t n); Would this
template<typename T> class A // template parameterization { private: T t; A(const T& v)
template <typename T> void f(T&) {} template <typename T> void f(T&&) {} int main()
Consider: template <typename Function, typename ...Args> auto wrapper(Function&& f, Args&&... args) -> decltype(f(args...)) {
Please consider this code: template<typename T> char (&f(T[1]))[1]; template<typename T> char (&f(...))[2]; int main()

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.