Results 1 to 2 of 2

Thread: Using new and delete Operators in C++

  1. #1
    Join Date
    Aug 2008
    Posts
    157

    Using new and delete Operators in C++

    C++ offers programmers a means to create variables dynamically using new operator. The new operator determines the type of receiving pointer and converts its return value appropriately. Memory is allocated. To release the memory allocated,C++ uses delete operator.

    The new operator, when used with the pointer to a data type, allocates memory for the item and assigns the address of that memory to the pointer. The delete operator does the reverse, it de-allocates the memory allocated to the variable. The C++ new operator is used to allocate memory from the heap. The advantage of the new operator is that it offers safety in terms of type and size. Whereas malloc only takes one argument, the desired size in bytes. The argument to the new operator identifies the type of the variable to create, which ensures that the user is getting memory of the intended size and type. When no longer needed, memory that is allocated with new should be deallocated with delete.

    Code:
    #include <iostream>
    int main()
    {
           char *name;                               //Declare a pointer
           name = new char[31];                  //Allocates 31 bytes memory for the object 
           cout<< "Enter name : " << endl;   //Will be displayed on the screen
           cin >> name;
           cout << name <<;
           delete [] name;                           //Destroys the object by de-allocating the memory
           return 0;
    }

  2. #2
    Join Date
    May 2008
    Posts
    2,012

    Re: Using new and delete Operators in C++

    C++ supports dynamic allocation and deallocation of objects using the new and delete operators. These operators allocate memory for objects from a pool called the free store. The new operator calls the special function operator new, and the delete operator calls the special function operator delete.

Similar Threads

  1. How to use an Operators in PHP?
    By Mr.ken in forum Software Development
    Replies: 5
    Last Post: 05-03-2010, 06:23 AM
  2. php operators and keywords
    By Fragman in forum Software Development
    Replies: 5
    Last Post: 26-02-2010, 06:15 PM
  3. What are the Operators and Expressions in C# ?
    By kyosang in forum Software Development
    Replies: 4
    Last Post: 09-02-2010, 06:32 AM
  4. overloading of operators in C++
    By shilong in forum Software Development
    Replies: 3
    Last Post: 28-01-2009, 10:30 PM
  5. Want info on C# Operators
    By ashwinK in forum Software Development
    Replies: 2
    Last Post: 03-11-2008, 02:31 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,721,045,484.34991 seconds with 17 queries