How does a string work?

closed account (42TXGNh0)
Inspired by this reply: http://www.cplusplus.com/forum/beginner/248142/3/#msg1094076

So, literally, string is an array of a bunch of character. But what it's different from array is, it can resize itself no matter how many time you want and it the size of it need not be known in compilation time. Out of curiosity, how does string work like which:

1. allows to let its size not to be known in compilation time and
2. can resize itself?
Last edited on
The string class uses dynamic memory allocation to manage the storage of the characters. This allows it to allocate memory when it needs to, to store whatever it's told to store.
closed account (42TXGNh0)
How is
dynamic memory allocation
looks like? Are there any examples, please? Thanks.
This should be adequately covered by your texbook. No C++ textbook of any value would fail to include a section about this.

Alternatively, the tutorial on this website as a "Dynamic Memory" section that contains examples.
closed account (42TXGNh0)
Like... this?: https://www.tutorialspoint.com/cplusplus/cpp_dynamic_memory.htm
Yes, that looks like it covers the same ground as the tutorial on this website that I suggested.
You need to know about pointers first though. So if you don't know about pointers, look it up or see the article on this website itself: http://www.cplusplus.com/doc/tutorial/pointers/

There's a page for dynamic allocation too: http://www.cplusplus.com/doc/tutorial/dynamic/
Topic archived. No new replies allowed.