0% found this document useful (0 votes)
20 views10 pages

02-05 - Introduction To The Free Store

This document provides an introduction to using dynamic memory allocation in C and C++. It discusses: 1) Using malloc() in C to allocate memory for single values or arrays in a similar way. 2) The new and delete operators in C++ that serve the same purpose as malloc() and free() in C. new allocates memory and returns a pointer, while delete frees it. 3) How new can allocate memory for both atomic types and user-defined types like classes, calling their constructors, and delete calls destructors. Other constructors can also be used.

Uploaded by

韩轶
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views10 pages

02-05 - Introduction To The Free Store

This document provides an introduction to using dynamic memory allocation in C and C++. It discusses: 1) Using malloc() in C to allocate memory for single values or arrays in a similar way. 2) The new and delete operators in C++ that serve the same purpose as malloc() and free() in C. new allocates memory and returns a pointer, while delete frees it. 3) How new can allocate memory for both atomic types and user-defined types like classes, calling their constructors, and delete calls destructors. Other constructors can also be used.

Uploaded by

韩轶
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Introduction to Free Store 1

Introduction to the Free Store

Overview
C and Dynamic Memory
C++ and Creating Single Objects
C++ and Arrays

This material is intended for students of the “C++ Programming for Financial Engineering” certificate
A joint project by the Baruch MFE program, Datasim Education BV and Quant Network LLC
© Datasim Education BV 2011. Unauthorized distribution is prohibited.
2 Introduction to Free Store

C and Malloc
In ANSI C you use a malloc function for dynamic memory
Creation of a single value or array is the same, no syntax differences
void main()
{
int* p;
p = (int*)malloc(1 * sizeof(int));
free(p);

p = (int*)malloc(10 * sizeof(int));
free(p);
}

C AND MALLOC
When you use arrays in C you have two options. A static or dynamic array. A static array is the most
straightforward since the declaration shows that it is an array because of the brackets. When you want a
bit of more flexibility you have to use dynamic arrays. Dynamic arrays are created on the heap or free
store. Both names are used to identify the memory that you can manage outside of the memory your
program code uses. This memory needs to be managed by you. When you ask the system for a piece of
memory it tries to allocate it and if it succeeds it returns a handle to the address of that memory. The
functions provided in C do not know what the memory is used for. You call the functions supplying a
number of bytes to allocate which it does. When there is no memory it returns NULL. This memory
can be used for all kind of purposes one of them being an array. If you look at the example on the sheet
you can see that the first call to malloc allocates a piece of memory the size of one integer value. This
memory is large enough for one value. The second example allocates a piece of memory large enough
to hold 10 integer values. It is an array of 10 integers. In the example you can see that the malloc
function does not know if it is an array or a single value that is going to be allocated. The function only
needs to know how much memory it needs to reserve.
Introduction to Free Store 3

C++ and The Free Store


Allocate memory resources at run-time
Two operators
for allocating new
for deallocating delete

FREE STORE
It is possible to allocate memory during run-time by the use of so called dynamic memory. In C we
allocate (request) memory by using a number of functions such as malloc() and calloc().

These functions attempt to allocate the memory from the free store and return a pointer to the allocated
memory. The C++ equivalent of these functions is the operator new(). This operator is not a function
but an operator which therefore can be overloaded.

The new() operator also tries to request the memory from the heap and returns a pointer to the allocated
memory. This pointer must also be checked to see if the memory is available.

When deallocating memory (freeing) you would use the free() function in C, in C++ it is also an
operator, delete.

When creating a C++ program it is still possible to use the C functions for allocating and deallocating
memory, but what you need to remember is not to mix these types of memory usage. When requesting
memory with malloc() do not free it using delete or vice versa. Unpredictable things can happen.

Using the free store can be divided in


• pointers to objects
• arrays of objects

This material is intended for students of the “C++ Programming for Financial Engineering” certificate
A joint project by the Baruch MFE program, Datasim Education BV and Quant Network LLC
© Datasim Education BV 2011. Unauthorized distribution is prohibited.
4 Introduction to Free Store

Pointers to Objects
(atomic types)
Allocation of single variable
int* pi;
pi = new int;

Deallocation
delete pi;

POINTERS TO OBJECTS
The simplest case possible is to allocate memory to store a value of an atomic type like int, char, float
and double. The new operator returns a pointer to a piece of memory that is large enough to hold a
value of that specific type. The allocated memory needs to be freed using the delete operator.

