operator new (CRT)

Allocate 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();

Parameters

  • count
    The size of the allocation.

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

Return Value

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

Remarks

This form of operator new is known as vector new, in contrast to the scalar 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.

Requirements

Routine

Required header

new[]

<new.h>

For additional compatibility information, see Compatibility in the Introduction.

Libraries

All versions of the C run-time libraries.

Example

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

// crt_new4.cpp
#include <stdio.h>
int main() {
   int * k = new int[10];
   k[0] = 21;
   printf("%d\n", k[0]);
   delete [] k;
}

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

// crt_new5.cpp
#include <stdio.h>
#include <new.h>
int main() {
   int * i = new int[10];
   i[0] = 21;
   printf("%d\n", i[0]);
   // initialize existing memory (i) with, in this case, int[[10]
   int * j = new(i) int[10];   // placement vector new
   printf("%d\n", j[0]);
   j[0] = 22;
   printf("%d\n", i[0]);
   delete [] i;   // or, could have deleted [] j 
}

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

// crt_new6.cpp
#include <stdio.h>
#include <new.h>
int main() {
   int * k = new(std::nothrow) int[10];
   k[0] = 21;
   printf("%d\n", k[0]);
   delete [] k;
}

See Also

Reference

Memory Allocation