string vs dynamic char array

This is a discussion on string vs dynamic char array within the C++ Programming forums, part of the General Programming Boards category; We can store data dynamically in a string just like in a dynamic char array, right?
So "string str;" is ...

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.

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.

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.

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.

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.