new
and delete
.new
. new
is followed by a data type specifier and, if a sequence of more than one element is required, the number of these within brackets []
. It returns a pointer to the beginning of the new block of memory allocated. Its syntax is:
pointer = new type
pointer = new type [number_of_elements]
type
. The second one is used to allocate a block (an array) of elements of type type
, where number_of_elements
is an integer value representing the amount of these. For example:
|
|
int
and returns a pointer to the first element of the sequence, which is assigned to foo
(a pointer). Therefore, foo
now points to a valid block of memory with space for five elements of type int
.foo
is a pointer, and thus, the first element pointed to by foo
can be accessed either with the expression foo[0]
or the expression *foo
(both are equivalent). The second element can be accessed either with foo[1]
or *(foo+1)
, and so on...new
. The most important difference is that the size of a regular array needs to be a constant expression, and thus its size has to be determined at the moment of designing the program, before it is run, whereas the dynamic memory allocation performed by new
allows to assign memory during runtime using any variable value as size.new
are going to be granted by the system. bad_alloc
is thrown when the allocation fails. Exceptions are a powerful C++ feature explained later in these tutorials. But for now, you should know that if this exception is thrown and it is not handled by a specific handler, the program execution is terminated.new
, and is the one used in a declaration like:
|
|
nothrow
, and what happens when it is used is that when a memory allocation fails, instead of throwing a bad_alloc
exception or terminating the program, the pointer returned by new
is a null pointer, and the program continues its execution normally.nothrow
, declared in header <new>
, as argument for new
:
|
|
foo
is a null pointer:
|
|
nothrow
method is likely to produce less efficient code than exceptions, since it implies explicitly checking the pointer value returned after each and every allocation. Therefore, the exception mechanism is generally preferred, at least for critical allocations. Still, most of the coming examples will use the nothrow
mechanism due to its simplicity.delete
, whose syntax is:
|
|
new
, and the second one releases the memory allocated for arrays of elements using new and a size in brackets ([]
).delete
shall be either a pointer to a memory block previously allocated with new
, or a null pointer (in the case of a null pointer, delete
produces no effect).
|
|
How many numbers would you like to type? 5 Enter number : 75 Enter number : 436 Enter number : 1067 Enter number : 8 Enter number : 32 You have entered: 75, 436, 1067, 8, 32, |
i
), not a constant expression:
|
|
i
so big that the system cannot allocate enough memory for it. For example, when I tried to give a value of 1 billion to the "How many numbers" question, my system could not allocate that much memory for the program, and I got the text message we prepared for this case (Error: memory could not be allocated
).nothrow
) or by catching the proper exception.new
and delete
for allocating dynamic memory. But these were not available in the C language; instead, it used a library solution, with the functions malloc
, calloc
, realloc
and free
, defined in the header <cstdlib>
(known as <stdlib.h>
in C). The functions are also available in C++ and can also be used to allocate and deallocate dynamic memory.new
, so they should not be mixed; each one should be handled with its own set of functions or operators.Index |