What are the advantages (if any) of C strings over C++ strings?

joeyd999

Joined Jun 6, 2011
4,239
Are there any advantages that C strings have over C++ strings?
C doesn't have strings. It has arrays of characters and all the null pointers, memory leaks, and buffer overflows that go along with that.

C++ has a string class which overcomes many of the deficiencies of C's arrays -- but at a run-time performance cost.
 

WBahn

Joined Mar 31, 2012
24,695
Are there any advantages that C strings have over C++ strings?
Assuming by "C string" you mean nul-terminated arrays of char values, then the advantage is performance, both in terms of memory and speed. There are a lot of disadvantages, too. So it all depends on what is important to you.
 

MrAl

Joined Jun 17, 2014
6,490
Hi,

You can write your own C String Class or just regular C string functions and then you'll get a first hand look at what is involved and what it helps you with and what it may cause as a result.

There are some operations on strings that are often needed when dealing with a lot of strings. These involve replacing sub strings in a larger string, selecting a range of characters in a string, and of course sorting to name a few. Sorting can be done with your own algorithm or what i think was called "bsort" or something like that.

I had a program a long time back that i had to write to store file names and sort them and retrieve them for various purposes. I ended up writing a "C String Array" class that would automatically allocate memory and keep track of pointers and stuff like that. It made life much easier because i had functions like Append, Prepend, Sort, Replace, stuff like that, but you make whatever you need. If i remember right, one of the Microsoft Foundation Classes is a C String Array, but i was happier writing my own.
 

MrSoftware

Joined Oct 29, 2013
1,564
Then why mention "C"?
I was thinking he was confused and got "C" from the class name "CString".

Sometimes I think beginners get confused and think strings are actually part of the language, as opposed to just a library someone wrote for convenience. I also think a lot of guys now are starting with C++, skipping C and not realizing the difference. i.e. the class is named CString so it must be the string implementation for the C language, as opposed to a C++ class. I could be wrong, just the perception I got.
 
Top