char vs string
Ok, my instructor for my CS II class is insisting we use use the char datatype versus the string datatype. He gave us a few reasons (very loosely knit) as to why the char datatype is better.
My query is, which one IS better? I know that I have always had alot of trouble with the char datatype just because it can have a subscript, and it can do without a subscript. Also, there dont seem to be predefined functions to work with the datatype char so it is just generally a pain in the butt even if you produce your own functions.
Now string on the other hand has a slew of functions already built to work with it such as .length(), AND its just all around easier to work with.
What do you guys think?
# 1 Re: char vs string
I agree that the string class, if it can be called that, is easier to use. However when using old APIs it's easier to use char arrays instead of strings because many functions have to be passed char pointers. It depends what you're working with.
# 2 Re: char vs string
string is better than using plain chars yourself.
because u dont have to worry about allocations and such.
but string is still not a decent class (most of the STL classes arent in my opinion, but thats why they are called containers)
# 3 Re: char vs string
Ok, my instructor for my CS II class is insisting we use use the char datatype versus the string datatype. He gave us a few reasons (very loosely knit) as to why the char datatype is better.Have your instructor look at this document, written by the inventor of C++, Bjarne Stroustrup. He disagrees with your teacher, and gives reasons why.
http://www.research.att.com/~bs/new_learning.pdf
Also, char is only a single character. That is not a string. If your teacher means char arrays, then there are security issues that arise when handling char arrays, such as buffer overruns. Some C++ shops disallow any use of char arrays and functions such as strcpy(), strcat(), etc. because of these reasons.
Regards,
Paul McKenzie
# 4 Re: char vs string
I agree that the string class, if it can be called that, is easier to use. However when using old APIs it's easier to use char arrays instead of strings because many functions have to be passed char pointers. It depends what you're working with.You can still use strings. You have the c_str() member function, and if the string is writeable, you have std::vector<char>.
In this day and age of C++, rarely, if ever, do you need to use the new[]/delete[] way of creating a string or string buffer.
Regards,
Paul McKenzie
# 5 Re: char vs string
I find it amusing that a CS instructor would profess character arrays to be superior to string classes.
I would genuinely enjoy reading his reasoning if you would be so kind as to post it. In true Platonic spirit I would want to hear of a salient point that might be illuminating, even if I must admit I doubt there can be any.
There are a number of good string classes, and the STL string class may not be the best of them. I've used a portable, 'almost' drop in replacement of the MFC CString class (also loosely based upon the STL basic_string) for portable development with good effect. I suppose one of the reasonable arguments against string classes (though not compelling IMO) is the fact that there are several string classes. I suppose these days there may be agreement that the STL string class is the standard, but obviously developers using various frameworks have other relationships to consider (MFC's use of CString for example).
The const char array (and char array) is generally widely applicable, and perhaps that's what the professor is thinking.
Containment is more compelling as a reason, I think - favoring string classes.
JVene at 2007-11-9 1:27:57 >

# 6 Re: char vs string
Have your instructor look at this document, written by the inventor of C++, Bjarne Stroustrup. He disagrees with your teacher, and gives reasons why.
http://www.research.att.com/~bs/new_learning.pdf
:eek: ^^^^^^^^^^^^
Impressive Paul, thanks! I read that and got a better idea of just why strings are generally better.
Im not really sure why he thinks char's and char arrays are better. Personally I hate them, lol. And to answer your question JVene, I believe he said something about a string variable not having a null terminator at the end. And also something about the spaces between collective words in a string causing search issues or something. My mind kind of shut off as I quickly realized that this semester may very well be a proverbial hell if he pushes the char datatype onto us and discards anything string.
OFF TOPIC EDIT:
On a side note, I just put honey in my coffee. Its pretty good actually
# 7 Re: char vs string
I quickly realized that this semester may very well be a proverbial hell if he pushes the char datatype onto us and discards anything string
If he does not discard classes, you could write your own string class and use that.
# 8 Re: char vs string
If he does not discard classes, you could write your own string class and use that.
LOL, I was already thinking about that. I was actually working on a class with a function to compare char arrays to strings but then I was like "Why dont I just demand that I use string? Its me that paying for the education!"
And besides, if he demands that we use char... he's not really teaching us to use things the proper way, is he? Strings from what Ive always known in the past, are just better on many different levels.
# 9 Re: char vs string
They are better on many levels.
The 'zero termination' notion is a red herring.
I have no idea what spaces have to do with it - sounds odd.
JVene at 2007-11-9 1:31:56 >

# 10 Re: char vs string
the only reason i see fit to use dynamic char arrays and handling them yourself is to learn why string classes are better and how to make them yourself and how to learn how memory works. but if he is using this method to teach you C++ he is seriously wrong in doing so
# 11 Re: char vs string
Bravo Mitsukai!
Teacher: "Go ahead, put your hand on the hot stove. Tell me what you discover!"
A lesson more valuable than simply being told!
JVene at 2007-11-9 1:33:56 >

