Delete and vector pointer

This is a discussion on Delete and vector pointer within the C++ Programming forums, part of the General Programming Boards category; I know with an array I need to call "delete [] myarray". What about with a vector? A pointer to ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    184

    Delete and vector pointer

    I know with an array I need to call "delete [] myarray". What about with a vector? A pointer to a vector?

    For example:

    Code:
    //How should I delete this when done?
    vector<vector<string> >* vecPointer;

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by 6tr6tr View Post
    I know with an array I need to call "delete [] myarray". What about with a vector? A pointer to a vector?

    For example:

    Code:
    //How should I delete this when done?
    vector<vector<string> >* vecPointer;
    A vector is not an array. Just use "delete."

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> What about with a vector?
    You don't have to call delete on a plain vector.

    >> A pointer to a vector?
    Call delete on a pointer to a vector only if you used new to allocate the vector.

    You may need to use delete [] on your vector pointer if it points to an array of vectors allocated with new []. Although I'm not sure why someone would do that.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,027
    To avoid copying for one thing. I always tend to do it, wrapping them in smart pointers.
    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
    Jan 2005
    Posts
    7,344
    >> To avoid copying for one thing. I always tend to do it, wrapping them in smart pointers.
    Why not a vector of vectors instead of a dynamic array of vectors? That was the point. And if you're wrapping them in smart pointers, then you're not calling new [] and delete [], the smart pointer is.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,027
    It's not an array, it's merely about returning a vector. Returning a pointer ensures no copying is done with the vector data.
    For arrays, I always use vector.
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by Elysia View Post
    It's not an array, it's merely about returning a vector. Returning a pointer ensures no copying is done with the vector data.
    For arrays, I always use vector.
    Why do you think returning a vector directly (not through a pointer) would involve any copying? We have this thing called RVO.

    Returning a pointer would imply that the vector has been dynamically allocated with new. Doing that just to avoid some purely speculative overhead is letting the tail wag the dog.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,027
    And how will I know the compiler will do RVO?
    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
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> And how will I know the compiler will do RVO?
    By profiling your app and not noticing any performance issues in that location.

    Besides, rather than return a pointer, just make an output parameter. Dynamically allocating the vector so it can be returned makes little sense.

  10. #10
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by Elysia View Post
    And how will I know the compiler will do RVO?
    You're always singing the praises of Visual Studio and now you don't believe it will do something as simple as RVO? A compiler that doesn't do this optimization is not worth using unless you have no choice.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,027
    No, I do believe it can do it, but I don't know how or when.
    And an output parameter is not something I like. A smart pointer works just as well. Otherwise the function call needs to be on two lines (one for defining the data and one for calling the function and pass the appropriate argument).
    And normally I would avoid complexity. But returning a pointer via a smart pointer is not complexity. It works just as if you'd return by value or take a reference as argument. Because the smart pointer handles everything.
    Last edited by Elysia; 04-15-2008 at 10:19 AM.
    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
    Jan 2005
    Posts
    7,344
    You're picking and choosing which performance concerns to worry about and as a result you're selecting the option that is the least clear. You worry that RVO won't be applied so you don't return a vector by value, but you don't mind all the overhead of dynamically allocating that vector and wrapping it in a smart pointer instead of just using an output parameter.

    >> And normally I would avoid complexity.
    Your solution adds complexity because it is one more (unnecessary) layer that serves no purpose.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,027
    Quote Originally Posted by Daved View Post
    You're picking and choosing which performance concerns to worry about and as a result you're selecting the option that is the least clear. You worry that RVO won't be applied so you don't return a vector by value, but you don't mind all the overhead of dynamically allocating that vector and wrapping it in a smart pointer instead of just using an output parameter.
    It's hardly anything compared to if RVO wouldn't be applied.

    >> And normally I would avoid complexity.
    Your solution adds complexity because it is one more (unnecessary) layer that serves no purpose.
    It's completely transparent. If I had to manually delete the pointer, then it would add complexity.
    As Dak'kon use to say: Balance, in all things.
    It ensures there will be no copy by value, but perhaps no optimization either, so it's in the middle.

    If it's too slow when looking at a profiler, then I will consider changing 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.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And remember: Don't optimize performance unless you know it's necessary. If you pass around vectors of 10000 or more elements, perhaps it's a good idea to make sure they are not copied (by profiling, or by using smart pointers or whatever), but copying a few thousand (simple) objects is actually not going to take many microseconds.

    --
    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.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,027
    I know it well, but also as I do mention, it doesn't add complexity and it removes the slight chance of the overhead of copy by value, so it's the middle standpoint.
    If it doesn't add complexity (it's just as easy to read, understand and use as if it was not used), then I don't really consider it an optimization.

    So anyway, that's my reasoning. Which may or may not be wrong. But it's up to each one to choose a preferable solution, I guess.
    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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

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