Thread: iterators heap or stack

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    450

    iterators heap or stack

    I want to return an iterator for a data type for a function and wondering if it will be destroyed when the function goes out of scope.

    Are iterators created in free store the heap or on the stack.

    I may be a little confused it is not necessary to destroy a pointer correct just the object it points to. So if you use new you must use delete to destroy the object though you use the pointer name. Is this why some suggest assigning the pointer to null after delete.

    I am guessing scope rules apply normally to pointers (hence they are stored on the stack) but the objects pointed are still on the heap so you can loose the ability to delete the object if you do not return a refrence to the pointer correct.
    If not how do you destroy the actual pointer.

    Also I asked somtime ago if container.clear() calls the destructor for the objects within the container. Is this true when the container contains pointers.

    The people at gamedev said you need to call delete for every object in a containor (vector) before using the clear function.

  2. #2
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    You will have to provide an example of the usage you'd like to implement for a solid answer, but here's how things generally work. If you create a container inside a function, it will be cleared when you leave the function. If you pass a pointer or reference to a container into a function, anything you add/delete will still remain that way when the function leaves. While it's true that every iterator in a container is allocated on the heap, that doesn't mean the container won't destroy it once it goes out of scope. What the people at gamedev said is true only for elements in your container that you called new for. If you have a vector<char*> for example, you would need to delete [] any elements you called new [] for. However, if you have a vector<string> you will not have to call delete because you will not have called new.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    20
    It does not matter. Iterators will be copied correctly regardless of the internal implementation. The iterator will be copied and your only problem is if the container is a local object.
    I may be a little confused it is not necessary to destroy a pointer correct just the object it points to. So if you use new you must use delete to destroy the object though you use the pointer name. Is this why some suggest assigning the pointer to null after delete.
    A pointer is a reference to an object. You must have a reference to delete an object. Some suggest setting a pointer to null after delete so the pointer will have a predictable value.
    I am guessing scope rules apply normally to pointers (hence they are stored on the stack) but the objects pointed are still on the heap so you can loose the ability to delete the object if you do not return a refrence to the pointer correct.
    Correct.
    Also I asked somtime ago if container.clear() calls the destructor for the objects within the container. Is this true when the container contains pointers.
    Yes, but calling the destructor for a pointer is not what you want.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  3. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  4. heap question
    By mackol in forum C Programming
    Replies: 1
    Last Post: 11-30-2002, 05:03 AM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM