Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



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

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 05-02-2010
Member
 
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.

Reply With Quote
  #2  
Old 05-02-2010
Member
 
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;
Reply With Quote
  #3  
Old 05-02-2010
Member
 
Join Date: Oct 2005
Posts: 2,387
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.
Reply With Quote
  #4  
Old 05-02-2010
Member
 
Join Date: May 2008
Posts: 2,377
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
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #5  
Old 05-02-2010
Member
 
Join Date: Feb 2008
Posts: 1,843
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
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Distinguished between the new and new [ ] : C++"
Thread Thread Starter Forum Replies Last Post
Distinguished between the delete and delete [ ] : C++ Gaauge Software Development 4 05-02-2010 06:50 PM


All times are GMT +5.5. The time now is 06:59 PM.