Jump to content
  • Advertisement

Archived

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

Rob Loach

Char Array vs String

This topic is 5487 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!