3

これはdelete[]演算子を使用する正しい方法ですか?

int* a=new int[size];
delete[] a;

はいの場合、誰(コンパイラまたはGCまたは誰でも)が新しく作成されたアレイのサイズを決定しますか?配列サイズはどこに保存されますか?

ありがとう

4

3 に答える 3

2

技術的には、その使用法は完全に有効です。ただし、一般に、そのような新しい配列を使用することはお勧めできません。std::vectorを使用する必要があります。

于 2010-04-28T11:15:35.160 に答える
2

For each chunk of memory allocated, the memory allocator stores the size of the chunk (that's why it is inefficient to allocate many small blocks compared to one big one for example). When delete frees the memory, the allocator knows how large the memory chunk is the pointer points to.

于 2010-04-28T09:42:48.150 に答える
0

ちなみに、書きたくなるたびに、代わりにnew T[size]使うべきでしょう。std::vector<T>動的配列へのローカルポインタを使用すると、例外がスローされた場合に適切なメモリ解放を保証するのは非常に困難です。

于 2010-04-28T11:04:26.207 に答える