Deleting C++ pointers to an object

前端 未结 9 1186
攒了一身酷
攒了一身酷 2021-01-26 20:06

I thought that the delete command would free up memory I allocated. Can someone explain why it seems I still have the memory in use after delete?

class Test
{
pu         


        
9条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-26 20:35

    As others have said, what you've done is wrong, but you can't expect any particular behavior.

    What's probably happening on most modern systems is the memory allocated for and occupied by e is on a memory page mapped as valid for your program, and the fact that you have deleted e, doesn't not undo that mapping.

    So you are still accessing memory that your application has permission to use from OS. Thus, no segmentation fault. It is possible, however that if enough things happen between delete e and accessing that memory the page will have been remapped. Then you get a seg fault.

提交回复
热议问题