Menu
C++ Tutorials

C++

Memory Allocation



Tutorials > C++ > Memory Allocation

View Full Source

Introduction

Whenever you declare a variable, space is allocated on the stack. The problem with the stack is that it has limited capacity. This can be overcome by allocating memory on the heap. The heap is also known as the free store. This represents the free memory available to you on your computer. This tutorial will show you how to allocate and deallocate memory on the heap using C and C++.

Contents of main.cpp :


#include <iostream>
#include <stdlib.h>
#include <stdio.h>

using namespace std;

int main()
{

Firstly, we create a pointer to an integer and assign it the value of NULL. It is always a good idea to assign pointers a value of NULL as this indicates that no memory is being pointed to.

	// C Memory Allocation
	//---------------------
	int *c = NULL;

The function to allocate memory in C is the malloc function. The function takes one parameter which indicates how many bytes to allocate. It returns a void pointer so you therefore have to cast it to whatever type of pointer you are working with.

The code below allocates memory for one integer and assigns it to our integer pointer.

	c = (int *)malloc(sizeof(int));

We can now use the dereference operator to access the data being pointed to.

	*c = 56;

	printf("c : %d\n", *c);

What is extremely important is that you must always deallocate (free up) any memory that you have previously allocated. If this is not done, you will create a memory leak. This will result in you having less memory for your other programs until you reboot your computer.

The function to deallocate memory in C is unsurprisingly the free function. The only parameter that this function has must take the pointer that points to the allocated memory.

	free(c);

Once we free up the memory, we assign a value of NULL to the pointer to indicate that it is not pointing to any memory anymore.

	c = NULL;

C++ memory allocation is different. Instead of using functions, keywords are now used.

	// C++ Memory Allocation
	//-----------------------
	int *cpp = NULL;

When allocating memory, you must use the new keyword. After the new keyword, the data type that you are allocating for must be given. The size to be allocated will be automatically calculated.

Below we allocate space for one integer by having "new int". This returns a pointer to the memory of the specified type.

	cpp = new int;

If the value returned is NULL, this means that not enough memory could be allocated. This is also true for if the result of malloc is NULL.

	if (!cpp)
	{
		cout << "Insufficient memory" << endl;
		return 1;
	}

We can use the dereference operator as per normal.

	*cpp = 23;
	cout << "cpp : " << *cpp << endl;

The keyword to deallocate the memory previously allocated is the delete keyword. The pointer to the allocated memory is given after the delete keyword.

	delete cpp;

Once again, it is always a good idea to assign a value of NULL to pointers not pointing to any memory.

	cpp = NULL;

	system("pause");

	return 0;
}

Well done. You should now be able to allocate and deallocate memory as needed. This is extremely useful as now you are not limited to the inefficient stack space.

Please let me know of any comments you may have : Contact Me

Source Files : Visual Studio Dev-C++ Unix / Linux

< Tutorial 39 - String Streams Tutorial 41 - Dynamic Arrays >

Back to Top


All Rights Reserved, © Zeus Communication, Multimedia & Development 2004-2005

Read the Disclaimer

Links