Segfault when deleting pointer

后端 未结 5 1484
南笙
南笙 2021-01-03 12:32

I\'ve been experiencing segfaults when running some C++ code. I\'ve isolated the problem to a line in the program that deletes a pointer. Here\'s a simple example that pro

相关标签:
5条回答
  • 2021-01-03 13:05

    You should only ever delete memory that has been allocated with new. Automatic variables declared on the stack do not need to be deleted. As a rule, always match your memory allocation and deallocation types:

    • Memory allocated with new should be deallocated with delete.
    • Memory allocated with new [] should be deallocated with delete [].
    • Memory allocated with malloc() should be deallocated with free().

    The segfault is because the delete operator will attempt to put that memory back into the heap, and that relies on certain properties of the memory that don't hold true for automatic memory on the stack that didn't originate from the heap.

    0 讨论(0)
  • 2021-01-03 13:09

    Calling delete on a pointer, deallocates the dynamically allocated memory that the pointer points to.

    In the first program, pointer points to a statically allocated memory location.The variable number is an 'automatic' variable, which means that its memory is automatically managed.

    On the other hand in the second program, pointer is pointing to a memory location allocated in the heap segment, which needs to be manually deallocated by calling delete.

    You might find this link useful.

    0 讨论(0)
  • 2021-01-03 13:11

    You can't use delete on anything you didn't get with new. Trying to do so will cause undefined behaviour. Your program crashed, but anything could have happened.

    0 讨论(0)
  • 2021-01-03 13:23

    When you allocate a variabile with new:

    int *a=new int(4);
    

    This variable is put on the heap, which contains all the memory dnamicly allocated. If instead you declare a variable:

    int a=4;
    

    a is allocated in the stack, where there is static memory. Dynamic memory can be deallocated with delete from the user, but static memory can not. Static memory is automaticlly deallocated when yuo exit froma function:

    void function()
    {
        int a;
    }
    

    When the function ends a is automatically deallocated (except for variables declared with the keyword "static"). Also variables in main function are automatically deallocated. So you can not say to the program to deallocate a variable in the stack. In your example number is on the stack, pointer points to number which is in the stack, if you delete it you are trying to delete a variable in the stack, which is not allowed,because it's not dynamic memory.

    0 讨论(0)
  • 2021-01-03 13:25

    When you delete a pointer that wasn't allocated with new, you're creating a conflict between the memory management system and the stack. Each will operate as if it still has sole ownership of the memory, and a crash can result when they overwrite each other's values.

    0 讨论(0)
提交回复
热议问题