Share via


new 演算子 (CRT)

更新 : 2007 年 11 月

ヒープからメモリ ブロックを割り当てます。

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

パラメータ

  • count
    メモリ割り当てのサイズ。

  • object
    オブジェクトが作成されるメモリ ブロックへのポインタ。

戻り値

新たに割り当てられるストレージの最下位バイト アドレスへのポインタ。

解説

この形式の operator new は、ベクタ型の new (operator new[]) に対して、スカラ型の new と呼ばれます。

この演算子の 1 番目の形式は、仮引数なしの形式と呼ばれます。第 2 の形式は仮引数付き、第 3 の形式は例外をスローしない、仮引数付きと呼ばれます。

この演算子の 1 番目の形式はコンパイラによって定義され、プログラムに new.h をインクルードする必要がありません。

operator deleteoperator new によって割り当てられたメモリを解放します。

割り当てに失敗したときに operator new が null を返すか、例外をスローするかを設定できます。詳細については、「The new and delete Operators」を参照してください。

CRT の operator new の動作は、スローの動作を除き、標準 C++ ライブラリの operator new と同じです。

必要条件

ルーチン

必須ヘッダー

new

<new.h>

互換性の詳細については、「C ランタイム ライブラリ」の「互換性」を参照してください。

ライブラリ

C ランタイム ライブラリのすべてのバージョン。

使用例

スカラ型で仮引数なしの operator new の使用方法を次に示します。

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

スカラ型で仮引数付きの 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
}

スカラ型で例外をスローしない、仮引数付きの 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;
}

.NET Framework の相当するアイテム

適用できません。標準 C 関数を呼び出すには、PInvoke を使用します。詳細については、「プラットフォーム呼び出しの例」を参照してください。

参照

参照

メモリ割り当て