Vector of pointers

This is a discussion on Vector of pointers within the Windows Programming forums, part of the Platform Specific Boards category; Hey, just a quick question: If I have a vector of pointers to objects of a class on the heap, ...

  1. #1
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879

    Vector of pointers

    Hey, just a quick question:

    If I have a vector of pointers to objects of a class on the heap, then when the vector is destroyed, will the objects' destructors be called and then the objects' memory released back to the heap? Or do I have to use delete on the pointers or something?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    You have to delete them yourself. Loop through the vector and delete each.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Ok, thanks.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Hey... are you sure about that? I took your advice and when removing stuff from my vectors, I added "delete (vector's name)[(index)]" and now my program is mysteriously causing "assertion errors" (MSVC++ 6), and when I hit "retry" ("press retry to debug the application") it tells me I get an illegal operation (I think that's part of the debugging thing), and when I click on details, it says that my prog caused an "invalid page fault".

    So are you absolutely sure (as in, very very absolutely) that I need to do the delete thing and that I'm doing something else wrong?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    yes. there is something else wrong. Are you sure that all of the pointers in the vector have memory allocated to them? if you call delete on one that is not valid you'll see obvious problems. A nice way of handling it is initializing all to zero when there isn't memory in it. Then check before you delete.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Ok, thanks I actually just noticed that it only crashed in a few functions, most of which were called by the destructor. I'll work on it from there, I guess (I think they should all have memory in them, unless they were delete twice...)
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MergeSort with array of pointers
    By lionheart in forum C Programming
    Replies: 18
    Last Post: 08-01-2008, 10:23 AM
  2. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21