# 12 Re: char vs string
Teacher: "Go ahead, put your hand on the hot stove. Tell me what you discover!"
Student: "Yum! Tasty! Where did my hand go?"
# 13 Re: char vs string
Using a string is a much better choice than a char array. UNLESS, the only thing you are doing is storing text data, with possible appends.
If you are inserting, etc... Then a string type is better.
# 14 Re: char vs string
I knew string was better, I will continue to use it until he tells me to stop (or gives me an F) and then I will bring up the points you guys have told me. Thanks!
# 15 Re: char vs string
sorry to bring this old thread up...
I find it amusing that a CS instructor would profess character arrays to be superior to string classes.
i dont... because i see alot of errors in the IT/CS education branch...
and the most frustrating is that it is passed on.. and creates more mistakes... and thats how words are twisted of their meanings and such..
in these ECDL(Europian Computer Driver License) books i have read i see an error or 2 in each summary.. and i neach book there are around 10 summaries.. i had to memorize the wrong facts to pass the tests...
the most problematic is, the meaning of words, one word an mean diffrent things, but this is IT/CS and word should only have one meaning, but still theyare going around writing facts with using the wrong words... this could be a problem of Language English -> dutch or something... but it just deeply disturbs me
# 16 Re: char vs string
I don't use char arrays for anything, the only thing I use the char datatype for is if I have a single character I want to store, and thats it. Other than that, char arrays are too much of a pain to mess with...
I remember him saying something about running into trouble manipulating strings, whereas char arrays wouldn't have problems like that. I cant remember what he was complaining about them for, but he's an old school C programmer so he probably has some sort of a vendetta against using strings for one reason or another. But Ill program my way, the easy and more efficient way, using strings.
# 17 Re: char vs string
... he's an old school C programmer ...
Say no more...
# 18 Re: char vs string
... he's an old school C programmer ...
Ouch! I'm an old C programmer!
Though I jumped into C++ within the first year of it's availability and hardly ever looked back.
If you want a bit of a laugh, look up Linus Torvalds (sp?) comments on C++.
He says things like (and I must paraphrase):
"C++ programmers are snobs",
"They should admit they're really writing in C anyway",
"C++ compilers are all crap" (though he later retracted that, saying "they're getting better"),
"You can't write an OS in C++" (see Stroustrup on this point),
"It's slow, buggy..."
After I got through that, I thought to myself "how did this guy ever make an operating system", but then I have to realize that anyone with THAT much invested in C just can't help letting emotions and prejudices overtake judgment, and turn that into rationalizations and justifications.
It can become a religious level argument.
Now, we're talking here about character arrays vs string classes, but under the hood, I submit, you're really talking about the difference between object level solutions and non-object level solutions (or C level solutions).
I suppose that to anyone so dedicated to C (and nothing beyond C), C++ programmers might seem like religious zealots (and the reverse is true).
I briefly joined a debate on the subject of "object oriented techniques in C", but I had to bow out. It seems that someone had decided that objects were a good idea, but they didn't need C++ to implement them, and according to their line of thinking, they had written objects in C long before anyone called them that.
The string class is a primary argument against that notion, but the faithful never let go.
JVene at 2007-11-9 1:41:03 >

