Why do you prefer char* instead of string, in C++?

后端 未结 12 1318
北海茫月
北海茫月 2021-02-01 08:31

I\'m a C programmer trying to write c++ code. I heard string in C++ was better than char* in terms of security, performance, etc, however sometimes it

12条回答
  •  我在风中等你
    2021-02-01 08:42

    Ok, the question changed a lot since I first answered.

    Native char arrays are a nightmare of memory management and buffer overruns compared to std::string. I always prefer to use std::string.

    That said, char array may be a better choice in some circumstances due to performance constraints (although std::string may actually be faster in some cases -- measure first!) or prohibition of dynamic memory usage in an embedded environment, etc.

提交回复
热议问题