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

后端 未结 24 3116
暗喜
暗喜 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 13:02

    Technically I believe it could cause a problem with mismatched allocators, though in practice I don't know of any compiler that would not do the right thing with this example.

    More importantly if STRUCT where to have (or ever be given) a destructor then it would invoke the destructor without having invoked the corresponding constructor.

    Of course, if you know where pStruct came from why not just cast it on delete to match the allocation:

    delete [] (BYTE*) pStruct;
    

提交回复
热议问题