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

后端 未结 24 3118
暗喜
暗喜 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条回答
  •  旧时难觅i
    2021-02-14 12:45

    It's always best to keep acquisition/release of any resource as balanced as possible. Although leaking or not is hard to say in this case. It depends on the compiler's implementation of the vector (de)allocation.

    BYTE * pBytes = new BYTE [sizeof(STRUCT) + nPaddingSize];
    
    STRUCT* pStruct = reinterpret_cast< STRUCT* > ( pBytes ) ;
    
     // do stuff with pStruct
    
    delete [] pBytes ;
    

提交回复
热议问题