stl::String vs Char*

Started by
8 comments, last by GOOSEDUMP 22 years, 6 months ago
what are the advantages/dissadvantages of both? In what situations whould one be clearly benificial?
Advertisement
Assuming you have the choice...

Use std::string when you need text strings that you will manipulate extensively (append, concatenate, search, truncate, copy, etc).

Use char* when you need a pointer, have a static string that wont change (char str[] = "Here we go, yo!") or when you find yourself spending too much time converting back and forth from std::string for manipulation to char* for function use (I know it''s only string.c_str(), but that has overhead).

Not to forget that most of the security holes in net are because of buffer overflows, that is, using limited sized char arrays. std::string is pretty safe... (not absolutely safe, but way safer than char* ..)

What comes to c_str(), when dealing with basic std::string (or std::basic_string , if I remember correctly), c_str just returns a (const) pointer to string''s internally used char array...
AFAIK, that is.

And you shouldn''t use std::strings (or any containers!) when calling dll functions. That''s just asking for trouble... (oh, the hours I have spent debugging those problems...)
~~~ "'impossible' is a word in the dictonary of fools" --Napoleon
quote:Original post by Hway
And you shouldn''t use std::strings (or any containers!) when calling dll functions. That''s just asking for trouble... (oh, the hours I have spent debugging those problems...)


I assume you used VC++ default STL? If so, that''s because VC++ strings uses ref counting. Use some other STL and it *should* be fine.

quote:Original post by Hway
std::string is pretty safe... (not absolutely safe, but way safer than char* ..)

How is it unsafe?
quote:
And you shouldn''t use std::strings (or any containers!) when calling dll functions. That''s just asking for trouble... (oh, the hours I have spent debugging those problems...)

Works for me.

You are asking for trouble with STL and dll if the dll was compiled with another version of the STL. But if the dll was compiled using the same STL, you shouldn''t have any problem.

dlls are simply lib file linked at load time or run time.


I just wanted to add that even for strings that never changes, I would still use std::string, because you never know when you will need to use function like substrings.
I know it''s probably bad, but I just use a homebrewn string class I cooked up a while back. Reinventing the wheel? Yeah, probably. It''s not as efficient as the std::string, but I like its interface more.

---
blahpers
---blahpers
And don''t forget that you probably want to use std::string where you might not be using standard ASCII - For example, UNICODE or representations of asian languages are 2 byte characters. That''ll screw up your char* handling very quickly

--

MP3 Dancer


Games, Anime and more at GKWorld

Meet Bunny luv'' and the girls, ready to perform on your desktop just for you (warning: adult content).

quote:Original post by taliesin73
And don''t forget that you probably want to use std::string where you might not be using standard ASCII - For example, UNICODE or representations of asian languages are 2 byte characters. That''ll screw up your char* handling very quickly


_wchar * for static (always) Unicode support; or
_tchar * for dynamic Unicode support (if UNICODE is defined).
quote:Original post by Oluseyi

_wchar * for static (always) Unicode support; or
_tchar * for dynamic Unicode support (if UNICODE is defined).


yes ...

But I believe std::string will silently handle conversions for you in the event you need to mix the two.

Nevertheless, point taken. If you declare either of those in place of char, you get back to the basic problem of which one to use without worrying about UNICODE

--

MP3 Dancer


Games, Anime and more at GKWorld

Meet Bunny luv'' and the girls, ready to perform on your desktop just for you (warning: adult content).

This topic is closed to new replies.

Advertisement