Momory management operator and Member Dereferencing operators

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

member dereferencing operator in c plus plus picture

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:

p=new int; s=new float;

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

int *p=new int; float *q=new float;

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

int *p=new int(25);
float *q=new float(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

int *p=new int[10];

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 int[4][5][4]; //legal
array_ptr=new int[n][4][5]; //legal
array_ptr=new int[4][5][]; // illegal
array_ptr=new int[][4][5]; //illegal

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

delete pointer-variable;

The pointer variable is the pointer that points to a data object created with new operator. for Examples

delete p;

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

for example
delete []p;

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 int;
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

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise