reason

why c++ must learn c type string?
I didn't get the question... Could you elaborate on it?
Perhaps, "Why does someone who is using C++ need to learn C-style strings?"
If Zhuge is right... well, they don't strictly speaking need to. But academia has yet to figure out that C++ is not, in fact, C with Classes.

-Albatross
For what it's worth, understanding the C string might help when dealing with string literals.
Take Lego bricks. The basic bricks are simple, but you can create complex modeals with them if you have enough of them and can think. These days Lego has much more special bricks than before. IMHO, they are less generic, less open-ended and less thought-provoking.

Programming is thinking. One can learn to think with simple bricks, which have less irrelevant decorations. Learning to program is one goal. Simple bricks are assumed to make it easier.

Learning to use C++ is a separate goal. Learning to use it efficiently means skipping the basic bricks, because in C++ the special bricks like std::string are more generic, unlike in Legos, and save redundant effort too.

The dilemma is that a 'beginner' has to learn both goals. Should she dive to the deep end, or dip toes in the "simple C constructs" first?


There is more to it. Many operating system APIs are C. Even the C++ contains int main( int argc, char * argv[] ), so there are situations, where some understanding about C-strings and pointers etc is necessary. Those can of course occur after mastering std::string & co.
String literals are C strings so I think it should be learned pretty early to avoid confusion.

Here are some examples of things that people might try if they think string literals are std::strings.

1
2
std::string str = "This string contains multiple lines\n" + 
                  "But why does this not work?";

1
2
auto str = "hello";
std::cout << str.length() << std::endl;

Another situation I have seen is when beginners try to add an integer to a string literal. This is allowed in many other languages so it is not strange that it is tried out but the problem is that it doesn't even give an error message when the code is compiled. Instead it just gives the wrong output at runtime which can be very confusing if you don't know what is going on.

1
2
int n = 3;
std::cout << "hello " + n;
Strings are really important . The reason to teach you c-string is to show you the existing string.h lib before <string> . In c , there was no class . In c++ string is a class .

Methods are not necessarily the same . For some issues , a c-string can be better than a c++ string . As a c++ string can be easier to work with such with all its operator overload methods.

It might happen more than once in your life that you will have to replace your c++ string by a c-string in a code for opt . Beginners tend to use c++ strings everywhere even without really needing a string . The c++ string class is big , you can reduce the generate asm code (its instructions )by simply changing your c++ strings by c- strings .

I would say that for educational purpose use c++ string.
The reason to teach you c-string is to show you the existing string.h lib before <string> .
That is a bad reason. There is no need to learn nuances and shortcomings of c-strings befor std::string. Most textbooks practicing modern approach to teaching C++ (Programming: principles and practices using C++ and Accelerated C++ gor example) do not explain c-strings until user can easily understand logic behind them.

The c++ string class is big , you can reduce the generate asm code
24 bytes of overhead per sting. 8 bytes for small strings with Small String Optimisation-supporting library. And you mot likely to store string size nearby anyway + buffer size if it dynamically generated string + pointer itself = almost same overhead.

Almost no overhead for most common operations (and O(1) size() compared to O(n) strlen() ). GCC string is backward compatible with c-strings in addition to that. It is just a pointer to beginning of c-string and other bookkeeping stuff is actually before that. This is done because of historical reasons (Easy GDB integration AFAIR).

http://coliru.stacked-crooked.com/a/11beb72065568693
Code is actually contains UB, so it for entertaiment purpose only.
That is a bad reason. There is no need to learn nuances and shortcomings of c-strings befor std::string
You should tell that to teachers.

24 bytes of overhead per sting. 8 bytes for small strings with Small String Optimisation-supporting library. And you mot likely to store string size nearby anyway + buffer size if it dynamically generated string + pointer itself = almost same overhead.
It depends if you know coding or not. No one says it was easy.

Almost no overhead for most common operations (and O(1) size() compared to O(n) strlen() )
anyone can create a getter and an integer for size within the class using a c-string.

But as the question insist on the must , it is a debate .

Strings provide good examples for students in order to learn the modifications brought by the string library .
You should tell that to teachers.
Competent teachers already know that. Like C++ author, B. Stroustrup, who is univercity teacher. Opinion of those who threat C++ as C with classes is irrelevant.

It depends if you know coding or not. No one says it was easy.
? Point was that there is almost no overhead for C++ strings.
anyone can create a getter and an integer for size within the class using a c-string.
You will have to provide many, many more function to this new class as this simple action will prevent most of c-library functions to properly work with it. Or stop reinventing the wheel and use class somebody more competent had written earlier. Due to requirements to string class it is essentually a wrapper over c-string, which you are proposing to create.


Best time to learn special case of sentiel-terminated sequences: null-terminated character arrays (more commongly known as c-strings) is after you will get hang on manual memory allocation. This will clear some unanswered questions about string literals and will prepare student to learning fun stuff like marshalling.

You will have to learn them as they clearly present one of two ways of handling sequences of unknown length which you will encounter often.
Last edited on
? Point was that there is almost no overhead for C++ strings.

This can be critical for software , you must think like 2015 with Big Data concepts :)

Due to requirements to string class it is essentually a wrapper over c-string, which you are proposing to create.
Indeed . How creative are programmers. :)

This will clear some unanswered questions about string literals and will prepare student to learning fun stuff like marshalling
Well I did not go in programming for having fun :)

You will have to learn them as they clearly present one of two ways of handling sequences of unknown length which you will encounter often.
Yes.

But as I mentionned earlier ,
I would say that for educational purpose use c++ string.
or for prototyping . Me , I am working with const char * and everything is fine :)
must think like 2015 with Big Data concepts
We are talking in context of teaching. If somebody is going to work on BigData he is competent enough to make decidion himself (And I saw Bstring more often than c-strings in highload systems).

I am working with const char * and everything is fine :)
Lucky. I was working on proprietary library for fast encoding manipulation. Reallocating buffer/copying after each Normal form convertion/other manipulations was costly so both c-strings and std::string was out. I had to work with ropes. And those used p-strings as internal node storage. Infinite fun, until I replaced manual memory management mess with boring RAII, fixing happines in form of numerous memory leaks.

There is no reason to not learn C++ string first. When situation where c-strings are preferable arise student should be skilled enough to use c-stings with responsibility.
Last edited on
fair enough
Topic archived. No new replies allowed.