C/C++ : Deallocating or deleting a block of dynamically created memory [duplicate]

主宰稳场 提交于 2019-12-11 03:48:42

问题


Possible Duplicate:
C++ delete - It deletes my objects but I can still access the data?

I am trying to deallocate a block of dynamically created memory in C/C++.
But both the standard methods I used (malloc/free and new/delete) seems to be dysfunctional.
The o/p of both the codes given below is similar.
Here is code using malloc/free :

    #include<stdio.h>
    #include<stdlib.h>

    int main(){
        int * arr;
        arr = (int *)malloc(10*sizeof(int)); // allocating memory
        int i;
        for(i=0;i<10;i++)
            arr[i] = i;
        for(i=0;i<10;i++)
            printf("%d ",arr[i]);
        printf("\n");
        free(arr); // deallocating
        for(i=0;i<10;i++)
            printf("%d ",arr[i]);
        printf("\n");
    }


Here is code using new/delete[] :

    #include<stdio.h>
    #include<stdlib.h>

    int main(){
        int * arr;
        arr = new int[10]; // allocating memory
        for(int i=0;i<10;i++)
            arr[i] = i;
        for(int i=0;i<10;i++)
            printf("%d ",arr[i]);
        printf("\n");
        delete[] arr; // deallocating
        for(int i=0;i<10;i++)
            printf("%d ",arr[i]);
        printf("\n");
    }

But, even after deallocating the memory, none of them are giving any errors.
The o/p in both the cases is same :

0 1 2 3 4 5 6 7 8 9
0 0 2 3 4 5 6 7 8 9


So, what is the correct method of deallocating memory in C/C++ ? Also, why is the array is getting printed even after deallocating the memory ?


回答1:


Because deallocating/freeing memory does not mean annulling or something like this.

Freeing memory (most likely) will just mark that piece of memory as free and will not do anything else to it, until something else use it again.

This makes the freeing memory faster.

What you have in your code is undefined behaviour, because you're reading (using) memory, that you're not supposed to touch at all. It's "freed" and using that memory could cause anything. A crash in the best case.




回答2:


Undefined behavior means anything can happen. Including appearing to work.

And make no mistake about it, you are running into undefined behavior when you access memory that was released. Simple as that.




回答3:


The system is free to do anything that it wants with the freed memory, including nothing at all. Doing a free or a delete doesn't gaurantee that the freed memory will e.g. be set to zeros, or any other value. The values in the array may happen to be unchanged. This is probably the typical behavior.

But regardless, using or accessing this memory after freeing it is incorrect code, and invokes undefined behavior.




回答4:


the memory has been freed and has been given to the OS for other tasks. But in between if the OS does not use this memory address, the previous written data on that address on still there. Your program output can any time give different output aka UNDEFINED behaviour.



来源:https://stackoverflow.com/questions/12033842/c-c-deallocating-or-deleting-a-block-of-dynamically-created-memory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!