operator new
Collapse the table of content
Expand the table of content
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

operator new (CRT)

Allocates block of memory from heap

void *__cdecl operator new( 
   size_t count 
void *__cdecl operator new( 
   size_t count,  
   void * object 
) throw(); 
void *__cdecl operator new( 
   size_t count,  
   const std::nothrow_t& 
) throw();


The size of the allocation.


A pointer to a block of memory in which the object will be created.

A pointer to the lowest byte address of the newly allocated storage.

This form of operator new is known as scalar new, in contrast to the vector new form (operator new[]).

The first form of this operator is known as the nonplacement form. The second form of this operator is known as the placement form and the third form of this operator is the nonthrowing, placement form.

The first form of the operator is defined by the compiler and does not require new.h to be included in your program.

operator delete frees memory allocated with operator new.

You can configure whether operator new returns null or throws an exception on failure. See The new and delete Operators for more information.

With the exception of throwing or no-throwing behavior, the CRT operator new behaves like operator new in the Standard C++ Library.


Required header



For additional compatibility information, see Compatibility in the Introduction.


All versions of the C run-time libraries.

The following shows how to use the scalar, nonplacement form of operator new.

// crt_new1.cpp
#include <stdio.h>
int main() {
   int * i = new int(6);
   printf("%d\n", *i);
   delete i;

The following shows how to use the scalar, placement form of operator new.

// crt_new2.cpp
#include <stdio.h>
#include <new.h>
int main() {
   int * i = new int(12);
   printf("*i = %d\n", *i);
   // initialize existing memory (i) with, in this case, int(7)
   int * j = new(i) int(7);   // placement new
   printf("*j = %d\n", *j);
   printf("*i = %d\n", *i);
   delete i;   // or, could have deleted j

The following shows how to use the scalar, placement, no-throw form of operator new.

// crt_new3.cpp
#include <stdio.h>
#include <new.h>
int main() {
   // allocates memory, initialize (8) and if call fails, new returns null
   int * k = new(std::nothrow) int(8);   // placement new
   printf("%d\n", *k);
   delete k;

Not applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples.

© 2016 Microsoft