Hello. I’m trying to switch over to using GDB instead of using print
statements everywhere in my code, but I’m running into problems. Most notably, GDB seems to not know what to do whenever an implicit loop is used when generating an array inside a function. Take the code below, for instance:
!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%!
! Main program:
!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%!
program main
use :: iso_fortran_env, only: dp=>real64
implicit none
!++ Declare variables:
real(dp), dimension(11) :: arr
!++ Execution:
arr = linspace(0._dp,2._dp,11)
print*, arr
contains
!**********************************************************************************************************************************!
function linspace(a,b,N) result(lin_arr)
!! Equivalent to Numpy's numpy.linspace.
!! Returns an array whose "N" elements are linearly spaced, from "a" to "b" (inclusive).
!! The array's indices will be bounded in the region (0,N-1), so that the i-th element is simply
!! x_i = a + dx*i, i=0,1,...,N-1.
!! dx = (b-a)/(N-1)
!++ Declare variables:
! IN
real(dp), intent(in) :: a,b ! Linspace limits.
integer, intent(in) :: N ! Total array elements.
! INOUT/OUT
real(dp),dimension(0:N-1) :: lin_arr
! Local
integer :: i ! Loop index
real(dp) :: dx ! Lattice spacing
!++ Initialization:
dx=(b-a)/(N-1)
!++ Execution:
! lin_arr = a + dx*[0,1,2,...,N-1]
lin_arr = a+dx*[(i, i=0,N-1)]
end function
!**********************************************************************************************************************************!
end program
I’m using VS Code for graphical debugging. This program was compiled with the following flags:
gfortran -O0 -g -Wall -Wextra -pedantic -fcheck=all main.f90 -o main
When debugging, after stepping inside the linspace
function, the debugger hangs upon reaching lin_arr = a+dx*[(i, i=0,N-1)]
, and VS Code accuses the following:
Unable to open 'malloc.c': File not found.
I tried to debug it manually, and had the same problem. In VS Code, this call stack is the one where the debugger hangs:
libc.so.6!__GI___libc_malloc(size_t bytes) (/build/glibc-sMfBJT/glibc-2.31/malloc/malloc.c:3023)
MAIN__::linspace(real(kind=8) (11) __result, real(kind=8) a, real(kind=8) b, integer(kind=4) n) ([insert_path_here]/main.f90:44)
MAIN__() ([insert_path_here]/main.f90:14)
Does anyone know what I’m doing wrong? After some time looking up the internet for answers, I noticed some StackOverflow threads commenting about “bad array allocation”. Is that the case?
All help is appreciated.