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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:13:01+00:00 2026-06-14T03:13:01+00:00

Possible Duplicate: How to correctly deallocate or delete a c++ vector? I’m having some

  • 0

Possible Duplicate:
How to correctly deallocate or delete a c++ vector?

I’m having some problems trying to delete memory that I’ve allocated in a vector. Even though I call list.clear(), it’s not deallocating the memory.

So I have some code like this in a template-based class called Set

template <class T>
class Set {
public:
    // stuff
private:
    int size;
    std::vector<T> list;
};

And in the constructor, I have allocated memory for the vector. So I call list = new std::vector;

For your interest, here is my copy constructor and assignment operator that I’ve also witten where I also allocate memory for the vector:

template <class T>
Set<T>::Set(const Set& aSet)
{
    size = aSet.size;
    list->clear();
    list = new vector<T>;
    for (int i = 0; i < size; ++i) {
        list[i] = aSet.list[i];
    }
}

template <class T>
Set<T>& Set<T>::operator=(const Set& right)
{
    if (this != &right) {
        list->clear();
        size = right.size;
        list = new vector<T>;
        for (int i = 0; i < size; ++i) {
            list[i] = right.list[i];
        }
    }
    return (*this);
}

In the destructor, I just have list.clear() to delete all elements and then deallocate the memory.

But the problem is, when I run valgrind on my .out file, it’s telling me that I’ve definitely lost some memory and I don’t know why it’s telling me this. I’ve read some questions here on Stackoverflow but I’ve basically tried everything. I tried clear() and then delete on the vector but that didn’t work. I then tried to erase(list.begin(), list.end()) but that didn’t work too.

My thought process is that I’m using a Set *aSet = new Set; in my main class and since an int is not an object, it’s not being freed when I call list.clear(). Is this right? How would I go about deleting the memory correctly?

Thanks for any help.

Edit1 = changed list* to setList

My new constructors and the assignment operator:

template <class T>
Set<T>::Set(const Set& aSet)
{
    size = aSet.size;
    setList.clear();
    setList = aSet.setList;
}

template <class T>
Set<T>& Set<T>::operator=(const Set& right)
{
    if (this != &right) {
        setList.clear();
        size = right.size;
        setList = right.setList;
    }
    return (*this);
}

Valgrind still reports I have the same amount of lost memory though. In my destructor I still have list.clear()

Valgrind log:

==11398== 
==11398== HEAP SUMMARY:
==11398==     in use at exit: 62,969 bytes in 352 blocks
==11398==   total heap usage: 540 allocs, 188 frees, 68,046 bytes allocated
==11398== 
==11398== LEAK SUMMARY:
==11398==    definitely lost: 8,624 bytes in 14 blocks
==11398==    indirectly lost: 1,168 bytes in 5 blocks
==11398==      possibly lost: 4,829 bytes in 56 blocks
==11398==    still reachable: 48,348 bytes in 277 blocks
==11398==         suppressed: 0 bytes in 0 blocks
==11398== Rerun with --leak-check=full to see details of leaked memory
  • 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-14T03:13:03+00:00Added an answer on June 14, 2026 at 3:13 am

    My thought process is that I’m using a Set *aSet = new Set; in my main class and since an int is not an object, it’s not being freed when I call list.clear(). Is this right? How would I go about deleting the memory correctly?

    No. To delete the memory you allocate correctly you need to call delete:

    Set *aSet = new Set;
    
    delete aSet;
    

    However manually managing memory like this is difficult and error prone. You should prefer alternatives. The first is that you should not use dynamic allocation at all. You should simply use automatic variables:

    Set aSet;
    // no delete required. Variable destroyed/deallocated when it goes out of scope.
    

    If you really do need dynamic allocation you should use smart pointers.

    std::unique_ptr<Set> aSet(new aSet);
    

    Smart pointers implement RAII for dynamic allocation so you don’t have to do it manually.

    In some rare circumstances you may actually need to do dynamic allocation manually, but that’s an advance topic.


    std::vector<T>::clear() is not required to deallocate the vector’s memory. You can use the C++11 member function shrink_to_fit(), or you can use the swap trick:

    std::vector<int> list;
    
    ...
    
    std::vector<int>(list).swap(list);
    

    Also you really shouldn’t be using a pointer to a vector. A vector uses RAII to manage dynamic memory for you. When you use a pointer to a vector you no longer have the benefit of not manually managing the resource yourself.

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

Sidebar

Related Questions

Possible Duplicate: SmartGWT Dialog title not set correctly I have a problem, is that
Possible Duplicate: Android : Location.distanceTo not working correctly? I am trying to calculate the
Possible Duplicate: How to correctly invoke a function that maps a SProc to ObservableCollection
Possible Duplicate: How to correctly unregister an event handler MSDN states the following two
Possible Duplicate: Programmatically delete my own app Currently I am working on a iphone
Possible Duplicate: Unicode in C++ If I remembered correctly, the default character and string
Possible Duplicate: Space Before Closing Slash? Hi, If I remember correctly we have it
Possible Duplicate: file_put_contents permission denied I have recently transferred server, and it seems that
Possible Duplicate: before_filter :require_owner I am trying to restrict access to certain actions using
Possible Duplicate: How do I correctly set up, access, and free a multidimensional array

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.