New Operator and Delete Operator

New operator: It allows dynamic storage allocation. It throws an exception if memory allocation fails. The general format of new operator is return type, pointer to data type.

The C++ statement

Allocates memory to variable ptrvar dynamically of specified data type and specified size. The operator new allocates a specified amount of memory during run time and returns a pointer to that memory allocation. It compares the size of memory allocated by

Size of (data type) * integer type size;

Where data type can be a standard data type or a user defined data type, integer size can be an integer expression, which specifies the number of element in the array. The new operators returns the NULL, if memory allocation is unsuccessful

Example:   Int * a = new (100);

It creates a memory for an integer and initializes it with 100.

Delete operator: This is used to return the memory allocated by new operator back to the memory pull. Memory thus released will be reused for other part of programme. Although memory allocated by is returned automatically to the system. When the programme terminates it is safer to use this operator explicitly within the pointer. This is absolutely necessary in situations where local variable pointing to the memory get destroyed when the function terminates leaving memory inaccessible to the rest of the delete operator is-

Deal locates the memory allocates to ptrvar. By de allocating the memory, the pointer variable does not get deleted and the address value stored in it does not change. However this address become invalid, as the returned memory will be used up for storing entirely different data. 

   Related Questions in Programming Languages

  • Q : Define Absolute filename Absolute

    Absolute filename: It is a filename whose full path is unambiguously provided starting from the top (that is, root) of a file system tree. For example: c:\Java\bin\javac.exe

  • Q : What is Factory pattern Factory pattern

    Factory pattern: A pattern of class definition which is employed as a generator of instances of other classes. Frequently employed to form platform- or locale-particular implementations of abstract classes or interfaces. This decreases coupling betwee

  • Q : Define Uniform Resource Locator Uniform

    Uniform Resource Locator: It is a Uniform Resource Locator (abbreviated as URL) expands the concept of file access from a wholly local context to one in which the resources are named uniformly, irrespective of where they may be physically situated. A

  • Q : Explain Call-by-value Call-by-value:

    Call-by-value: The semantics of passing an argument to a method in which a copy of actual argument value is taken and positioned in a separate memory location, symbolized by the corresponding formal argument. As an outcome, assignment to the formal ar

  • Q : ArrayLists I. The Assignment The

    I. The Assignment The Bashemin Parking Garage contains a single lane that can hold up to ten cars.  Arriving cars enter the garage at the rear and are parked in the empty space nearest to the front.  Departing cars exit only from the front.  If a cu

  • Q : Define Software engineering Software

    Software engineering: It is the system of applying of an engineering discipline to the implementation, design and maintenance of the software systems.

  • Q : Explain the meaning of semantic

    Explain the meaning of semantic connotations.

  • Q : Explain the common uses of XML Explain

    Explain the common uses of XML.

  • Q : Simple Sorting Program in C Shell

    Assignment: Sorting You will write a simple sorting program. This program should be invoked as follows: shell% ./fastsort -i inputfile -o outputfile

  • Q : What is Cursor Cursor : This is a

    Cursor: This is a visual representation of the existing position of the mouse on the user's virtual desktop. Cursor shapes are frequently set to symbolize the current state of a program – utilizing an hour glass shape to point out that the user

©TutorsGlobe All rights reserved 2022-2023.