new long [0] の解放


momoでん  2005-08-19 08:04:13  No: 58697

new long [0]で取得したポインタに対して、
delete [] を実行させた場合、問題は発生しませんか?


通りすがりのxxx  2005-08-19 19:40:46  No: 58698

配列要素 0 で new する意味がよくわかりませんが、
実際にコードをコンパイルしてみるのが早いと思います。

long *lp = new long[0]; ←この地点でコンパイラがエラーを吐かないのかな?

コンパイラが new long[0] を喰っても lp にメモリが割り当てられて
いないのならば delete[] に対して問題あるような気もする。

でも delete は NULL に対しては何も行わないんだっけか?


YuO  2005-08-19 19:59:23  No: 58699

> new long [0]で取得したポインタに対して、
> delete [] を実行させた場合、問題は発生しませんか?

operator new[]とoperator delete[]に問題がなければ,
問題は発生しません。

> long *lp = new long[0]; ←この地点でコンパイラがエラーを吐かないのかな?

INCITS/ISO/IEC 14882:2003 5.3.4 New / Paragraph. 7
>When the value of the expression in a direct-new-declarator is zero,
>the allocation function is called to allocate an array with no elements.

というわけで,コンパイラはエラーを吐きません。
また,operator new[]は記憶域を割り付けます。
ちなみにmallocと違い,要求サイズ0の時にoperator new/operator new[]がヌルポインタ定数を返すことはありません。
# 同 3.7.3.1 Allocation functions / Paragraph. 2

> でも delete は NULL に対しては何も行わないんだっけか?

行いません。
同 5.3.5 Delete / Paragraph. 2
>... In either alternative,
>if the value of the operand of delete is the null pointer the operation has no effect. ...


※返信する前に利用規約をご確認ください。

※Google reCAPTCHA認証からCloudflare Turnstile認証へ変更しました。






  このエントリーをはてなブックマークに追加