You are not logged in.
Pages: 1
I'm making my way through Practical C++ Programming... One of the things the book advises is to avoid using C-style strings if possible, as the std::string class is easier to manage. However, std::string is mutable, which IIRC introduces some potential security issues and may hamper performance.
So for future reference, is there any commonly used library that provides an immutable string class for C++, as in C# or Java? I've found a few, but they don't look to be very popular.
(Assuming such a thing would be a good idea. If not, well, my excuse for now is that I'm a rank newbie.)
Offline
I assume by mutable you're meaning non-constant size.
As far as i know, strings have not the same problem like char pointers with memcpy or strcat when you overflow the size, so, i don't see any potential security risk there. Obviously, there is a performance "issue" when you need to increase the size of the string as it has to request a change on size, copy and so, but all of that is handled by the class.
If you want some kind of static sized string (c++ class) what I think that might be useful is handling the size by yourself, always controlling that you're capacity after what you're going to do is not going to be greater than the size you want to not exceed.
I'm a student so maybe I'm not seeing something that can help you with better results than what I said.
PS: I have the feeling that maybe i didn't spelled or used any English word or construction correctly, but as tired as I am right now, i can't see what it is. I apologize in case I misspelled something
Best Testing Repo Warning: [testing] means it can eat you hamster, catch fire and you should keep it away from children. And I'm serious here, it's not an April 1st joke.
Offline
By mutable I mean the contents can be changed on the fly. From what I understand, in C# and Java all strings' contents are fixed. Methods that change a string don't actually change the contents of that location in memory; instead they return a new string with the required changes, which you can assign a variable too. But with std::string, you can actually change the string's contents and length dynamically. From what I've read and heard elsewhere this can cause all kinds of hairiness.
I didn't have any trouble reading that post, BTW, though I can't speak for anyone else.
Offline
If you don't want your string to change, declare it as a const... Or am I missing something here?
Offline
However, std::string is mutable, which IIRC introduces some potential security issues and may hamper performance.
Well, in fact, mutable objects give better performance than their immutable counterparts... Try to loop on a non mutable String in Java/C# to append it 10000 times a simple character and compare with the same treatment with a mutable one (StringBuilder) and you will see a huge advantage for mutable strings... BUT, immutable objects have a lot of advantage over mutable ones : they are thread safe, the compiler may optimize them, reuse them, etc.
As for your question about C++, i don't know enough the language to give you an answer, sorry.
Last edited by jaco (2011-03-21 23:06:33)
Offline
Wow there's a lot of confusion in this thread about a fairly common programming concept. Although, admittedly, I don't expect most Arch users to prefer the types of languages with immutable strings.
Most purely object-oriented languages (Java, C#, etc.) treat strings as immutable objects, in the same way that the number 5 is immutable. This is largely a philosophical choice; two strings that consist of the same characters really should be considered the same object. However, there are a few implications it has on programming:
- C-style "str[3] = 'q';" statements don't work any more, but there's usually a StringBuilder class for doing stuff like that.
- substr() becomes super fast. Since the contents of a string are immutable, substr() can return a string object that wraps the appropriate substring in-place
- immutable strings are obviously thread-safe
- identical string constants can be "interned" -- made to be references to the same object
- and a few more I can't think of
HOWEVER, as long as you understand how the implementation works, there is no reason for string algorithms to be slower on either implementation. Only when attempting to use C# strings in a C way, or visa versa, will your algorithm be slower. And C++ is so much faster than those languages that you really should not be afraid of the performance differences anyway.
In response to Allan, a "const std::string" is not equivalent to a Java/C#-style string, because you can't do this in C++:
const std::string str = "foo";
str = "bar";
Edit: here's a decent explanation from the Java point of view.
Last edited by tavianator (2011-03-22 06:14:28)
Offline
In response to Allan, a "const std::string" is not equivalent to a Java/C#-style string, because you can't do this in C++:
const std::string str = "foo"; str = "bar";
Hmm... I'm lost! Can you change an immutable string in java? I thought the point of being immutable was not to be able to change it...
Offline
immutable in java/c# means that whenever you change the value of the object , a new object is created.
eg
string s="foo";
s="bar"; // Here the old object is destroyed and a new object is created.
Tamil is my mother tongue.
Offline
immutable in java/c# means that whenever you change the value of the object , a new object is created.
Ah, thanks. That is quite different to the impression I got from the name. (obviously!)
Offline
Given
const char constant_string1[]="some string";
const std::string constant_string2="hello world";
One will end up in the "read only data" section of the binary and the other one will be in the "uninitialized data" section. If it's a string that's never changes, it's always better to have it in the "read only data" section of your binary. (see nm)
Offline
Pages: 1