Memory Allocation/Deallocation?

Isaac picture Isaac · Mar 24, 2013 · Viewed 71.9k times · Source

I have been looking at memory allocation lately and I am a bit confused about the basics. I haven't been able to wrap my head around the simple stuff. What does it mean to allocate memory? What happens? I would appreciated answers to any of these questions:

  1. Where is the "memory" that is being allocated?
  2. What is this "memory"? Space in an array? Or something else?
  3. What happens exactly when this "memory" gets allocated?
  4. What happens exactly when the memory gets deallocated?
  5. It would also really help me if someone could answer what malloc does in these C++ lines:

    char* x; 
    x = (char*) malloc (8);
    

Thank you.

Answer

Joseph Mansfield picture Joseph Mansfield · Mar 24, 2013

The Memory Model

The C++ standard has a memory model. It attempts to model the memory in a computer system in a generic way. The standard defines that a byte is a storage unit in the memory model and that memory is made up of bytes (§1.7):

The fundamental storage unit in the C++ memory model is the byte. [...] The memory available to a C++ program consists of one or more sequences of contiguous bytes.

The Object Model

The standard always provides an object model. This specifies that an object is a region of storage (so it is made up of bytes and resides in memory) (§1.8):

The constructs in a C++ program create, destroy, refer to, access, and manipulate objects. An object is a region of storage.

So there we go. Memory is where objects are stored. To store an object in memory, the required region of storage must be allocated.

Allocation and Deallocation Functions

The standard provides two implicitly declared global scope allocation functions:

void* operator new(std::size_t);
void* operator new[](std::size_t);

How these are implemented is not the standard's concern. All that matters is that they should return a pointer to some region of storage with the number of bytes corresponding to the argument passed (§3.7.4.1):

The allocation function attempts to allocate the requested amount of storage. If it is successful, it shall return the address of the start of a block of storage whose length in bytes shall be at least as large as the requested size. There are no constraints on the contents of the allocated storage on return from the allocation function.

It also defines two corresponding deallocation functions:

void operator delete(void*);
void operator delete[](void*);

Which are defined to deallocate storage that has previously been allocated (§3.7.4.2):

If the argument given to a deallocation function in the standard library is a pointer that is not the null pointer value (4.10), the deallocation function shall deallocate the storage referenced by the pointer, rendering invalid all pointers referring to any part of the deallocated storage.

new and delete

Typically, you should not need to use the allocation and deallocation functions directly because they only give you uninitialised memory. Instead, in C++ you should be using new and delete to dynamically allocate objects. A new-expression obtains storage for the requested type by using one of the above allocation functions and then initialises that object in some way. For example new int() will allocate space for an int object and then initialise it to 0. See §5.3.4:

A new-expression obtains storage for the object by calling an allocation function (3.7.4.1).

[...]

A new-expression that creates an object of type T initializes that object [...]

In the opposite direction, delete will call the destructor of an object (if any) and then deallocate the storage (§5.3.5):

If the value of the operand of the delete-expression is not a null pointer value, the delete-expression will invoke the destructor (if any) for the object or the elements of the array being deleted.

[...]

If the value of the operand of the delete-expression is not a null pointer value, the delete-expression will call a deallocation function (3.7.4.2).

Other Allocations

However, these are not the only ways that storage is allocated or deallocated. Many constructs of the language implicitly require allocation of storage. For example, giving an object definition, like int a;, also requires storage (§7):

A definition causes the appropriate amount of storage to be reserved and any appropriate initialization (8.5) to be done.

C standard library: malloc and free

In addition, the <cstdlib> header brings in the contents of the stdlib.h C standard library, which includes the malloc and free functions. They are also defined, by the C standard, to allocate and deallocate memory, much like the allocation and deallocation functions defined by the C++ standard. Here's the definition of malloc (C99 §7.20.3.3):

void *malloc(size_t size);
Description
The malloc function allocates space for an object whose size is specified by size and whose value is indeterminate.
Returns
The malloc function returns either a null pointer or a pointer to the allocated space.

And the definition of free (C99 §7.20.3.2):

void free(void *ptr);
Description
The free function causes the space pointed to by ptr to be deallocated, that is, made available for further allocation. If ptr is a null pointer, no action occurs. Otherwise, if the argument does not match a pointer earlier returned by the calloc, malloc, or realloc function, or if the space has been deallocated by a call to free or realloc, the behavior is undefined.

However, there's never a good excuse to be using malloc and free in C++. As described before, C++ has its own alternatives.


Answers to Questions

So to answer your questions directly:

  1. Where is the "memory" that is being allocated?

    The C++ standard doesn't care. It simply says that the program has some memory which is made up of bytes. This memory can be allocated.

  2. What is this "memory"? Space in an array? Or something else?

    As far as the standard is concerned, the memory is just a sequence of bytes. This is purposefully very generic, as the standard only tries to model typical computer systems. You can, for the most part, think of it as a model of the RAM of your computer.

  3. What happens exactly when this "memory" gets allocated?

    Allocating memory makes some region of storage available for use by the program. Objects are initialized in allocated memory. All you need to know is that you can allocate memory. The actual allocation of physical memory to your process tends to be done by the operating system.

  4. What happens exactly when the memory gets deallocated?

    Deallocating some previously allocated memory causes that memory to be unavailable to the program. It becomes deallocated storage.

  5. It would also really help me if someone could answer what malloc does in these C++ lines:

    char* x; 
    x = (char*) malloc (8);
    

    Here, malloc is simply allocating 8 bytes of memory. The pointer it returns is being cast to a char* and stored in x.