DEV Community

Lalit Kumar
Lalit Kumar

Posted on

ERROR Double Free or Corruption (out).

In this article, we are going to discuss another error named “Double Free or Corruption Error”. This is an error related to memory operations. All descriptions and solutions to this error are given ahead of time.


title: "originally posted here 👇"

canonical_url: https://kodlogs.com/blog/332/error-double-free-or-corruption-out

Double Free or Corruption Error

The error Double Free or Corruption Error in the program means that our program has a C runtime function free(). The C runtime function, free(), can be called by mistake or somehow with an invalid pointer. All of this fuss can happen due to the use of dynamic memory allocation or by invoking free() directly.

Cause

This error is commonly caused if your code has overwritten memory due to an out of bounds array index. For further explanation, you can observe this example.

!! Generates a "double free or corruption" error.
program free
  integer, dimension(:), allocatable :: data
  integer :: i

  allocate(data(5))
  do i = 1, 5
     data(i-1) = i
  end do
  deallocate(data)
end program free
Enter fullscreen mode Exit fullscreen mode

When we compile this piece of code we will get this error.

./free: double free or corruption (out): 0x0000000000607590

Solution

To resolve this problem we have to track the issue as to where it should be. The way to achieve this is: to check invalid array indexing, compile the program with -fbounds-check. Then when we run it again and if there is any array indexing that is out of bounds then we will get an error message like this.

% gfortran -fbounds-check -o free free.f90
% ./free
At line 8 of file free.f90
Fortran runtime error: Array reference out of bounds for array 'data', lower bound of dimension 1 exceeded (0 < 1)

This redirects us to a line of code. Here we can rewrite it and can change it to solve our issues.

Top comments (0)