The memory for the string may or may not be in the string class. A possible (and possibly) class string will manage its own memory, having only a pointer to the memory used to store data. Example:
struct Airlane { String Name { char *data; // size = 4 size_t size; // size = 4 } int diameter; // size = 4 int weight; // size = 4 }; // size = 16
Please note that they are not necessarily the actual sizes; they, for example, are simple.
Also note that in C ++ (unlike C, for example), for each class T , sizeof T is a compile-time constant, which means that objects can never have dynamic size. This actually means: as soon as you need dynamic runtime size data, there should be external areas of memory (wrt the object). This may mean using standard containers such as std::string or std::vector , or even manually managed resources.
This, in turn, means that operator new does not have to know the dynamic size of all members, recursively, but only the size of the outermost class that you allocate. When this outer class needs more memory, it must manage it itself. Example p-code:
Airline* myPlane = new Airline { Name = { data = new char[some-size] ... } ... }
Internal allocations are performed by holding designers:
Airline::Airline() : string(), ... {} string::string () : data(new char[...] ... {}
operator new does nothing but allocate some fixed-size memory as โsoilโ for Airline (see the first p-code), and then the Airline seed constructor, which itself must manage its lifetime in that limited amount of โsoilโ by calling the string constructor (implicitly or explicitly), which itself executes another new .
Sebastian mach
source share