![]() |
| | #1 |
| Registered User Join Date: Dec 2007 Location: France
Posts: 396
| string vs dynamic char array 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 | |
| | #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 | |
| | #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 | |
| | #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 | |
| | #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 | |
| | #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 | |
| | #7 |
| Registered User Join Date: Dec 2007 Location: France
Posts: 396
|
__________________ Using Code::Blocks,MingW with Windows. |
| Ducky is offline | |
| | #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() ); Code: old_c_function( &vec[0], vec.size() ); |
| Daved is offline | |
| | #9 | |
| Registered User Join Date: Sep 2004 Location: California
Posts: 2,845
| Quote:
__________________ bit∙hub [bit-huhb] n. A source and destination for information. | |
| bithub is offline | |
| | #10 |
| Algorithm Dissector Join Date: Dec 2005 Location: New Zealand
Posts: 2,476
| 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 | |
| | #11 |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,381
| 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 | |
| | #12 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,364
| Quote:
__________________ 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 | |
| | #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 | |
| | #14 |
| C++ Witch 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 | |
| | #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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |