Will this C++ code cause a memory leak (casting array new)

后端 未结 24 3056
暗喜
暗喜 2021-02-14 12:26

I have been working on some legacy C++ code that uses variable length structures (TAPI), where the structure size will depend on variable length strings. The structures are allo

24条回答
  •  野的像风
    2021-02-14 12:47

    The behaviour of the code is undefined. You may be lucky (or not) and it may work with your compiler, but really that's not correct code. There's two problems with it:

    1. The delete should be an array delete [].
    2. The delete should be called on a pointer to the same type as the type allocated.

    So to be entirely correct, you want to be doing something like this:

    delete [] (BYTE*)(pStruct);
    

提交回复
热议问题