Results 1 to 5 of 5

Thread: Distinguished between the new and new [ ] : C++

  1. #1
    Join Date
    Dec 2009
    Posts
    33

    Distinguished between the new and new [ ] : C++

    Hello, I am the student of the Msc-IT second year. I have the knowledge of the C++ language but I do not know all of the operators. I have to use the memory slot for execution. Thus, I want to Distinguished between the new operator and new [] operator. I also want to know how can I use the new and new [] operator in C++ program. So, If you can know the new and new [] operator then give me reply.

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: Distinguished between the new and new [ ] : C++

    Hi, I think new operator can be used to allocate the block of memory on the heap dynamically. The new operator can throws an exception that can be std::bad_alloc, if new operator can not be able to allocate memory on the heap. The syntax of the new operator can be as follows :
    Code:
    pvr = new name;

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    new [ ] : C++

    The new [] operator can be used to create place in the memory block for an array. The following can be the syntax of the new [] operator :
    Code:
    pr = new type [sz];
    In the above syntax, you noticed that sz can specifies the size of the one-dimensional array to create. It can returns the address of the first element and the value can be stored in the variable pr.

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    new operator : Program

    The example of the new operator can be as follows :
    Code:
    #include <iostream>
    #include <new>
    using namespace std;
    struct mcs 
    {
    mcs() 
    {
    cout <<"mcs constructed\n";
    }
    };
    int main ()
     {
       int * p3 = new int;
       int * p4 = new (nothrow) int;
       mcs * p5 = (mcs*) operator new (sizeof(mcs));
       new (p5) mcs; 
       return 0;
    }
    The Output of above :
    Code:
    mcs constructed

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    new [ ] : Program

    The example of new [] :
    Code:
    #include <iostream>
    #include <memory>
    #include <new>
    using namespace std;
    struct myc
    {
    myc() 
    {
    cout <<"myc constructed\n";
    }
    };
    int main () 
    {
      int * p3 = new int[5];
      int * p4 = new (nothrow) int[4];
      pair <myc*,ptrdiff_t> p5 = get_temporary_buffer<myc>(3);
      new (p5.first) myc[3];   
      return_temporary_buffer(p5.first);
      return 0;
    }
    Output:
    Code:
    myc constructed
    myc constructed
    myc constructed

Similar Threads

  1. Distinguished between the delete and delete [ ] : C++
    By Gaauge in forum Software Development
    Replies: 4
    Last Post: 05-02-2010, 07:50 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,710,815,231.78902 seconds with 16 queries