Archived

This topic is now archived and is closed to further replies.

c++ and strings vs Cstrings

This topic is 5014 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hey I am doing C++ @ uni and all my lecturers tell me _not_ to use string objects but to use Cstrings... they say strings cause problems, what are these problems? which is best? Thanks!! |:Lee

Share this post


Link to post
Share on other sites
std::string generally causes less problems than char *''s. It all depends on what you''re doing. Even if you aren''t going to use them, you should know how character pointers work. I''d say it''s better to learn char * first, just so you know why and where it is better to use std::string.

Ask your professor for a justification, and follow his rules when you write code for your classes.

Share this post


Link to post
Share on other sites
generally strings cause problems in C++ from buffer overflow errors. for instance:

char foo[10];
strcpy(foo, "longer than 10 characters");

this will work and, if you're lucky, cause a runtime exception. the problem is if the memory adjacent to that char array is also posessed by your app, strcpy could just keep on writing with no error. it would write into some other part of your app and you'd end up with wierd errors.

it obviously doesn't have to be strcpy that does the overwriting, you can do it yourself!


for (int i = 0; i < 11; ++i)
foo = "c";


this will also overwrite foo, and if you're lucky it will crash your app. but again it doesn't necessarily crash your app. you might just get wierd errors.

buffer overflows are one of the more commonly used "bugs" that crackers will use to break into IIS web servers, for instance, or use to write worms and virii.

that's why you're told that C++ strings are bad-o. but i think std::string is a lot more reliable. i'm sure it's not perfect but i've never know a problem with it. i guess you could do something stupid like:

std::string myString = "someString";
char * hackString = (char *) myString.c_str();

then you've cast it away from being a const char * and now you can probably break it because the internals of the stl string are no longer protecting the internal char array against buffer overflows.

-me


[edited by - Palidine on March 26, 2004 9:12:13 PM]

Share this post


Link to post
Share on other sites
If anything, i''d say C strings cause more problems to beginners than String objects. I guess only your professor knows exactly what complications he''s talking about. Why wouldn''t you ask this question to him instead of us?

Some problems with std::String might be that you don''t have as much control over things like how much memory they take up. They can be cumbersome to use with a lot of functions that are already are based on strings. They introduce more overhead into your program. They are more difficult to use in networking applications. They don''t teach as much about memory issues and how computers work as character arrays do.

I''m by no means saying std::Strings are bad. I''m just taking some shots in the dark as to what your professor might be referring to.

Share this post


Link to post
Share on other sites
woah.. quik replies \o/

i don''t see my lecturer til next thursday O_o
also there seems a blind reasoning not to use std::strings
"just don''t..." i think he did refered to something about problems with passing.. but not sure tbh

atm i and using char* with no problems, just std:stings has some nice OO features and fuctions.

thanks for the replies, all somewhat helpful !!!

i will nagg my prof for answers


|:Lee

Share this post


Link to post
Share on other sites
You know, from the original post I can''t tell if your professors are recommending C style null terminated strings or instances of the MFC CString class. Depending on the compiler you''re using in your class I can see justifications for the latter. The std::string implementations in MSVC 5 and 6 were just wretched. In particular, the nature of their reference counting mechanism made proper multi-threaded programming with them hideously difficult.

Share this post


Link to post
Share on other sites
Recall that C and C++ are value semantic languages; that means when you pass a std::string to a function a copy of it''s value is made - unless you explicitly instruct it to do otherwise.

Like all objects, you should pass them by constant reference instead of copying them.

You have _complete control_ over the memory use of std::string. It functions a great deal like std::vector, but has additional methods and functions to manipulate strings (e.g. c_str()). std::string also makes the same continuous memory buffer gaurantee than vector does, i.e. this is legal and correct code (ignoring the buffer-safety problems of sprintf, and I don''t handle the error condition though I''m virtually certain it would generate an exception in this case):
std::string str;
str.resize(1024);
str.resize(sprintf(&*str.begin(), "Testing %d", 12));

std::string is a template instantiation of the std::basic_string template class. basic_string, like all other stl container objects, takes an allocator as a template parameter, which allows you to completely customize the memory (de)allocation behavior.


I can think of two reasons to forbid the use of std::string. If the class does not yet understand how to properly use raw char *''s, or if the class does not have sufficent C++ knowledge to understand the complexity of basic_string (templates, object construction & copying, stl design), which will be covered later in the course.

If (s)he actually means ''never use std::string'', then focus on the syntax and take any design advice with serious skepticism. There are several scenarios where using std::string is not advisable, but that is the exception not the rule.

Share this post


Link to post
Share on other sites
quote:
Original post by SiCrane
You know, from the original post I can''t tell if your professors are recommending C style null terminated strings or instances of the MFC CString class. Depending on the compiler you''re using in your class I can see justifications for the latter. The std::string implementations in MSVC 5 and 6 were just wretched. In particular, the nature of their reference counting mechanism made proper multi-threaded programming with them hideously difficult.


Are you certain they did COW?

Share this post


Link to post
Share on other sites
quote:
Original post by Magmai Kai Holmlor
Are you certain they did COW?

Positive. I''m looking at the std::basic_string implementation for MSVC 6 right now. (It''s in the xstring file if you want to follow along.)

Share this post


Link to post
Share on other sites