C ++: struct and new keyword

I start with C ++, I have the following code snippet:

struct Airline { string Name; int diameter; int weight; }; Airline* myPlane = new Airline; 

My question is: when I call the new method, it allocates memory, if I remember correctly. How does a PC know how much memory is allocated, especially considering that there is a line type there?

thanks

+7
source share
5 answers

The std::string object has a fixed size; it contains a pointer to the actual character buffer along with its length. std::string definition looks something like

 class string { char *buffer; size_t nchars; public: // interface }; 

Consequently, your Airline objects also have a fixed size.

Now new not only highlights; it also initializes your object, including std::string , which means that it probably sets the char pointer to 0 because the string is empty.

+12
source

You can also get the size of the structure using sizeof :

 cout << "sizeof(Airline) = " << sizeof(Airline) << endl; 

This is due to the fact that the compiler knows the fields inside the structure and adds the sizes of each element of the structure.

The string object is no different from your structure. This is actually a class in the standard library, and not a special type of type int or float , which is processed by the compiler. Like your structure, the string class contains fields that the compiler knows about the size, and therefore it knows the size of your full structure and uses this when using new .

+2
source

A call to new will allocate sizeof(Airline) , which is necessary to store an object of type Airline .

Like string management, the string object contains some internal data for managing the memory of the actual data, but not the data itself (unless optimization of small objects is used). While the idea is the same as others pointed out, stores have a pointer to the actual string, which is not accurate enough, since its implementations will store this pointer plus the additional data needed to store size() and capacity() (and others, such as reference counting in reference counting implementations).

+2
source

When you allocate Airline , new allocates enough heap space for the two ints, string and its fields.

A string will always have the same size on the stack. However, inside string stores a pointer to an array of characters.

0
source

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 .

0
source

All Articles