# 19 Re: char vs string
strings are easier and faster most importantly. char is much more powerful because as always if you understand the building block of something you can create more then you can if your more limited such as string functionality.
I also agree you dont need c++ or objects. Objects are easier for a 3rd party to use, but C has done everything ive ever seen c++ do. Ive seen programs as stable and large as any c++ program ive seen as well. I havent seen c++ or object oriented code really do anything I havent seen done in C.
You could say well OOP is modular and easy to read, and I can say functions were made to keep programs modular and easy to read.
I run into lots of problems using strings so I understand char arrays which is all a string is layered on top of anyways. Then knowing char arrays you avoid the problems that the string class has. I agree with your instructor char arrays must be understood then you can choose between the two, but char arrays in general are less of a hassle.
If containment is an argument for string class explain to me how a function
foo()
{
char word[128];
}
isnt contained inside a function and is destroyed at the end of it. If thats not contained I dont know what is.
# 20 Re: char vs string
Ouch! I'm an old C programmer!
I wasn't poking fun at it :p What I meant behind that was that if he's teaching a C++ class, he shouldn't be using C style ideals to do it. Its C++, similar to C, sure, but not the same curriculum.
# 21 Re: char vs string
If containment is an argument for string class explain to me how a function
foo()
{
char word[128];
}
isnt contained inside a function and is destroyed at the end of it. If thats not contained I dont know what is.
What does the number 128 mean here? How many times in the rest of that function will you have to check against this boundary? Do you really want all this data to be on the stack?
I'm curious to know what problems you've been having with STL strings. The only disadvantage to null-terminated strings I can think of is poor binary interface compatibility.
# 22 Re: char vs string
abcdefgqwerty, I think you've touched upon the central theme of the point.
After I posted this, I thought I'd preface: I'm not trying to evangelize on C++, or convince anyone - I just wanted to illustrate my thinking on the subject.
The comparative advantages of strings vs character arrays follows the same lines of thought as the advantage of object oriented design over non object oriented design. The notions of portability, safety, flexibility and leverage come to mind, though I'm sure there are others.
Your point is correct enough. When any character array, allocated on the stack, looses scope, the two basic tenets of containment are demonstrated. There are other factors not so well covered, though.
For one, the size of the array is now fixed. The boundary of the storage can be overridden, unless care is taken to avoid it. That care can be encoded within a good string class, but is 'manual' in the case of character arrays. It may depend on the limit of one's own view of the subject of containment, but I think it's reasonable to propose that containment ought to include such tributary concepts as dynamic extension of the storage, strong limits on the boundaries of storage, perhaps even the mutation of storage type.
The character array isn't applicable to a unicode build, for example. A string class can be presented so as to automatically mutate according to the build configuration. Obviously one may counter with a reply that TCHAR or some similar macro solution does the same, and that's true, but an object solution provides even more flexibility, more automatically, than the imposition of a macro solution.
If, for example, one used a string class throughout application code, and then, for some reason, a change in the presumed use of the string class were declared, a search and replace is sufficient to alter the chosen string object, or perhaps a redefinition of the string class would suffice. I've done that a few times in my career, where existing code was moved from one string class to another, or code written without the assumption of unicode was transformed into unicode compliance rather easily. The use of the class was similar, with respect to indirection of the concept, to the use of TCHAR, which could also be defined according to changing specifications. However, if application code used the default 'char *' or 'char array', it's not quite as straight forward.
Perhaps that doesn't sound like such a big deal. Ok, how about custom allocation substitution for the string class? In two cases I was able to substitute the allocation mechanism for an application without changing the application code, simply because the code used a string class for the work. That is simply not so straight forward if TCHAR or character arrays are used.
With character arrays, one might find mixtures of the use of new and malloc (or calloc), the latter being an older C practice. Switching from ASCII to unicode might not work so well for application code built by several hands mixing these styles. It wouldn't even be possible to cause the problem with string classes. One could counter that malloc should never have been used, and I agree. My point is that with a string class such a problem isn't even possible, as long as the class works reasonably well.
Now, the point you bring up with a stack allocated string is significant. For one, it may well be much faster than a counterpart based on a string class, unless a fixed sized string class is available (and there are a few). There can be important performance arguments that hint that character arrays, or the TCHAR solution, might offer some minor performance advantage. Even here, however, an advanced C++ engineer would loose no advantage through the use of fixed sized string classes. The result is similar to the comparison of arrays and vectors. Considerable debate surrounds that comparison, too, but ultimately the choice isn't well made if the weight of one argument is given too much value, specifically performance over stability or flexibility.
You must observe, too, that a local character array isn't common enough to be the deciding factor in one's choice of practice. A fair guess, I think, is that at least a significant portion (half, perhaps) of string allocations must be dynamic, and will therefore require 'delete' to complete containment. In the presence of exceptions, strings offer a safer alternative against memory leaks.
The application level mutation of string behavior is another notion fairly important to me. When Visual Studio's progression of it's CRT deprecated several familiar string functions in favor of 'safer' versions, application code either required change or continued use of the deprecated versions. The deprecation is based on sound reasoning, but retrofitting application code is expensive for little or no obvious return (assuming one is continuing development on a substantial codebase). Such a retrofit is rather simple in the case of disciplined use of string classes, because the classes offer a point of indirection that is unavailable if the CRT string operations were used.
Indirection of an important subject like this is, in my opinion, one of the strongest reasons to favor a string class. It is true, also, of most object solutions vs non object oriented implementations.
One use I think is novel is language independence. In my own work, dialogs, menus, button labels - everything - comes from a table, loaded into strings. In that case, however, the strings are part of a language portability library, such that I can provide a translation of the prompts, labels and help text, then substitute rather easily. I built the system a few years ago, and I've used it to good effect, supporting languages I've never learned. I design in English, purchase translations in Russian, Greek, French, Spanish, etc...and provide the database at installation. The string classes are compatible, but derived from my 'standard' string class. The application code operates without change, but 'automatically' adopt to changes in language. So far as I've observed, its the easiest means of language portability I've found. One could say that my 'languagestring' class mutates on a switch of human language. I hardly give it much thought anymore.
My underlying motivation was to respond to your point; containment is not the only or even the primary decisive factor in recommending string classes over character arrays. I don't think the subject is so firm that everyone would be convinced of my own view, but the weight of advantages for string classes does appear compelling the more I thought about it over the years.
One hassle I have encountered, though, has been the fact that the STL string class isn't so desirable as others, and no real standard string class ever really appeared. The STL string is, to my mind, a 'default' standard, but I don't use it myself.
That became a nuisance to me when I adopted the use of wxWidgets. They have their own string class (wxString), which, while fair, wasn't compatible with the string class I had used for some time to that point (a CString like class based, somewhat, on the recent style when CString was based on basic_string). I had a large volume of libraries which I dragged from the days of OS/2 forward, through OWL (Borland's framework), MFC, a couple of others I've forgotten by now, and then wxWidgets. My preferred string class blended very well with CString (I could even USE CString instead), so MFC was very easy. OWL, if I recall, didn't rely on a string class, so it was no problem, but wxWidgets accepted wxString as a parameter throughout.
So, I didn't what any practiced engineer might think to do - I replaced wxString in wxWidgets. There was about 30 minutes of fussing to do it, but after that there were no issues.
Now as a response to the comparison of C++ vs C, it's true that one can use either language for any target. Consider, though, that some of the most ambitious applications made are not, at least in current and recent forms, built with C. AutoCAD, ArchiCAD, 3D Studio Max, PhotoShop, and Adobe Illustrator are good examples. I'm not sure if the oldest versions of Illustrator or PhotoShop were built in C, but I do recall that the older versions of AutoCAD and 3D Studio (the DOS version) were C applications.
When the developers considered making a windows version of 3D Studio, they decided on a ground up re-make, rather than any kind of conversion of the DOS application. They choose C++ in order to gain leverage in development.
I've seen that in the 20 years I've written in C++ (though certainly more in the last 15). The volume of work I can produce, and the solidity of that work, is greatly enhanced by the use of objects to provide leverage. I can't gain that leverage with C, though I could write the applications in C. Indirection in the designs, threading, multiplexing - it's difficult to relay in a thread just how, in all that time, I've observed leverage coming as a direct result from thinking and implementing ideas in objects.
At the risk of a full hijack of the original inquiry, my own thread classes are a prime example. Threading in C would be, I'm fairly certain, an entirely 'manual' process. Certainly possible, even viable, but the effort I would have to spend on the subject would be many times greater than in C++, using the objects I developed for threading when OS/2 was new. A quick example (and I've posted on this a few times around here), I can thread with no more than:
class SomeWindow : public BasicPanel
{
private:
QueProcessor Que;
public:
void SomeUIMessage( UIEvent & );
void SomeThreadedFunction();
};
void SomeWindow::SomeUIMessage( UIEvent &)
{
Que.Call( this, &SomeWindow::SomeThreadFunction );
}
void SomeWindow::SomeThreadedFunction()
{
.. this runs in a thread
}
To me, this leverage is irreplaceable in C development. The pseudo code above illustrates all that's required to launch a thread and call a member function on that thread from some message response function. The solution is portable from Linux, Windows and MAC. I don't see how that can be so short or simple in C.
Over time, any problem I've faced that appears even remotely applicable to other application code ends up in my personal library for future use. While I could do that in C, also, the results wouldn't deploy quite so easily as the thread example above.
Ultimately that returns two business level benefits that argue heavily in favor of C++ for application level code; speed and reliability. The threaded solutions always work (they were debugged years ago, but if a bug were ever found, the correction would lend itself to all older application code with a simple recompilation), and they're quick (therefore cheap) to deploy.
JVene at 2007-11-9 1:45:14 >

# 23 Re: char vs string
JVene, your posts look like articles. :) You should write articles more frequently...
The only thing that I think using char arrays tells you when managing dynamic length strings is that whenever you do a new, you know you have a call that can be slow. With std::string encapsulating this detail, many-a-times one can find code that does a lot of string concatenation without realizing that as soon as you overgrow the internal allocation size, you destroy the last one and reallocate a larger chunk. You can almost get away with this with reserve()/resize() but I haven't seen it put much into practice. And everytime someone writes a code like this:
std::string string1="somestring";
std::string string2="something";
//etc more strings
std::string final = string1+string2+...
//or
std::string final="somethingelse";
final+=string1;
Well, not exactly like that but frequent concatenation like above, he implicitly under the hood is allocation, deallocating, allocation again, deallocation and so on, which is useless. When you do that with chars it makes it visible... :) nothing more...
Moreover, for local statics and literals, fixed size ascii strings, char array is okay. But really usage of std::string should be promoted among beginners for sure.
