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?
C Strings, C++ Strings?
Started by telboon, Nov 19 2008 09:49 AM
20 replies to this topic
#1
Posted 19 November 2008 - 09:49 AM
|
|
|
#2
Posted 19 November 2008 - 09:57 AM
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.
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
Posted 19 November 2008 - 10:29 AM
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.
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
Posted 19 November 2008 - 11:13 AM
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'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?
#5
Posted 19 November 2008 - 11:44 AM
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.
#6
Posted 20 November 2008 - 06:23 AM
MeTh0Dz|Reb0rn said:
C strings insecure, how? That doesn't make sense.
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).
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.
No, you will not use less memory using C++ strings if you use C char arrays correctly.
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
Posted 20 November 2008 - 07:52 AM
however, many standard functions uses char[] as a input parameter. is there a way to pass c++ String to it?
#8
Posted 20 November 2008 - 07:55 AM
You can use the .c_str() method.
#9
Posted 20 November 2008 - 01:03 PM
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.
Quote
Also true, but this takes usually requires more code in order to protect them from overrunning the end of the string.
Quote
Joining strings take more code than in c++ style strings.
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.
Quote
When you are joining strings, there is need. This is a lot simpler and managed by c++.
#10
Posted 20 November 2008 - 01:45 PM
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
Posted 20 November 2008 - 02:56 PM
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.
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
Posted 20 November 2008 - 03:08 PM
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:
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.


Sign In
Create Account


Back to top









