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 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.
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.
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.
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.
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.Originally Posted by MeTh0Dz|Reb0rn
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.Originally Posted by MeTh0Dz|Reb0rn
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.Originally Posted by MeTh0Dz|Reb0rn
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.Originally Posted by MeTh0Dz|Reb0rn
When you are joining strings, there is need. This is a lot simpler and managed by c++.Originally Posted by MeTh0Dz|Reb0rn
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.
however, many standard functions uses char[] as a input parameter. is there a way to pass c++ String to it?
You can use the .c_str() method.
As long as you know how much space was allocated, you can be extremely safe with external string e.g. in functions.
Is it that hard to make a loop while(s[i])?Also true, but this takes usually requires more code in order to protect them from overrunning the end of the string.
No. it just takes one function call: s = strcat(s,s2);Joining strings take more code than in c++ style strings.
You don't need to pass the length, C strings are null terminated! Learn some basic C before saying things like that.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.
Yes, at a cost in speed. Do you think your time is more valuable than the user's?When you are joining strings, there is need. This is a lot simpler and managed by c++.
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.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks