14

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

1
  • my guess, not knowing C++, is that it stores only a pointer to the string, whose actual contents are somewhere in the heap. Mar 5, 2012 at 15:52

5 Answers 5

13

An std::string object is fixed-size; it contains a pointer to an actual buffer of characters along with its length. std::string's definition looks something like

class string
{
    char *buffer;
    size_t nchars;

  public:
    // interface
};

It follows that your Airline objects also have a fixed size.

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

0
3

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

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

This is because the compiler knows the fields inside the structure, and adds up the sizes of each structure member.

The string object is no different than your structure. It is actually a class in the standard library, and not a special type like int or float that is handled by the compiler. Like your structure, the string class contains fields that the compiler knows the size of, and so it knows the size of your complete structure and uses that when you use new.

2

The call to new will allocate sizeof(Airline) which is what is needed to hold an object of type Airline.

As of the management for strings, the string object holds some internal data to manage the memory of the actual data stored, but not the data itself (unless the small object optimization is in use). While the idea is the same that has been pointed by others with stores a pointer to the actual string, that is not precise enough, as it implementations will store that pointer plus extra data required to hold the size() and capacity() (and others, like reference counts in reference counting implementations).

0
1

The memory for the string may or may not be within class string. Possible (and probably), class string will manage its own memory, having only a pointer to the memory used to store the 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

Note that those are not necessarily actual sizes, they are just for example.

Also note that in C++ (unlike C, for example), for every class T, sizeof T is a compile time constant, meaning that objects can never have dynamic size. This in effect means: As soon as you need runtime dynamic sized data, there have to be external (w.r.t. the object) memory areas. This may imply the use of standard containers like std::string or std::vector, or even manually managed resources.

This in turn means, operator new does not need to know the dynamic size of all members, recursively, but only the size of the outermost class, the one that you allocate. When this outer class needs more memory, it has to manage it itself. Some exemplary p-code:

Airline* myPlane = new Airline {
    Name = {
        data = new char[some-size]
        ...
    }
    ...
}

The inner allocations are done by the holding constructors:

Airline::Airline() : string(), ... {}
string::string () : data(new char[...] ... {}

operator new does nothing else but to allocate some fixed size memory as the "soil" for Airline (see first p-code), and then "seeds" Airlines constructor, which itself has to manage its lifetime in that restricted volume of "soil", by invoking the string constructor (implicitly or explicitly), which itself does another new.

0

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

A string will always be the same size on the stack. However, internally, the string stores a pointer to a character array.

1
  • You might want to add where new allocates and how the return of new relates to that location. Mar 5, 2012 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.