Archived

This topic is now archived and is closed to further replies.

Rob Loach

Char Array vs String

Recommended Posts

Which one is faster when dealing with real-time games? And also, which one do you use and prefer? I currently use strings because I find it easier than using a char array.
- Rob Loach Current Project: Go Through Object-Oriented Programming in C++ by Robert Lafore "Do or do not. There is no try." - Yoda

Share this post


Link to post
Share on other sites
Char arrays can be useful when dealing with files but otherwise, you should stick to strings as much as possible. Keep in mind that :

1. Strings aren''t slower than char arrays in most cases.
2. Even if they were slower, it wouldn''t be relevant since string operations certainly don''t represent more than 0.0001% of the CPU usage, unless you''re making a ridiculously simple game.
3. They are much easier and safer than char arrays since you don''t have to worry about evil buffer overflows.

Share this post


Link to post
Share on other sites
I use std::string wherever possible for implementing string manipulation and storing strings. Even std::vector[std::string] when i need an array of strings.

As Prosper mentioned, string manipulations shouldnt happed much(if at all) during the mainloop of a game, so it shouldnt be a problem even if they are slower.

Most of my file loading methods still look like this however.
void loadsomthing(const char *filename)

[edited by - gibber on May 3, 2003 1:09:52 PM]

Share this post


Link to post
Share on other sites
Not sure about speed, but for ease of use I use std::string consistently. If I ever need a char* for use in an API call, you just use the c_str() function on a std::string.

Share this post


Link to post
Share on other sites
The drawback of char arrays is, that they don''t store the length of the string and for example a simple strlen has to test every byte for zero. Although it is a very simple representation working with char arrays is very errorprone.

I would strongly suggest strings, also for speed reasons (if strings or char array become the bottleneck of your game, perhaps something is wrong with your code!)

Have fun
Bunnz
Visit: www.bunnz.com

Share this post


Link to post
Share on other sites
I use chars simply because I haven''t needed to do any string manipulation apart from storing filenames for loading, where passing chars is a lot easier.

Share this post


Link to post
Share on other sites
Well in "Effective C++: 50 Ways to Improve Your Programs and Design" the author says that you should use strings as much as possible while avoiding character arrays; it is very rare that you will have to use a char array rather than a string. From now on I''m saying goodbye to char arrays and am focusing on getting up to speed on all the new and convenient features of the standard library.

unkn.Enigma1625

Share this post


Link to post
Share on other sites
There is no good reason to use char arrays in your application, and many to use strings.

You don''t have to worry about dynamic allocation with strings. You get typesafe stream reading and writing. You get first class semantics with strings. You can assign and copy strings. There are a ton of useful functions built into the string class that you get for free. Strings work with all the stl algorithms, too.

And to those who say that string is slower than char[], profile. You''d be surprised at what is actually slow in your code, and if you really need to optimize, you can target the specific part of your program, rather than engaging in premature optimization because "char* is faster."

Share this post


Link to post
Share on other sites
Just to play devils advocate here, I''d say you may want to look into how std::string works a bit more before you commit to it. Others have pointed out that string manipulation shouldn''t be the bottleneck in your game, and they''re probably correct. However, that''s not the only issue. What they haven''t pointed out is that the dynamic allocation you get with std::string may cause severe memory fragmentation if abused.

Personally, I don''t use std::string or raw char arrays...I use my own string class

-John

Share this post


Link to post
Share on other sites
You should keep in mind though, that you might run into difficulties when debugging your code.

SInce the string class uses two different ways of storing your characters, the debugger can get confused (VC70, e.g.). In case your string only consists of 16 characters or less it will be stored internally in a preallocated array of string and is accessible then through the _Buf variable. If it contains more than 16 characters, memory is dynamically allocated and accessible through the _Px pointer.

The VC70 compiler only shows one of them, so if the string is stored in the other, you will see only garbage unless you trace into that string in the debug pane. ;-)

Just keep that in mind when you think everything is alright, but only seeing garbage, ... everything might be alright though ;-))




LightBrain website relaunched, BomberFUN only USD 9.99
visit http://www.lightbrain.de

Share this post


Link to post
Share on other sites
Well you can always do something like this if you have to:

char* szNewString;

szNewString = new char[iValue];

I have had some dodgy function calls that have give me an error message on compliation like:

Error: cannot convert char[10] in to char[5];

So if you get an error you can use the new operator.

Share this post


Link to post
Share on other sites
quote:
Original post by Teknofreek
What they haven't pointed out is that the dynamic allocation you get with std::string may cause severe memory fragmentation if abused.

Have you ever actually looked at STL allocator implementations? There's very little to worry about in terms of memory fragmentation. And if you're really scared by it, just use boost:: Pool.


How appropriate. You fight like a cow.


[edited by - sneftel on May 8, 2003 8:36:32 PM]

Share this post


Link to post
Share on other sites
quote:
Have you ever actually looked at STL allocator implementations? There''s very little to worry about in terms of memory fragmentation. And if you''re really scared by it, just use boost:: Pool.


