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 : Explain Package declaration Package

    Package declaration: It is a declaration employed to name a package. This should be the first item in the source file, preceding any import statements. For example,     pa

  • Q : Define Package Package : The named

    Package: The named grouping of classes and interfaces which gives a package namespace. Classes, interfaces and class members devoid of an explicit public, protected or private access modifier {access!modifier} encompass package visibility. The public

  • Q : Define Statement Statement : The

    Statement: The fundamental building block of Java method. There are numerous different kinds of statement in Java, for example, the assignment statement, if statement, while loop and return statement.

  • Q : What is Member What is Member : Members

    What is Member: Members of a class are methods, fields and nested classes.

  • Q : Define the features of DTD Define the

    Define the features of DTD?

  • Q : Explain the relationship between XHTML

    Explain the relationship between XHTML and the Web?

  • Q : How is an external entity reference

    While constructing an XML DTD, how do you make an external entity reference within an attribute value?

  • Q : Define Property Specifications Property

    Property Specifications: Users can specify assertions using the assert(expr) statements. An assert statement is used to check if the property specified by the expression expr is valid within a state. If expr evaluates to 0, this implies that it is not

  • Q : Write the benefits of DLR Write the

    Write the benefits of DLR?

  • Q : Automaton distributed in the class

    Write a code in a c++/java  for the automaton distributed in the class which accepts keywords(cat,bat,cab). Create an input file with these words may be two or three copies of these words scattered in a paragraph and show that your program does accept these words and gives an output to that

©TutorsGlobe All rights reserved 2022-2023.