Momory management operator and Member Dereferencing operators
as we know c++ permits us to define a class containing various types of data and functions as members, c++ also permits us to access the class members through pointers. In order to achieve this c++ provides a set of three pointer-to-member operators , in the table show those operators and their functions further details on these operators will be meaningful only after we discuss classes, and therefore we defer the use of member dereferencing operators until then
MEMORY MANAGEMENT OPERATOR
c uses malloc() and calloc() , free() functions to allocate memory and deallocate memory dynamically at run time, similarly, it uses the function free() to free or delete dynamically allocated memory.
in programming we use dynamic allocation techniques when it is not know in advance how much memory space is needed.
but C++ supports these functions, it also defines two unary operators new and delete that perform the task of allocation and freeing the memory in a better and easier way.
since these operators manipulate(change) the memory on the free store, they are also know as free store operators.
An object can be created by using new and destroy by using delete operators, as and when the required. A data object created inside a block with new will remain in existence until it is explicitly destroyed by using delete operator thus the lifetime of an object is directly under our controls and is unrelated to the block structure of the program.
the new operator can be used to create objects of nay type. it take the following general form
here pointer variable is a variable of type pointer. the new operator allocates the sufficient space to hold a data object of type data-type and returns the address of the object. The data type may be any valid data type in c++. The pointer variable holds or store the address of the memory space allocated. for example:
where p is a pointer of type int and s is a pointer of type float. here p and s must have already been declare as pointers of appropriate type. Alternatively we can combine the both declaration of pointers variables and their assignments as follows
subsequently, the statements
*p=25;
*q=8.9;
assign 25 to the new created int type object and 7.5 to the float type object we can also initialize the memory using the new keyword(operator). this is done as follows.
pointer-variable=new data-type(value);
|
here value specifies the initial value,Examples
*p=new (25);
*q=new (7.5);
|
as mentioned earlier, new can be used to create a memory space for any data type including user-defined types such as arrays, classes, structures.The general form for a one dimensional
array is…
pointer-variable=new data-type[size];
|
Here size specifies the number of elements in the array for example the statement
create a memory space for an array of 10 integers. p[0] will refer to the first element ,p[1] to the second element and so on
when we creating multi dimensional array with new operator, all the array sizes must be supplied
array_ptr=new [4][5][4];
array_ptr=new [n][4][5];
array_ptr=new [4][5][];
array_ptr=new [][4][5];
|
the first dimensional may be variable whose value is supplied at runtime. All others must be constants
DELETE:
when a data object is no longer needed, it is destroyed or delete to release the memory space for reuse.
The general syntax of its use is
The pointer variable is the pointer that points to a data object created with new operator. for Examples
if we want to free or(delete) a dynamically allocated array, we must use the following form of delete.
delete[size] pointer-variable;
|
The size specifies the number of elements in the array to be feed, the problem with this form is that the programmer should remember the size of the array. recent version of c++. do not require the size to be specified
will delete the entire array pointed to by p
what happens if sufficient memory is not available for allocation? in such cases, like malloc(), new returns a null pointer. therefore this may be a good idea to check for the pointer produced by new before using it.
take example it is done as follows
……..
……..
p=new;
if(!p)
{
cout<<“allocation failed \n”;
}
………..
………..
|
the new operator offers the following advantages over the function malloc()
a.it automatically computes the size of the data object. we need not use the operator sizeof.
b. it automatically returns the correct pointer type, so that there is no need to use a type cast
c. it is possible to initialize the object while creating the memory space
d. like any other operator, new and delete can be overloaded