C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-02-2009, 07:49 AM   #1
Registered User
 
Join Date: Dec 2007
Location: France
Posts: 396
string vs dynamic char array

We can store data dynamically in a string just like in a dynamic char array, right?

So "string str;" is almost the same as "char * ch = new char[64];"
only much better because we dont have to define the max size?

If the answer is yes i wonder why people continue to complicate their lives by using
dynamic char arrays when they can use strings as well?
__________________
Using Code::Blocks,MingW with Windows.

Last edited by Ducky; 11-02-2009 at 11:54 AM.
Ducky is offline   Reply With Quote
Old 11-02-2009, 10:07 AM   #2
Jack of many languages
 
Join Date: Nov 2007
Location: Katy, Texas
Posts: 1,929
Yes.

Yes.

Certain functions still require char arrays.
__________________
Mac and Windows cross platform programmer. Ruby lover.

Memorable Quotes From Recent Posts:

I can't remember.
Dino is offline   Reply With Quote
Old 11-02-2009, 10:30 AM   #3
Registered User
 
Join Date: Jan 2005
Posts: 7,137
>> Certain functions still require char arrays.
Even then the string is usually better with a simple conversion done before working with the other functions.

It might make sense to use a non-dynamic char array instead of the string class in some cases, although those cases are somewhat rare.
Daved is offline   Reply With Quote
Old 11-02-2009, 10:53 AM   #4
crazy
 
Join Date: Jun 2008
Posts: 26
Use strings, and if you need an old school string for any reason you can do this:

string lolwat = "hai therreee";
// ... lolwat.c_str();

Of course, don't forget to distinguish between C and C++ when reading random source online, which is probably where you confusion comes from. Most people carelessly mix the two languages, and in C there is no string class because classes don't exist in C. But there still are libs and ways to write functions to manage a 'dynamic' arrays in C.

Last edited by gltiich; 11-02-2009 at 10:58 AM.
gltiich is offline   Reply With Quote
Old 11-02-2009, 11:45 AM   #5
Registered User
 
Join Date: Dec 2007
Location: France
Posts: 396
Thats awesome and crystal clear, strings all the way from now on!

Thanks a lot everyone!
__________________
Using Code::Blocks,MingW with Windows.
Ducky is offline   Reply With Quote
Old 11-02-2009, 10:10 PM   #6
and the hat of sweating
 
Join Date: Aug 2007
Location: Toronto, ON
Posts: 3,120
If you need to pass a char* to a function instead of a const char*, you can use a vector<char> like this:
Code:
std::string str = "Hello World";
std::vector<char> vec( str.begin(), str.end() );
vec.push_back( '\0' );  // I can't remember if this is required, but it doesn't hurt.
vec.reserve( 100 );  // Make extra room for the string to be written into.

old_c_function( &vec[0], vec.capacity() );

// Now put it back into the string.
str = &vec[0];
__________________
"I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008
cpjust is offline   Reply With Quote
Old 11-03-2009, 03:27 AM   #7
Registered User
 
Join Date: Dec 2007
Location: France
Posts: 396
Thank you Cpjust, i think that's just what i was looking for!

Using a vector element as a string
__________________
Using Code::Blocks,MingW with Windows.
Ducky is offline   Reply With Quote
Old 11-03-2009, 12:03 PM   #8
Registered User
 
Join Date: Jan 2005
Posts: 7,137
Note that this part is not quite correct:
Code:
old_c_function( &vec[0], vec.capacity() );
It should be:
Code:
old_c_function( &vec[0], vec.size() );
The internal data in the vector will not be accurate if you use capacity, because capacity only tells you how much space was allocated, but the size tells you how many actual chars are available to be modified.
Daved is offline   Reply With Quote
Old 11-03-2009, 12:25 PM   #9
Registered User
 
Join Date: Sep 2004
Location: California
Posts: 2,845
Quote:
Originally Posted by Daved View Post
Note that this part is not quite correct:
Code:
old_c_function( &vec[0], vec.capacity() );
It should be:
Code:
old_c_function( &vec[0], vec.size() );
The internal data in the vector will not be accurate if you use capacity, because capacity only tells you how much space was allocated, but the size tells you how many actual chars are available to be modified.
No, capacity is correct.
__________________
bit∙hub [bit-huhb] n. A source and destination for information.
bithub is offline   Reply With Quote
Old 11-03-2009, 12:28 PM   #10
Algorithm Dissector
 
iMalc's Avatar
 
Join Date: Dec 2005
Location: New Zealand
Posts: 2,476
Quote:
Originally Posted by bithub View Post
No, capacity is correct.
Correct in the sense that it might pass the integer value you were expecting, but incorrect in the sense that any characters written to beyond the value of size() are surely undefined behaviour.
__________________
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
iMalc is offline   Reply With Quote
Old 11-03-2009, 12:29 PM   #11
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,381
Quote:
Originally Posted by bithub View Post
No, capacity is correct.
I don't think so, because although the characters from vec[0] to vec[capacity-1] may be writable, the vector itself doesn't consider them to be part of the current value. They are just available space.

The vector tracks the size of its contents, so it isn't really correct to go behind vector's back and write into the space beyond end(), even if that space is reserved. It will have no way of knowing that you did that.
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Old 11-03-2009, 12:30 PM   #12
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,364
Quote:
Originally Posted by bithub
No, capacity is correct.
In practice for vectors of objects of built-in types (or maybe POD types in general), it probably works okay. However, semantically and more likely as an issue for non-POD objects, it is not correct.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 11-03-2009, 12:35 PM   #13
Registered User
 
Join Date: Sep 2004
Location: California
Posts: 2,845
My comment was just for the context of the posted code. In that case, capacity() is correct and size() is incorrect (since size() would return 0, and thus fail for the given example). I realize that after the function call, the vector is in an undefined state (and that size() will probably still return zero after the function call), but in the posted code, this was irrelevant.
__________________
bit∙hub [bit-huhb] n. A source and destination for information.
bithub is offline   Reply With Quote
Old 11-03-2009, 12:39 PM   #14
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,364
That does not imply that capacity() is correct. Rather, that implies that reserve() is incorrect, and resize() should be used instead.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 11-03-2009, 12:51 PM   #15
Registered User
 
Join Date: Jan 2005
Posts: 7,137
I misread the original code and thought it used resize instead of reserve.

Using reserve and capacity is wrong in the sense that the vector's internal variables will be out of sync with the data.

You should use resize and size instead as laserlight said. Sorry for the confusion.
Daved is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to compare string with 'getter' returned string. Swerve C++ Programming 2 10-30-2009 05:56 PM
[Inheritance Hierarchy] User Input on program with constructors. How ? chandreu C++ Programming 8 04-25-2008 02:45 PM
Personal Program that is making me go wtf? Submeg C Programming 20 06-27-2006 12:13 AM
Character arrays PsychoBrat C++ Programming 7 06-21-2002 12:02 PM
How do you search & sort an array? sketchit C Programming 30 11-03-2001 05:26 PM


All times are GMT -6. The time now is 06:07 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22