How to use char* in map instead of using string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Man4ish
    New Member
    • Mar 2008
    • 151

    How to use char* in map instead of using string

    How to use char* instead of using string as key and value.

    map <string, string> m;
    m["Test"]= "Pass";
    m["Result"]= "Fail";

    When i try to use char* as key value there is memory leak.

    map <const char*, const char*> m;
    m["Test"]= "Pass";
    m["Result"]= "Fail";

    How to avoid memory leak? Thanks in advance.

    Regards
  • newb16
    Contributor
    • Jul 2008
    • 687

    #2
    First, you need to implement comparer for the char* as a key, or else two keys with the same string value at different addresses will be considered different. Then use shared_ptr to take care of keys and values.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      I feel compelled to ask why would you want to make this change?

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        You avoid the memory link by using a struct/class that has a destructor.

        You cannot use the STL containers without well-behaved classes. That is, classes with proper constructors, destructors, copy constructors and assignment operators. You may also need an operator< or a suitable comparator function.

        Comment

        • whodgson
          Contributor
          • Jan 2007
          • 542

          #5
          If 'char*'is a C string type (which it is)how could you do so and why would you want to ?

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Originally posted by weaknessforcats
            You cannot use the STL containers without well-behaved classes. That is, classes with proper constructors, destructors, copy constructors and assignment operators. You may also need an operator< or a suitable comparator function.
            I completely understand what you mean and whole heartedly agree with the sentiment but that isn't quite true. You can use the containers with the numeric PODs (int, long, float, enum etc). It is arrays (or pointers to arrays) that are troublesome.

            For anyone thinking I have left out structs I basically lump them under "well-behaved classes" since a struct is a class.

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Originally posted by whodgson
              If 'char*'is a C string type (which it is)
              A char* is not a string type.

              Instead it is a pointer to a single char.

              True, some C functions ASSUME a char* is a pointer to a C-style string but it is always an assumption.

              In C++ the string type is string.

              Comment

              Working...