I’m a beginner to C++, I’ve got the following piece of code:
struct Airline {
string Name;
int diameter;
int weight;
};
Airline* myPlane = new Airline;
my question is when I call the method new it allocates memory, if I recall correctly. How does the PC know how much memory to allocate,especially given that there is a string type in there?
Thanks
An
std::stringobject is fixed-size; it contains a pointer to an actual buffer of characters along with its length.std::string‘s definition looks something likeIt follows that your
Airlineobjects also have a fixed size.Now,
newdoes not only allocate; it also initializes your object, including thestd::string, which means it probably sets thecharpointer to0because the string is empty.