Skip to main content
gamedev.net
🔒 Locked

a string class

Started by okonomiyaki May 6, 2004 at 2:12 AM 4 replies 350+ views
Original Post
okonomiyaki
okonomiyaki
I''m developing my own string class just for the fun of it. In past projects I''ve learned the value of copying over character arrays instead of just pointing to them. For example:
mystring hello = "Hello";
 
In the "=" operator, I know that I shouldn''t just point the internal character array to the passed in char pointer. This is because it''s allocated on the heap and will be destroyed.. causing problems. So in the "=" operator, should I allocate char space and then do a strcpy? How does the string class (#include <string>) do it? Thanks.
ChaosEngine
ChaosEngine
pretty much. There are a lot of optimisations as well, things like reserving more space then you need at the moment and implementing a reference couted copy on write system.
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight
Jingo
Jingo
quote:
Original post by okonomiyaki
I''m developing my own string class just for the fun of it. In past projects I''ve learned the value of copying over character arrays instead of just pointing to them. For example:


mystring hello = "Hello";


In the "=" operator, I know that I shouldn''t just point the internal character array to the passed in char pointer. This is because it''s allocated on the heap and will be destroyed.. causing problems. So in the "=" operator, should I allocate char space and then do a strcpy? How does the string class (#include <string>) do it?

Thanks.


In the example inside your code, the = operator will probably never be called. The compiler will construct the hello mystring object using the mystring(const char[]) constructor.

Also the "Hello" string is not on the heap(i think you meant stack anyway, heap objects are only destroyed when you delete them or call the destructor manually), it is a string literal with static storage duration, meaning it will exist for the duration of the program, and wont be automatically destroyed until main() exits or you call the exit() function.

That said, most std::string implementations have a small stack allocated char buffer inside the class that you can use for small strings, it saves having to allocate lots of small strings to the heap, and if the buffer isnt big enough to hold the string then you allocate it to the heap.
Reference counting also helps improve performance when you do alot of string copying and concatenation.
okonomiyaki
okonomiyaki
Ah, you are right, I meant stack. I was trying to say that problems will occur if that''s inside a class member function, where the stack string is deleted after the function exits.. so I guess a strcpy is necassary, with certain optimizations like you said I guess it wouldn''t be too bad. Any other opinions on optimization techniques in this situation?
Thanks!
C-Junkie
C-Junkie
You don''t have to copy the string because it stays there, but..

you do have to sopy the string because

mystring s = "Hello";

is indistiguishable from

mystring s = otherchararraythatwillbedeleted;

The first is okay not to copy (until you try to change it, possibly, that is) the second should be copied.

THAT is why you always have to copy.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.