No, to be honest, I haven''t. To be completely fair, I haven''t used the STL much at all, since we have our own implementations of everything we need at the place I currently work.

However, aren''t the STL implementations up to each vendor? I ask because that''s the impression that I''ve gotten about STL, and I believe is why our company wrote our own stuff way back when.

-John

Share this post


Link to post
Share on other sites
quote:
Original post by Ethereal
So if you get an error you can use the new operator.

IE: If you get compilation errors, just try random things ''till they go away.

...and that is why I believe linux must be destroyed.

Share this post


Link to post
Share on other sites
quote:
Original post by Zipster
Well, yes and no. Any vendor can implement STL the way they wish, however they still have to comply to The Standard.


Yes, but the standard only describes what each class must do, not how it works behind the scene correct? So, one implementation could be much more efficient than the next, or vice-versa? I''ve always written my own functions also, I dislike being restricted with my stuff (I use my own linked lists for my GUI stuff because I can store the "current" pointer of my doubly linked list and render in reverse order so the windows draw properly, and insert a new window right in where current is. I have no need to store a head/tail, is it possible to do this with an stl list?). I wrote my own string class just for the heck of it, and it works great, so I use it.

I use char arrays for things in game, like displaying my frame rate and stuff. I actually have heavy use of char arrays in my game code, while my GUI code uses mostly strings of my own flavor.

Share this post


Link to post
Share on other sites
quote:
Original post by Ready4Dis
Yes, but the standard only describes what each class must do, not how it works behind the scene correct?

That's correct, but the standart describes the time a function may take, and that limits the number of possible implementations almost to one. E.g. std::list is very likely implemented as a linked list of nodes.

For std::string however the maximum time a function will take is not defined by the standart, but the compiler vendors do a great job optimizing it, so it shouldn't be slower than a char array

quote:
Original post by Ready4Dis
I use my own linked lists for my GUI stuff because I can store the "current" pointer of my doubly linked list and render in reverse order so the windows draw properly, and insert a new window right in where current is. I have no need to store a head/tail, is it possible to do this with an stl list?

Yes.

[edited by - novum on May 11, 2003 9:05:29 AM]

Share this post


Link to post
Share on other sites
Does the standard also not specify the algorithmic complexity of most operations? If not it seems like most of the implementations I have come across seem to do a pretty good job of optimizing the operations.

I guess I will have to go dig up ye olde standard and see what the deal is.

Share this post


Link to post
Share on other sites
In the postmortem to Star Wars Starfighter, they mention one of their problems was all the small allocations the string class makes:
http://www.gamasutra.com/features/20010801/corry_03.htm

I know of one development house that outlaws the STL for the same reason. As mentioned in the article, this is more of a console problem. Throw enough memory at it(PC), and the problem goes away.

If you''re developing for PC''s, I''d go with strings.

Share this post


Link to post
Share on other sites
i usually do not use std::string''s very much. i only use them when i need to do string manipulation or to hold strings that i dont know how big they will be. i use char arrays to output stuff to the screen, e.g.:

  
char buf[15];
int count=wsprintf(buf,"%i",num_of_lives);
// display text


the only time ive ever used std::string''s (in a game) is in my game engine to hold all of the errors and then output them to a file at the end of the program.



doh, nuts. Mmmm... donuts
My website

Share this post


Link to post
Share on other sites
quote:
Original post by Ready4Dis
I use my own linked lists for my GUI stuff because I can store the "current" pointer of my doubly linked list and render in reverse order so the windows draw properly, and insert a new window right in where current is. I have no need to store a head/tail, is it possible to do this with an stl list?


quote:
Original post by noVum
Yes.



How so? I would be interested in seeing how this would be accomplished using the STL, maybe i can even write a benchmark that simulates random mouse clicks and time each implementation to see if it''s worth the extra 5 minutes it took to implement my own version, or if spending 30 minutes finding out how to do it in STL would have been worth it .

Share this post


Link to post
Share on other sites
quote:
Original post by Thrump
In the postmortem to Star Wars Starfighter, they mention one of their problems was all the small allocations the string class makes:
http://www.gamasutra.com/features/20010801/corry_03.htm
You could always write your own allocator or use boost:ool. The benefits of using std::string pertain to interoperability with standard algorithms, streams and so forth for free.

quote:
Original post by Ready4Dis
How so?
Store an iterator to the "current" position. Use reverse iterators (which you can obtain via the rbegin and rend methods of every sequential container) to iterate in reverse.

Share this post


Link to post
Share on other sites
quote:
Original post by Oluseyi
Store an iterator to the "current" position. Use reverse iterators (which you can obtain via the rbegin and rend methods of every sequential container) to iterate in reverse.


Sounds simple enough, but I still prefer to do stuff on my own, one less library that needs to be linked . I MAY try the STL, but I am just about positive that it''s going to be slower than my own, and how measureable the difference will be I can''t tell you until I test it . I still think that rolling your own linked list is simple enough and will result in slightly better performance if used correctly, only takes an additional few minutes over STL if you know what you''re doing.

Share this post


Link to post
Share on other sites