これはdelete[]演算子を使用する正しい方法ですか?
int* a=new int[size];
delete[] a;
はいの場合、誰(コンパイラまたはGCまたは誰でも)が新しく作成されたアレイのサイズを決定しますか?配列サイズはどこに保存されますか?
ありがとう
これはdelete[]演算子を使用する正しい方法ですか?
int* a=new int[size];
delete[] a;
はいの場合、誰(コンパイラまたはGCまたは誰でも)が新しく作成されたアレイのサイズを決定しますか?配列サイズはどこに保存されますか?
ありがとう
技術的には、その使用法は完全に有効です。ただし、一般に、そのような新しい配列を使用することはお勧めできません。std::vectorを使用する必要があります。
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.
ちなみに、書きたくなるたびに、代わりにnew T[size]
使うべきでしょう。std::vector<T>
動的配列へのローカルポインタを使用すると、例外がスローされた場合に適切なメモリ解放を保証するのは非常に困難です。