the new and delete operatorsthe c language has


The new and delete operators

The C language has explained library functions- malloc() and free() for dynamic allocation and de-allocation of memory. C++ gives yet another approach to allocate blocks of memory - the new operator. This operator allocates memory for a given size and returns a pointer to its starting point. C++ also gives delete, to release the memory allocated by new. The pointer returned by the new operator require not be typecasted.

e.g.

                char arr[100];     // Compile_time allocation of an array

                char *arr;                             // Character pointer

arr = new char[size];                       //Run time allocation of an

                                                                //array. Size can be a

                                                                //constant or a variable.

In the above example, new returns a pointer to a block of size bytes. It is synonymous with declaring a character array. Though, declaring an array is an example of static binding - the array is built at the compile-time. This array remains in existence right from the beginning of the program to its end, even if not in use. While, the array declared by new operator can be allocated memory only when required and can be released when over with, using the delete operator. This is an example of dynamic binding.

 

Request for Solution File

Ask an Expert for Answer!!
C/C++ Programming: the new and delete operatorsthe c language has
Reference No:- TGS0309343

Expected delivery within 24 Hours