Thread: strings in C++

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595

    strings in C++

    I've noticed a trend of responses nudging/encouraging people to use of STL strings instead of null terminated char arrays as strings in C++, indicating that STL string class is "the" form of strings in C++ and null terminated char arrays are only for C. While I certainly agree that in general STL strings are easier to use, and should be prefered if a choice is available, I disagree that null terminated character arrays should be shunned in C++.

    I wonder how others feel.
    You're only born perfect.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    depends

    Kuphryn

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    While I certainly agree that in general STL strings are easier to use, and should be prefered if a choice is available, I disagree that null terminated character arrays should be shunned in C++.
    hmm... but if you agree that std::string should be preferred to null terminated multibyte strings, then would that not mean that, if a choice is available, one should avoid the latter?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    My two cents worth:

    I think the majority of cases where this "suggestion" is made are clear cases where a newb is trying to use them and they are stumbling around making obvious (to the experienced) mistakes with memory management or buffer overruns or NULL termination issues. In such an instance, especially considering this is the C++ forum and not the C forum, it makes sense to try and nudge them into using a far safer, far simpler alternative. This makes even more sense if the character manipulation isn't the crux of the issue they are trying to solve but rather something that is merely compounding the problems they are having with the code. If they happen to post a bit of code where they are using the character array correctly but having problems with some other aspect of the code then I might not suggest a string since they are obviously experienced to use them correctly. I still might, but I'd hope I'd be less pushy about it. There is usually a good reason to use them when suggested to make ones code cleaner looking and more intuitive which is a great plus for newbs.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    It seems to me there's a strong, positive rationale for preferring use of stl containers over corresponding c-style arrays, strings or otherwise. I'm not so sure the reverse can be convincingly argued.

    Fair enough, there might be circumstances where a char array might be a viable alternative but for dynamically allocated strings in c++ std::string is surely to be preferred.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Since C-style strings, and functions like strcpy and strcat are part of C++, I don't think it is something to completely sweep under the rug. And learning some of the pitfalls can be helpful. Sure, you can use a std::string quite a bit of the time, but there are still things like sprintf or strftime that can be useful.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Since C-style strings, and functions like strcpy and strcat are part of C++, I don't think it is something to completely sweep under the rug. And learning some of the pitfalls can be helpful. Sure, you can use a std::string quite a bit of the time, but there are still things like sprintf or strftime that can be useful.
    Well, those might fall under the category of a choice not being available (or having been ruled out for specific reasons). Consider that opening a file with fstream requires a C-style string.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by laserlight
    Well, those might fall under the category of a choice not being available (or having been ruled out for specific reasons). Consider that opening a file with fstream requires a C-style string.
    But one can be made available using std::string.
    Code:
       std::string filename("file.txt");
       std::ifstream file(filename.c_str());
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    But one can be made available using std::string.
    Ah, but if I remember correctly the reason was so that one could deliberately avoid using std::string in that context, if necessary.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by laserlight
    Ah, but if I remember correctly the reason was so that one could deliberately avoid using std::string in that context, if necessary.
    Are you talking about how you're supposed to avoid using STL coontainers with C functions? I'd agree, but in the case of opening a file there isn't much of a choice here. open's first argument is a character pointer.

    If you took the string object and cached it in a char array, so you could use C functions, you'd have to wonder about your use of the string class, but that's it. Like Dave said you can't completely avoid it.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Are you talking about how you're supposed to avoid using STL coontainers with C functions? I'd agree, but in the case of opening a file there isn't much of a choice here. open's first argument is a character pointer.
    No. What I meant is that in this case, by design, std::string is not necessary.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    I like how strings have overloaded operators, it makes me feel happy

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM