Segfault when deleting pointer

好久不见. 提交于 2019-12-19 04:19:30

问题


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 produces the same error:

int main()
{
  int* pointer=0;
  int number = 3;

  pointer = &number;
  delete pointer;//This line causes a segmentation fault
  pointer=0;

  return 0;
}

A slight modification produces code that will work as expected:

int main()
{
  int* pointer=new int(3);

  delete pointer;//This line now works
  pointer=0;

  return 0;
}

Can someone explain why the first causes a segfault and the second does not? I know the pointer isn't invalid, since it's been assigned to the address of the number variable.


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/9235770/segfault-when-deleting-pointer

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