Thread: vector destruction and malloc

  1. #1
    Registered User
    Join Date
    Jun 2008
    Location
    Toronto, Ontario, Canada
    Posts
    17

    vector destruction and malloc

    I'm learning how to code with vectors. I wrote a few lines to try out the vector destructor. I thought it was simple enough, but the code generates a runtime error that refers to memory allocation. It seems to be saying that memory allocated to the vector had already been freed before I called the destructor.

    Can you tell me what I'm doing wrong? I compiled it using gcc 4.0.1 on a MacPro running OS 10.5.3. Below is the code and the output.

    Thanks!
    Mike

    Code:
    #include <vector>
    
    using namespace std;
    
    int main() 
    {
    	vector<int> data;
    	
    	data.push_back(1);
    	data.~vector<int>();
    
    	return 0;
    }
    OUTPUT
    a.out(11915) malloc: *** error for object 0x100150: double free
    *** set a breakpoint in malloc_error_break to debug

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The variable data is destroyed when it goes out of scope. Things you didn't allocate (such as data) you shouldn't destroy.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    tabstop tells you what is wrong, but I thought I'd try to explain what actually happens:
    When you explicitly call the destructor for vector<int>, it goes and deallocates the internal data that the vector allocated to store your data. Then, when main reaches the return 0 and the end of the function, it automatically destroys the vector AGAIN. This means that the same data is now deallocated again - this is why it says "double free".

    There are VERY few places where you legally call a destructor explicitly - the only place I can think of is when you write your own new/delete functions that are not actually called new and delete.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Do not call the destructor on the vector.
    It will then destruct twice, causing errors.
    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
    Jun 2008
    Location
    Toronto, Ontario, Canada
    Posts
    17
    Okay, I see what you mean. What if data were a member of a class? Would I need to explicitly destroy it within the class destructor? For example, would I need to include a call to the vector destructor in my class destructor, like the following?

    Code:
    class Myclass
    {
        public:
          Myclass();
          ~Myclass();
          void insert(int n);
    
        private:
            vector<int> data;
    };
    
    Myclass::Myclass()
    {
        data.reserve(10);
    }
    Myclass::~Myclass()
    {
         data.~vector<int>();
    }
    void Myclass::insert(int n) 
    {
         data.push_back(n);
    }

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The vector data will go out of scope on its own when the object is destroyed.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Would I need to explicitly destroy it within the class destructor?

    No. You will likely never need to explicitly call a destructor. The compiler calls it for you.

    A vector<int> is an object like an int or string. It gets destroyed when it goes out of scope. You don't call ~int, right? So you wouldn't call ~vector<int> either.

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Until you learn a lot more about C++, NEVER explicitly call a destructor. I've been using C++ for almost a decade and I still haven't had a need to call a destructor (but there are a few very rare cases when you do need to).

  9. #9
    Registered User
    Join Date
    Jun 2008
    Location
    Toronto, Ontario, Canada
    Posts
    17
    Okay, thanks for everyone's advice!
    Mike

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    ~Thread();

  11. #11
    Registered User
    Join Date
    Jun 2008
    Location
    Toronto, Ontario, Canada
    Posts
    17
    Have you certain books you'd like to recommend? I've been reading the following:

    Effective C++, 3rd edition, by Scott Meyers (2005)
    C++ Cookbook, by D.R. Stephens, C. Diggins, J. Turkanis, and J. Cogswell (2005)
    The C++ Standard Library, by Nicolai M. Josuttis (1999)
    Absolute C++, 3rd edition, by Walter Savitch (2007)

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You can check out the stickied Book Recommendations thread. I'd recommend Accelerated C++ as a beginner book over Absolute C++.

    The others on your list are good but aren't exactly "teach the language" type books.

  13. #13
    Registered User
    Join Date
    Jun 2008
    Location
    Toronto, Ontario, Canada
    Posts
    17
    Thanks for the recommendation.

    Like many in the life sciences, I've had one semester in C++ and the rest of my education has come from reading, working with other scientists, and working in the same lab with a few software engineers.

    So I'm still a novice, with just enough knowledge to get into trouble!

    Mike

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 PM
  2. the basics of malloc
    By nakedBallerina in forum C Programming
    Replies: 21
    Last Post: 05-20-2008, 02:32 AM
  3. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 12:26 PM
  4. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  5. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 09:08 AM