Jump to content

C Strings, C++ Strings?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
20 replies to this topic

#1
telboon

telboon

    Newbie

  • Members
  • PipPip
  • 26 posts
I'm currently writing in C++. There're 2 kinds of strings that can be used, char[], and String. Which one will be recommended? What are the pros and cons of either choices?

I'm actually more comfortable with the former, char[]. However, I want to maintain a good habit of programming, so if String is better, I guess it'll be good for me to get used to the better one. So, should I change, or should i not?

#2
LukeyJ

LukeyJ

    Learning Programmer

  • Members
  • PipPipPip
  • 93 posts
C strings are faster, but insecure. You have to use a lot more code to make these secure. C++ strings are secure, offer more functionality and are easier. There is a slight performance decline as far as I'm aware but not enough. I think they are good practice and your code will be more secure. They are also easier to pass to functions and objects while keeping an efficient use of system resources (use only as much memory as they need).

Hope this helps.

Edit: Actually, I was referrering to pointers to char as a C-style string. If you are using character arrays. Any chances in the string size must be within the limits of the size initiated with. If this is over, you need to copy to a new char [] and delete the old one, which is awkward. You will use less memory by using C++ strings.

#3
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
Lukey J you are wrong in quite a few things you say there...

C strings insecure, how? That doesn't make sense.
C++ strings in no way offer more functionality. What can you do with C++ strings that I can't do with char arrays.
It is simple to allocate just enough memory for char arrays also so that is a moot point.
It is in now way "easier" to pass C++ style strings to functions, that doesn't even make sense.
It's called using a dynamically allocated string (see malloc and realloc). There is no reason to make and delete arrays.
No, you will not use less memory using C++ strings if you use C char arrays correctly.

Please don't post blatantly wrong information.

#4
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts

telboon said:

I'm currently writing in C++. There're 2 kinds of strings that can be used, char[], and String. Which one will be recommended? What are the pros and cons of either choices?

I'm actually more comfortable with the former, char[]. However, I want to maintain a good habit of programming, so if String is better, I guess it'll be good for me to get used to the better one. So, should I change, or should i not?
I'd recommend that you prefer std::string in C++ (not that C and C++ are case sensitive -- if there is some 3rd party String out there, that would be a different matter). Advantages would be STL functions overloaded to make you life much simpler and easier when manipulating strings. A disadvantage might be the fact that it is not POD.

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Methodz: The reason C-strings are less secure is because you must manually manage their memory. If you do it correctly, there isn't a problem. If you do it incorrectly (which is very easy to do), you have no idea what may happen. The string class has more overhead, but also makes it harder to run beyond the end of your data into other variables.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#6
LukeyJ

LukeyJ

    Learning Programmer

  • Members
  • PipPipPip
  • 93 posts

MeTh0Dz|Reb0rn said:


C strings insecure, how? That doesn't make sense.
As winged explained. If someone else modifies your code or uses you code afterwards it's a lot easier for them to mess up the behaviour of c-style strings.

MeTh0Dz|Reb0rn said:

C++ strings in no way offer more functionality. What can you do with C++ strings that I can't do with char arrays.

Also true, but this takes usually requires more code in order to protect them from overrunning the end of the string. Joining strings take more code than in c++ style strings.


MeTh0Dz|Reb0rn said:

It is in now way "easier" to pass C++ style strings to functions, that doesn't even make sense.

You mean "no" way easier? You have to send an additional parameter, the size of the array while with C++ strings they act a lot more naturally.


MeTh0Dz|Reb0rn said:

It's called using a dynamically allocated string (see malloc and realloc).
I'm very aware of this, by the use of malloc and realloc instead of new and delete you are clearly trying to code C in C++, while that is useful in performance critical programs in some instances, it is not consistant.

MeTh0Dz|Reb0rn said:

There is no reason to make and delete arrays.
No, you will not use less memory using C++ strings if you use C char arrays correctly.
When you are joining strings, there is need. This is a lot simpler and managed by c++.

Methodz, you should be aware by now, that in programming there are many ways to skin a cat. There are no right and wrong answers as to what's better, just advantages and disadvantages and it is a matter of personal taste. You may have to come to terms with that.

#7
telboon

telboon

    Newbie

  • Members
  • PipPip
  • 26 posts
however, many standard functions uses char[] as a input parameter. is there a way to pass c++ String to it?

#8
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
You can use the .c_str() method.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#9
Aereshaa

Aereshaa

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 790 posts

LukeyJ said:

As winged explained. If someone else modifies your code or uses you code afterwards it's a lot easier for them to mess up the behaviour of c-style strings.
As long as you know how much space was allocated, you can be extremely safe with external string e.g. in functions.

Quote

Also true, but this takes usually requires more code in order to protect them from overrunning the end of the string.
Is it that hard to make a loop while(s[i])?

Quote

Joining strings take more code than in c++ style strings.
No. it just takes one function call: s = strcat(s,s2);

Quote

You mean "no" way easier? You have to send an additional parameter, the size of the array while with C++ strings they act a lot more naturally.
You don't need to pass the length, C strings are null terminated! Learn some basic C before saying things like that.

Quote

When you are joining strings, there is need. This is a lot simpler and managed by c++.
Yes, at a cost in speed. Do you think your time is more valuable than the user's?
Watches: Nanoha, Haruhi, AzuDai. Listens to: E-Type, Dj Melodie, Nightcore.
"When people are wrong they need to be corrected. And then when they can't accept it, an argument ensues." - MeTh0Dz

#10
LukeyJ

LukeyJ

    Learning Programmer

  • Members
  • PipPipPip
  • 93 posts
Within reason. If performance was that critical we'd all be writing in assembly. I'm very aware of the strcat function. You have to ensure that the source countainer is big enough. It's also better use the secure version of the function. Either way it requires more code, as you can't predict what input the user will give. You have to put more work in to protect c-style strings... if performance is not critical it is better to be on the safe side and use C++ strings.

#11
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
LukeyJ, you are wrong give it up.

Wow people like you act like performance isn't important are so ridiculously clueless. You want to get the best performance you can whenever possible, regardless.

Also wrong, it'd be the destination "container", also if you can't keep track of that then you shouldn't be coding in C. Also strncat takes care of that.

You don't have to predict user input. Use fgets() or use scanf() with a boundary.

Wrong, again.

#12
outsid3r

outsid3r

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 623 posts
Both Methodz and Aereshaa understand people's idea but they insist on protecting C with their own life. Everyone knows that C allow us to do EVERYTHING that we can do in C++ and also with better performance, but handling strings in C SOMETIMES can cost more code and it's a fact that you can't deny. In my opinion both of you must learn to respect people's choice, some people prefer C while others prefer to stick with C++ and another fact is that C++ is SYNTACTICALLY easier to handle strings but i'm not saying that this makes them better, C cost more code but in the end they will operate a lot faster...well, it's all about people's choice.
Anyway, like lykeyj said if performance was critical we'd all be writting in asm.

Resume:
  • C strings work a lot faster than C++ strings.
  • C strings requires manual memory management to handle them.
  • Some functions available in C++ string class are not available in C standard string lib, you must make them manually.
  • C++ string class in most of cases offers more flexibility to syntatically handle string operations. This means that's it's easier to handle strings in C++.
  • With C++ string class you don't have to worry about memory management or string null char termination.