Memory allocation of pointers

 Memory allocation of pointers:

Memory allocation is done in two ways

1.    Static memory allocation

2.    Dynamic memory allocation

Static memory allocation:

·       The amount of memory to be allocated is pre known.

·       The memory is allocated during compilation time.

·       Ex: int a; // allocates 2 bytes.

·       Variable remains permanently allocated.

Dynamic memory allocation:

·       Memory is allocated during the execution of the program.

·       C++ supports dynamic allocation and de allocation of objects using the new and delete operations.

new operator:

·       We can allocate storage for a variable while program is running by using new operator.

·       Ex:

          int *variable;

          variable=new int; // allocates memory for variable

·       Ex:     int *array=new int[20]; // allocates memory for array

delete operator:

·       We can de allocate storage for a variable using delete operator.

·       Syntax:   delete variable;

·       Syntax for de allocating memory for array  delete [ ] arrayname;

Comments