• 4222阅读
  • 0回复

delete a dynamic array with delete instead of delete[] [复制链接]

上一主题 下一主题
离线steinlee
 

只看楼主 倒序阅读 楼主  发表于: 2008-07-31
— 本帖被 XChinux 执行加亮操作(2008-07-31) —
http://www.ddj.com/cpp/184403285


What happens if you mix them up? That is, what happens if you attempt to deallocate an array using non-array delete, or vice versa? The draft C++ standard says the behavior is undefined. That is, something bad may happen, maybe now or maybe later. More precisely, a program that uses the wrong form of delete-expression has committed an error which neither the compiler nor the runtime system is obligated to diagnose. The program might terminate abruptly, or limp along producing erroneous results. It might even get away seemingly unscathed.

if you do:
    int * a = new int[10];
    ....................
    delete[] a; a = NULL;
    //delete a; a = NULL; <====  so wrong
always use delete[] for a dynamic array. Even experienced programmers often make this type errors. However, you can find this problem if you use valgrind to check your code.
[ 此贴被steinlee在2008-07-31 00:35重新编辑 ]
Looking for remote C/C++ and Qt 兼职
快速回复
限100 字节
 
上一个 下一个