Results 1 to 22 of 22

Thread: deconstructing with a vector

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    183

    deconstructing with a vector

    I have created a vector of objects that I am constantly changing, but I want to have a deconstructor to deallocate a string in each of the objects when I am done using the vector. I wrote a deconstructor but I noticed that it is getting called all over the place- even when I am not done using the memory. Is there something special about vectors that can account for this, and if so how do I deallocate the memory used in the string for each object in a vector?
    Any suggestions would be great!

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    The destructor will be called every time the object is destroyed, but the vector will be doing its own internal memory management, causing your objects to be constructed/destructed more than you expect.

    If you have a copy-constructor (operator=) then you can make sure that it's all kept safe
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3
    BTW, destructors never take arguments and follow the format void ~classname().

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    They can't return values either.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    183

    hummm

    some of that makes sense to me, I can understand how the vector controls its own state as it needs to recopy itself each time its size changes, but are you saying then that I shouldn't worry about deallocating at all? What about when I am finished with the vector, how will I deallocate- or do I need to?

  6. #6
    Theoretically, in most cases, unless you are using pointers, you should never have to make a destructor.

  7. #7
    Originally posted by parksie
    They can't return values either.
    That's why I said "void".

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    No, it doesn't even have a return type
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  9. #9
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    Deconstructors have ~ to show that that they dont have any return type (yes Parskie, Not even void)
    Baaaaaaaaah

  10. #10
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Quibbling again

    Destructors have ~ because that's how you tell it's a destructor and not a constructor. Destructors take no arguments whatsoever (other than the implicit this), and return no values.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  11. #11
    Originally posted by abdul
    Deconstructors have ~ to show that that they dont have any return type (yes Parskie, Not even void)
    Oh, you're right. I was saying void meaning it didn't return anything, but then void is kinda different.

  12. #12
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    Is there anything else than Deconstructor that does not return anything - not even void ?
    Baaaaaaaaah

  13. #13
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Other than the destructor, only constructors cannot return values.

    Well, actually some functions cannot return values because they get interrupted before they reach their exit code (or none in the case of naked functions, which I haven't found much of a use for yet).
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    183

    hummm

    the objects in my vector do pointer elements in them, so it seems to me that they would need to be deallocated when they are finished being used... correct?

  15. #15
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Yes, in the destructor you deallocate all memory allocated in the class. Note you don't deallocate objects that was passed to it (that's undermining object orientation) On the pointers holding them you do
    delete varname

    or

    delete[] varname

    if varname is a pointer to an array.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  16. #16
    You might want to use the apvector class. It is templated and works fine for me. (I can't believe I'm actually recommending that AP crap )

    http://www.collegeboard.org/ap/compu...l/classes.html

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    183

    k but....

    THis all being said I am brought back to my original question then... If I have a vector of objects that have as a component say a *char I need to deallocate the memory used by this *char if I use the new operation to allocate memory. But, as the vector is constantly allocating and deallocating to increase its size, how do I perform a final deallocation once I am finished using the vector?

  18. #18
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    Is the vector on the heap?

    if your vector was newed from the beginning you can deallocate it with delete. If not it will be deallocated when it runs out of scope. The vector will of course delete it's objects before it deletes itself, that is handled in the vectors destructor.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  19. #19

  20. #20
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Hey! It's not as bad as it looks Once you get how it is and the benefits of them you can screw Java and be happy
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  21. #21
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169

    Re: k but....

    Originally posted by bob323
    THis all being said I am brought back to my original question then... If I have a vector of objects that have as a component say a *char I need to deallocate the memory used by this *char if I use the new operation to allocate memory. But, as the vector is constantly allocating and deallocating to increase its size, how do I perform a final deallocation once I am finished using the vector?
    Code:
    for(i = 0; i < 5; i++) {
        char *ptr = new char[10 * i];
    
        myvector.push_back(ptr);
    }
    
    // Use the vector, which will handle allocating the variables
    // holding the pointers, not your data itself
    
    for(i = 0; i < 5; i++) {
        delete[] myvector[i];
    }
    myvector.clear(); // Remove invalid pointers
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  22. #22
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Hmm, if that's how it is, you should use a string class that knows how to deallocate itself. If you like lowlevel stuff it's of course ok but otherways it's against object orientation in terms of encapsulation.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width