C and C++ strings

Hi.I am reading a book on C++ and it says there are 2 ways to handle strings in C++.One is by using char arrays(C Style) and another by using the string library.Can someone please explain the advantages/disadvantages of both?

Thanks.
closed account (o3hC5Di1)
Hi there,

As far as I understand it, std::string objects are now the preferred approach and C-style strings are mostly used for compatibility with functions that still take them as an argument.

Within an OOP context it is more conforming to use an OOP approach to strings as well imho.

Perhaps one of the experts here can give you a more in depth answer.

Hope that helps.

All the best,
NwN
Advantages of std::strings: easy comparison operators(no need for strcmp), can easily get size()(no need for strlen), easy copying(no need for strcpy).
Advantages of c-strings: NONE!
closed account (zb0S216C)
@Viliml: you're missing 1 important thing: a C-string's length is constant, whereas std::string's length is dynamic. This of course allows the user to enter a string without worrying about a buffer overflow.

Wazzak
c-strings are faster.

What do you think length() does but strlen()?

I don't particularly like c++ strings myself. Main reason is that I don't know if they are stored on the stack or the heap. If stored on the stack, I really don't see a reason for them. If they are on the heap, then sure, that's a bunch of code we all don't have to write over and over again.
Last edited on
length() is required to run in constant time. It certainly does not call strlen().

Main reason is that I don't know if they are stored on the stack or the heap.

There is no reason for you to care.

If stored on the stack, I really don't see a reason for them.

That requires further explanation.
Last edited on
Theoretically, before C++11, there were situations where C strings would be more efficient in some situations, but compiler vendors actually implemented C++ strings as C strings underneath, so as of C++11, C++ strings are officially 100% compatible with C strings: there is always an additional null char in the end, &s[0], data() and c_str() are synonyms (except that &s[0] allows modifications), and the pointer they return can be used with any function that expects a C string (as long as the original C++ string isn't resized).
Last edited on
length() seems to return the member variable for length (which would make sense, how else could it be constant I wonder). It also appears they are on the heap.

So my opinion would be that it depends on how unknown your string operations are going to be. I recently made a little program to parse an XML, which would have been crazy trying to do with c-cstrings (dynamically allocated especially). If nothing complicated or extremely time dependent was happening, I would go for c-string.

Edit: Just saw your post there Cubbi:
(as long as the original C++ string isn't resized)

So don't do this?
strcat(&str[0], "oops");
Last edited on
So don't do this?
strcat(&str[0], "oops");

Why not?
1
2
3
    std::string str;
    str.resize(4);
    std::strcat(&str[0], "oops");
is perfectly fine (only guaranteed since C++11 of course)

I meant that if you stored a &str[0] in some char*, then resized the string, then attempted to use that char*

It also appears they are on the heap.

Depends on the size (small strings are entirely on stack) and on the allocator supplied (long strings use the allocator, the default allocator places them on heap)
Last edited on
Topic archived. No new replies allowed.