查看释放的内存会导致访问冲突吗?
访问(只读)释放的内存是否会导致访问冲突,如果是,在什么情况下?
Can accessing (for read only) memory freed cause an access violation, and, if so, under what circumstances?
是的,可以.访问冲突"(分段错误"等)是当进程尝试访问(甚至只是为了读取)操作系统已知为空"、释放"或由于某些其他原因无法访问.这里的关键时刻是操作系统/硬件必须知道内存是空闲的.C 标准库的内存管理函数不一定会将释放"的内存返回给操作系统.他们可能(并将)保留它以备将来分配.因此,在某些情况下,访问释放"的内存不会导致访问冲突",因为从操作系统/硬件的角度来看,该内存并未真正被释放.但是,在某些时候,标准库可能会决定将收集的空闲内存返回给操作系统,之后尝试访问该内存通常会导致访问冲突".
Yes, it can. "Access violation" ("segmentation fault", etc) is the response that is normally generated by OS/hardware when the process attempts to access (even just for reading) memory that is known to OS as "empty", "freed" or inaccessible for some other reason. The key moment here is that the OS/hardware must know that the memory is free. Memory management functions of C Standard Library don't necessarily return the 'free'd memory back to OS. They might (and will) keep it for future allocations. So in some cases accessing 'free'd memory will not result in "Access Violation" since from the OS's/hardware's point of view this memory has not been really freed. However, at some point the Standard Library might decide to return the collected free memory back to OS, after which an attempt to access that memory will normally result in "Access Violation".