SYNTAX OF NEW

The syntax of the new operator is quite simple use the keyword new followed by the type for which
you want to reserve a piece of memory.
new <type>; The new Operator

void main() Usage of new


{
int* pi;
float* fp;
pi = new int;
pf = new float;
}

The new operator allocates a piece of memory large enough to hold a value of that specific atomic type.
If no memory is available the operator returns NULL.
Introduction to Free Store 5

SYNTAX OF DELETE
The memory can be deallocated (freed) by using the delete operator.

The syntax for delete when used for pointers to single values;

delete <pointer_to_memory>; The delete Operator

void main() Usage of delete


{
int* pi;
float* fp;
pi = new int;
pf = new float;

delete pi;
delete pf;

This material is intended for students of the “C++ Programming for Financial Engineering” certificate
A joint project by the Baruch MFE program, Datasim Education BV and Quant Network LLC
© Datasim Education BV 2011. Unauthorized distribution is prohibited.
6 Introduction to Free Store

Pointers to Objects
(user defined types)
Create objects dynamically
Point* pp;
pi = new Point; // Default constructor is called

Deallocation
delete pp; // Destructor gets called

ALLOCATING OBJECTS (USER-DEFINED TYPES)


The new operator can be used to allocate memory for holding objects as well. The syntax is the same.

void main() New and Objects


{
Date* pd;
Point* pp;

pd = new Date;
pp = new Point;
}

For the type you can use a class name for example Point or Date. The new operator allocates a piece of
memory to store an object of the specified type and calls for that block of memory, which is in fact an
object, the default constructor.

Using the delete operator for objects is the same as for the atomic types. The destructor is called for the
dynamically created object.

void main() delete and Objects


{
Date* pd;
Point* pp;

pd = new Date;
pp = new Point;

delete pd;
delete pp;

}
Introduction to Free Store 7

Other Constructors
For objects it is possible to call other constructors
Point* pp;

pp = new Point; // Default constructor


delete pp;

pp = new Point(10, 30); // Other constructor


delete pp;

CALLING OTHER CONSTRUCTORS


The second possibility is to call another constructor than the default constructor for the allocated object.

new <type>(constructor_arguments); Syntax

This syntax of the new operator allocates the object on the heap, calls it specified constructor and
returns a pointer to the object.

void main() Usage


{
Date* dp;
Date* dp2;

dp = new Date; // Default constructor called


dp2 = new Date(10,10,1995); // Constructor with day, month and year
...
...
delete dp;
delete dp2;
}

This material is intended for students of the “C++ Programming for Financial Engineering” certificate
A joint project by the Baruch MFE program, Datasim Education BV and Quant Network LLC
© Datasim Education BV 2011. Unauthorized distribution is prohibited.
8 Introduction to Free Store

Using Pointers to Objects


Call member functions using the ‘->‘
Point* pp;
pp = new Point;
pp->SetX(10.0);

Dereference
(*pp).SetX(10.0);

8
Introduction to Free Store 9

Arrays and the Free Store


Creating arrays
Point* pp;
pp = new Point[10];

Deleting arrays
delete[] pp;

Cannot call other than default constructor

ARRAYS AND THE FREE STORE


An other way of using the new operator is by allocating arrays of object instead of single objects.

This type of allocation is slightly different from the previous one.


new <type>[<size>]; Syntax
delete [] <pointer_to_memory>;

The new operator allocates a contiguous block of memory large enough to hold <size> number of
objects of type <type>. For each object on the array, the default constructor gets called. It is NOT
possible to specify a different constructor to be called on creation. The default constructor is called
always.

Freeing memory is executed by using the delete operator. You have to specify that the pointer is a
pointer to a block of memory which was allocated as an array.

The destructor is called for each object in the array. Do not forget the []. What happens if you forget
these brackets is unpredictable.

A few rules when using the free store:


• Do not mix the C and C++ free store.
• When allocating a single object use the delete without brackets.
• When allocating an array use the delete with brackets.
• Check what new returns to see if its NULL.

This material is intended for students of the “C++ Programming for Financial Engineering” certificate
A joint project by the Baruch MFE program, Datasim Education BV and Quant Network LLC
© Datasim Education BV 2011. Unauthorized distribution is prohibited.
10 Introduction to Free Store

Tips and Pitfalls for Free Store


Watch out for the [] with delete
Do not mix C allocation with C++ allocation and vice versa
Do not forget to delete allocated memory

10

You might also like