Char Array vs String

Started by
31 comments, last by Rob Loach 20 years, 10 months ago
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
Rob Loach [Website] [Projects] [Contact]
Advertisement
I don''t know but IF you code professional, then I guess you would go for Char arrays. The sizes are defined and easy to save/load to files.

.lick
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.
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]
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.
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
Have fun BunnzPunch'n'Crunch
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.
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
unkn.Enigma1625
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."
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
- John

This topic is closed to new replies.

Advertisement