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

后端 未结 24 2953
暗喜
暗喜 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:49

    @ericmayo - cripes. Well, experimenting with VS2005, I can't get an honest leak out of scalar delete on memory that was made by vector new. I guess the compiler behavior is "undefined" here, is about the best defense I can muster.

    You've got to admit though, it's a really lousy practice to do what the original poster said.

    If that were the case then C++ would not be portable as is today and a crashing application would never get cleaned up by the OS.

    This logic doesn't really hold, though. My assertion is that a compiler's runtime can manage the memory within the memory blocks that the OS returns to it. This is how most virtual machines work, so your argument against portability in this case don't make much sense.

提交回复
热议问题