CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Page 2 of 2 FirstFirst 12
Results 16 to 19 of 19

Thread: How to use set<char*>?

  1. #16
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004

    Re: How to use set<char*>?

    Larry, let me share with you someone told me about 16 years ago with regard to using STL collections.

    And that wonderful bit of advice was simply to learn STL collections inside and out, learn which containers to use for a particular job, learn how to iterate through them, how to clean up memory, etc.

    I took his advice and spent a few weeks (working through Scott Meyers' Effective STL) and it has served me well time and time again over the years.

    Of course, back then the internet was different and someone learning simply couldn't post a question to a forum and have 5 people give them an answer quickly. Back then you had to do you own research and actually invest time in learning a new technology.

    Learning this way didn't give you quick answers, but it sure stuck with you.

    To me, it's worth investing the time to get the background on a subject, then you won't have folks question knowledge gaps - like why after 9 years it seems that you don't know that delete goes with new, or not knowing that most questions can be answered quickly by just stepping through code in a debugger.

  2. #17
    Join Date
    Apr 2000
    Belgium (Europe)

    Re: How to use set<char*>?

    Short answer:
    yes you can use char* in a set to manage pointers to strings.
    but it takes quite a bit of management to get it to work properly for all usage cases.

    Why make your like harder than it needs to be?

    to achieve the similar result:

    set<string> is the simplest solution.
    set<someOtherStringClass> is the 2nd best solution, this could be MFC/ATL's CString or a string class from some other library, or even a very basic self written char* wrapper class.
    set<vector<char>> is alternative 3rd best.

    Any other solutions are really just about being masochistic enough to do it the hard way just because you like pain.

  3. #18
    Join Date
    Apr 2000
    Belgium (Europe)

    Re: How to use set<char*>?

    Quote Originally Posted by Arjay View Post
    like why after 9 years it seems that you don't know that delete goes with new
    Well to be more precise in this case. it's delete[] that goes with new[] ;-)

    I know arjay knows this, but for anyone else that needs to know.
    delete and delete[] are different beasts.
    no you can't use them interchangedly.
    no not even for char (or any POD type).
    yes I know it "probably works on your compiler", that's still doesn't make it right.

  4. #19
    Join Date
    Apr 1999

    Re: How to use set<char*>?

    Quote Originally Posted by LarryChen View Post
    Yes, erase does free the memory. Thanks.
    It does not free the memory that you allocated.

    Ask yourself this -- how would a std::set<char*> know that the pointer that is there points to dynamically allocated memory? It doesn't know.

    Ask yourself another question -- if the std::set<char*> did know that the pointer was dynamically allocated, how does it know which function or operator to call to deallocate the memory? Maybe it was allocated with new, new[], malloc, who knows. Each method requires its own deallocation routine/function.

    So no, the memory is not deallocated for the simple, logical reason that the set knows nothing about where that pointer comes from and how to deal with it. Only you know where it comes from and what to do with it. All the set knows is its own internal structures required to store the pointer.

    Also, it doesn't win you points by resorting to constructs that are harder to maintain. If you had to build an application that has to be close to, if not bug free, what do you think would be your choice? Handling char* or std::string?


    Paul McKenzie

Page 2 of 2 FirstFirst 12

Posting Permissions

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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)