0

How can strings be allocated in the heap using pointers. Strings can have variable size then how can they given a space in heap?

Like suppose yoy want to store the names of guest coming to your party. int guest_num; Int *gsts; Cin>>guest_num; Gsts = new string[guest_num]; Now how will the computer allocate memory for strings who's size is not yet determined?

4th Jan 2020, 2:34 PM
Sajal Chuttani
Sajal Chuttani - avatar
3 Réponses
+ 1
FYI, there's a problem with your code example. <gsts> variable in your example is an `int` pointer (assumed `int` although written as `Int`). The C++ std::string is a template class fully capable for managing memory as necessary, it is far different to the plain `char` array which is the C string. Expanding and shrinking the buffer is done internally, as necessary. How it works under the hood is far beyond my limited knowledge for understanding. I only have one word if I were to describe it "complex".
4th Jan 2020, 2:44 PM
Ipang
+ 1
Thanks Ipang
4th Jan 2020, 2:48 PM
Sajal Chuttani
Sajal Chuttani - avatar
0
You're welcome buddy 👌
4th Jan 2020, 2:48 PM
Ipang