Why use c strings in c++?

后端 未结 18 2024
终归单人心
终归单人心 2021-02-07 04:12

Is there any good reason to use C-strings in C++ nowadays? My textbook uses them in examples at some points, and I really feel like it would be easier just to use a std::string

相关标签:
18条回答
  • 2021-02-07 04:22

    After spending far, far, too much time debugging initialization rules and every conceivable string implementation on several platforms we require static strings to be const char*.

    After spending far, far, too much time debugging bad char* code and memory leaks I suggest that all non-static strings be some type of string object ... until profiling shows that you can and should do something better ;-)

    0 讨论(0)
  • 2021-02-07 04:22

    STL strings are certainly far easier to use, and I don't see any reason to not use them.

    If you need to interact with a library that only takes C-style strings as arguments, you can always call the c_str() method of the string class.

    0 讨论(0)
  • 2021-02-07 04:24

    A couple more memory control notes:

    C strings are POD types, so they can be allocated in your application's read-only data segment. If you declare and define std::string constants at namespace scope, the compiler will generate additional code that runs before main() that calls the std::string constructor for each constant. If your application has many constant strings (e.g. if you have generated C++ code that uses constant strings), C strings may be preferable in this situation.

    Some implementations of std::string support a feature called SSO ("short string optimization" or "small string optimization") where the std::string class contains storage for strings up to a certain length. This increases the size of std::string but often significantly reduces the frequency of free-store allocations/deallocations, improving performance. If your implementation of std::string does not support SSO, then constructing an empty std::string on the stack will still perform a free-store allocation. If that is the case, using temporary stack-allocated C strings may be helpful for performance-critical code that uses strings. Of course, you have to be careful not to shoot yourself in the foot when you do this.

    0 讨论(0)
  • 2021-02-07 04:25

    Some posts mention memory concerns. That might be a good reason to shun std::string, but char* probably is not the best replacement. It's still an OO language. Your own string class is probably better than a char*. It may even be more efficient - you can apply the Small String Optimization, for instance.

    In my case, I was trying to get about 1GB worth of strings out of a 2GB file, stuff them in records with about 60 fields and then sort them 7 times of different fields. My predecessors code took 25 hours with char*, my code ran in 1 hour.

    0 讨论(0)
  • 2021-02-07 04:25

    The usual reason to do it is that you enjoy writing buffer overflows in your string handling. Counted strings are so superior to terminated strings it's hard to see why the C designers ever used terminated strings. It was a bad decision then; it's a bad decision now.

    0 讨论(0)
  • 2021-02-07 04:32

    c strings don't carry the overhead of being a class.

    c strings generally can result in faster code, as they are closer to the machine level

    This is not to say, you can't write bad code with them. They can be misused, like every other construct.

    There is a wealth of libary calls that demand them for historical reasons.

    Learn to use c strings, and stl strings, and use each when it makes sense to do so.

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