Copy of const char * using std::string constructor

前端 未结 5 448
鱼传尺愫
鱼传尺愫 2021-01-15 09:36

is this code ok?

void SomeClass :: foo(const char * _name) {
     //name is of type const char *
     name = std::string(_name).c_str();
}

相关标签:
5条回答
  • 2021-01-15 09:47

    In this case you create a temporary object and assign it to your pointer. When you leave the function this object is destroyed and your pointer points to nowhere. In your case it might work because the memory is not overwritten yet. But you should use strcpy to avoid this problem.

    0 讨论(0)
  • 2021-01-15 09:52

    I vote for strcpy. Why complicate simple things?

    Or even better - since you'll obviously need to use it later, why not convert it to a std::string and just use that afterwards, forgetting about all those char *?

    0 讨论(0)
  • 2021-01-15 09:59

    If you don't do anything with name, it's perfectly safe. Otherwise, it will probably fail at some random point in the future. The C-style pointer returned by the c_str() member function is only valid for as long as the temporary std::string exists (and as long as you don't mutate it, which you haven't in this case). As soon as the enclosing block scope exits, the temporary is destroyed, and any use of name puts you into the twilight zone.

    As others have suggested, you should turn name into a std::string. Alternatively — if you really need it to stay a char * — you could just write name = strdup(_name).

    0 讨论(0)
  • It's ok since it compiles and doesn't cause undefined behavior.

    It's not ok since name points to an invalid memory after the statement completes execution.

    name = std::string(_name).c_str();
    

    At the end of this statement the temporary std::string is destroyed and it frees the memory of c_str().

    should i do an old school strcpy ?

    No, just change name to be std::string:

    void SomeClass :: foo(const char * _name) {
         //name is of type std::string
         name = _name;
    }
    
    0 讨论(0)
  • 2021-01-15 10:11

    It's not okay to use it afterwards - the memory can be freed as soon as the temporary goes. Why don't you use a string as a member? Then you wouldn't have to worry about memory management.

    0 讨论(0)
提交回复
热议问题