rnorris
rnorris

Reputation: 2082

Segfault when deleting pointer

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.

Upvotes: 2

Views: 20189

Answers (5)

bisarch
bisarch

Reputation: 1398

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.

Upvotes: 3

Ramy Al Zuhouri
Ramy Al Zuhouri

Reputation: 21966

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.

Upvotes: 0

Mark Ransom
Mark Ransom

Reputation: 308206

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.

Upvotes: 0

Carl Norum
Carl Norum

Reputation: 224944

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.

Upvotes: 4

Chad
Chad

Reputation: 19032

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.

Upvotes: 20

Related Questions