
- •Pointers
- •Procedural programming
- •Object oriented programming
- •3) The basic paradigms of object-oriented programming: encapsulation, polymorphism and inheritance.
- •Identifier or function name
- •7) String functions in the programming language Java.
- •8.Loops and branching statements in the programming language Java
- •The break Statement
- •The return Statement
- •11) Creation and using one-dimensional and multi-dimensional arrays in the programming language Java.
- •13) Working with I / o system in the programming language Java
- •17) Classification of data types in the programming language Java.
- •18) Java Object Model.
- •1. Java Distributed Object Model
- •1.1 Basic Concepts of the Java Object Model
- •1.2 Basic Concepts of the Java Distributed Object Model
- •1.3 Objects
- •19) Generics in the programming language Java.
- •Example
- •Rules in function overloading
- •Pointer Variables (or Pointers)
- •Declaring Pointers
- •30) Dynamic memory allocation operations: new and delete.
- •Operators new and new[]
- •Operators delete and delete[]
30) Dynamic memory allocation operations: new and delete.
C++ supports dynamic allocation and deallocation of objects using the new and delete operators. These operators allocate memory for objects from a pool called the free store. The new operator calls the special function operator new, and the delete operator calls the special function operator delete.
Beginning in Visual C++ .NET 2002, the CRT's new function (in libc.lib, libcd.lib, libcmt.lib, libcmtd.lib, msvcrt.lib, and msvcrtd.lib) will continue to return NULL if memory allocation fails. However, the new function in the Standard C++ Library (in libcp.lib, libcpd.lib, libcpmt.lib, libcpmtd.lib, msvcprt.lib, and msvcprtd.lib) will support the behavior specified in the C++ standard, which is to throw a std::bad_alloc exception if the memory allocation fails.
Normally, if you #include one of the C++ standard headers, like <new>, you'll get a /defaultlib directive in your object that will reference the appropriate C++ Standard Library according to the CRT model you used (the /M* compiler options). Generally, that will cause the linker to use the throwing operator new from the C++ Standard Library instead of the nonthrowing one from the main CRT, because the order of defaultlib directives will cause libcp.lib to be searched before libc.lib (under /ML).
Even if you use one of the C++ standard library headers, it is possible to get non-throwing new. For example, compile the program below with the following command line and you will not get throwing new behavior, even though standard C++ header files are included:
// new_and_delete.cpp
// compile with: /EHsc
#include <stdio.h>
#include <new>
#include <limits.h>
int main()
{
int * i_arr;
try {
i_arr = new int[0x3fffffff];
}
catch(...) {
printf("caught exception\n");
}
int * k_arr;
k_arr = new (std::nothrow) int[0x3fffffff];
delete[] i_arr; // vector delete
delete[] k_arr;
}
Operators new and new[]
In order to request dynamic memory we use the operator 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 form is: pointer = new type pointer = new type [number_of_elements] The first expression is used to allocate memory to contain one single element of type type. The second one is used to assign 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 * bobby;
bobby = new int [5];
Operators delete and delete[]
Since the necessity of dynamic memory is usually limited to specific moments within a program, once it is no longer needed it should be freed so that the memory becomes available again for other requests of dynamic memory. This is the purpose of the operator delete, whose format is:
1 2 |
delete pointer; delete [] pointer |
The first expression should be used to delete memory allocated for a single element, and the second one for memory allocated for arrays of elements. The value passed as argument to delete must 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).