logo

Crowdly

Browser

Add to Chrome

C++ supports dynamic allocation of objects on the free store [or heap] using a  ...

✅ The verified answer to this question is available below. Our community-reviewed solutions help you understand the material better.

C++ supports dynamic allocation of objects on the free store [or heap] using a new expression that begins with keyword new followed by the type of the object with dynamic storage duration. New expressions create objects of the given type on the free store and return a pointer to the newly minted object. If T is the type of an object, new T is an expression that allocates an uninitialized object of type T on the free store and yields a pointer to this [unnamed] newly allocated object. As an example,

int *p = new int;

will allocate an unnamed and uninitialized object of type int, and cause p to point to that object. It is possible to give a specific value to use when initializing the object by executing the function-style expression new T(args) or the more modern uniform initialization expression new T{args}. As an example,

int *p = new int {42};

will allocate an unnamed object of type int on the free store, initialize the unnamed object to , and cause p to point to that object.

C++ supports deallocation of a dynamic object using a delete expression, which is composed of keyword delete followed by a pointer to an object that was previously allocated by a new expression. A delete expression always returns void. To deallocate the object pointed to by p, you would use the following delete expression:

delete p;

The value contained in memory where the deleted object resided is undefined, meaning the compiler can produce code that leaves anything there. In practice, compilers will try to be as efficient as possible, so typically the object’s memory will remain untouched until the program reuses it for other purposes.

Although new expressions and delete expressions seem to have the same functionality as C standard library functions malloc and free, respectively, they've multiple differences:

  1. For a user-defined type, the new expression not only allocates memory for an object but also calls the object's constructor to initialize the object. On the other hand, function malloc just allocates the appropriate memory for storing the object. Fundamental data types have no constructors, so they will be default initialized meaning that the object is uninitialized and its value is undefined.
  2. The new expression returns the exact type of the allocated object, while function malloc returns a pointer of type void*, so typecasting is required. For example if A

    is an user-defined type, we define an object on the free store like this:

    A *pa {new A};

    whereas the value returned by function

    malloc must be typecast to type A*

    :

    A *pb = static_cast<A*>(malloc(sizeof(A)));

  3. Function malloc signals failure [for example, when the program is out of heap memory] by returning a null pointer where as the new expression throws an exception that requires the programmer to use C++ exception handling mechanisms to avoid program termination.
  4. While function free does nothing more than return the previously allocated memory back to the heap, the delete expression will first call the destructor function of the object's type followed by returning the memory back to the heap.

It should now be clear that C standard library functions malloc, calloc, realloc, and free must not be used in C++ programs except in exceptional cases. Such an exceptional scenario arises when a C++ program interfaces with an API that was implemented in C. Suppose the C++ program makes a call to an API function that returns a pointer pointing to dynamically allocated memory. [which presumably was allocated by the execution of function malloc]. To avoid memory leaks, the dynamically allocated memory must be returned to the free store. In this scenario, the only recourse available to the C++ program is to return the previously allocated dynamic memory back to the free store using C standard library function free.

If the following program doesn't compile, write . If the program has undefined behavior, write . If the program prints unspecified values to standard output, write . Otherwise, write the exact values written to the standard output stream.

#include <iostream>

#include <stdlib.h>

struct Vector {

int x, y, z;

Vector() : x{0}, y{0}, z{0} {} // default constructor

};

int main() {

Vector *pV {static_cast<Vector*>(malloc(sizeof(Vector)))};

std::cout << pV->x << pV->y << pV->z;

free(pV);

}

More questions like this

Want instant access to all verified answers on distance3.sg.digipen.edu?

Get Unlimited Answers To Exam Questions - Install Crowdly Extension Now!

Browser

Add to Chrome