Thread: Freeing memory from vector of structs

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    43

    Freeing memory from vector of structs

    After I performed some operations on the vector of structs, I need to free the memory. I suppose clear() will not be sufficient, but I'm not sure how to perform individual delete.
    Code:
    typedef std::vector<VertexRAM> VecVertexRAM; // definition of a vector of structs
    
    template <typename type>
    void freeFromMemory(std::vector<type>& myVec) {
        typename std::vector<type>::iterator myIter=myVec.begin();
        while(myIter!=myVec.end()) {
    	delete(*myIter);
    	++myIter;
        }
        myVec.clear();
    }
    This outputs the following error:
    Code:
    error: type ‘struct VertexRAM’ argument given to ‘delete’, expected pointer
    Any ideas on how to do this? Thanks

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Apparently you aren't storing pointers in the first place, so you have no need to delete.
    And if you would be storing pointers, you should be using smart pointers such as std::shared_ptr.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    43
    Thanks. So, what is your suggestion to free the memory from std::vector<VertexRAM> (which is a vector of structs)?
    Small code sample might help.
    What do you think of the following (bottom of the page):
    Does std::vector.clear() do delete (free memory) on each element? - Stack Overflow

    Thanks

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Since you have no pointers, there is nothing to free. Let the vector go out of scope. That's it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    43
    I monitor RAM usage, and I simply need to free the memory in the middle of the function (after I'm finished with vector of structs). Is that possible?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Make the vector go out of scope. As it should, after you're done with it. All memory should be freed.
    (Note that the OS may choose whether or not to "actually" return the memory; it might still say it's used.)
    Also, you say you monitor it. Does that mean you're using a lot of memory (say more than 1 MB, the limit of the stack)?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    43
    Here is the thing.
    In a specific function, I need to perform operation on std::vector<VectorRAM>. Obviously, this vector consumes much memory. After I finish operation with this vector, I need to perform some 'other operations' which would consume additional memory (and, therefore, I would need to free it).
    If I understand your suggestion correctly, I should split the function in 2. Would this really free my memory? Yes, if I make variables that I need for the 'other operations' external to the first function.
    I wonder in what way to do it within the function.
    Thanks

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You don't need to split it. You can introduce a local scope.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Yes, this would free a memory.

    Otherwise, there is no guaranteed way to force a vector to deallocate its memory (e.g if you clear() it, it still keeps its memory around. A trick that should work is to swap the vector with an empty one:

    Code:
    std::vector<XXX>().swap(my_vec);
    However, using smaller functions might be a better idea.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  10. #10
    Registered User
    Join Date
    Mar 2010
    Posts
    43
    Note that the input std::vector<VertexRAM> is not internal to the function (it is passed as a parameter from another function). I'm not sure how to introduce a local scope. Perhaps a small example might help (provided the assumption from above).

    As for the swapping, if the two vectors are 'exchanged', what would then be memory consumption of the previously empty one? This would take the role of the first in consuming memory. (correct me if I'm wrong)

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Obviously the function that has the vector local is the one that controls its lifetime. So it should call all the functions that needs the vector, then make it go out of scope.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Mar 2010
    Posts
    43
    I found the following (swapping contents) on:
    A Beginner's Tutorial For std::vector, Part 1 - CodeGuru
    Code:
    std::vector<int> v;
    //...
    v.clear();
    v.swap(std::vector<int>(v));
    I tried to adjust it to my case:
    Code:
         vertices.clear();
         VecVertexRAM vert;
         vertices.swap(vert);
    However, system monitoring shows no decrease in the RAM memory consumption after the lines above are executed. You could help by providing small code samples that I might test. Thanks

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    How about just
    Code:
    {
        std::vector<int> v;
        //...
    }
    Boom! Vector out of scope. Memory released.
    As far as we're concerned, at least.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by onako View Post
    However, system monitoring shows no decrease in the RAM memory consumption after the lines above are executed. You could help by providing small code samples that I might test. Thanks
    There is no guarantee that when you release memory the system actually releases memory. The "free" memory just goes back to the heap where it can be allocated again.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  15. #15
    Registered User
    Join Date
    Mar 2010
    Posts
    43
    Quote Originally Posted by brewbuck View Post
    There is no guarantee that when you release memory the system actually releases memory. The "free" memory just goes back to the heap where it can be allocated again.
    Do you think that THE ABOVE might the explanation to the following: Even by monitoring RAM consumption (using System Monitor in Ubuntu), after I free the memory consumed by vector (by some of the methods described above, such as swapping, for example) I see no decrease in RAM memory consumption (actually, sometimes there is a large drop, sometimes very little drop, and sometimes no drop at all). This is why I want to have really free memory. Is it somehow possible?
    Thanks for your patience.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structs, dynamic memory, and phone book entries
    By wkfcs in forum C Programming
    Replies: 5
    Last Post: 10-09-2009, 03:57 PM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. Doubt about freeing memory
    By guillermoh in forum C Programming
    Replies: 9
    Last Post: 02-04-2008, 05:26 PM
  4. Freeing memory necessary?
    By Snip in forum C Programming
    Replies: 3
    Last Post: 11-05-2005, 07:01 AM
  5. Allcoating memory and not freeing it
    By lambs4 in forum C Programming
    Replies: 1
    Last Post: 09-05-2001, 11:32 AM