Why freed struct in C still has data?

后端 未结 7 1198
梦毁少年i
梦毁少年i 2020-12-02 02:42

When I run this code:

#include 

typedef struct _Food
{
    char          name [128];
} Food;

int
main (int argc, char **argv)
{
    Food  *f         


        
7条回答
  •  天涯浪人
    2020-12-02 03:07

    In some systems freeing memory will unmap it from the address space and you will get a core dump or equivalent if you try to access it after unallocating it.

    In win32 systems (at least up through XP) this is specifically not the case. Microsoft made their memory subsystem on 32 bit Windows purposely linger memory blocks to maintain compatibility with well known MS-DOS applications that used memory after freeing it.

    In the MS-DOS programming model there is no concept of mapping or process space so these types of bugs didn't show up as program failures until they were executed as DOS-mode programs under Windows95.

    That behavior persisted for 32-bit Windows for over a decade. It may change now that legacy compatibility is being withdrawn in systems such as Vista and 7.

提交回复
热议问题