1

I was having trouble running fortran code, so I tried an example code in here: https://gcc.gnu.org/onlinedocs/gcc-8.4.0/gfortran/ICHAR.html

program read_val integer value character(len=10) string, string2 string = '154' ! Convert a string to a numeric value read (string,'(I10)') value print *, value ! Convert a value to a formatted string write (string2,'(I10)') value print *, string2 end program read_val 

I did

gfortran -o hello3 hello3.f -g3 -fcheck=all -Wall -fbacktrace

And it gave me no warning nor error. However,

./hello3

failed with

Program received signal SIGSEGV: Segmentation fault - invalid memory reference. Backtrace for this error: #0 0x103eab35c #1 0x103eaa6f3 #2 0x7fff7376cb5c #3 0x103fef340 #4 0x103fefd2d #5 0x103fed78f #6 0x103ea5cca #7 0x103ea5e96 Segmentation fault: 11 

I somehow feel like my gfortran compiler doesn't work properly. I'm not familiar with Mac OS and feel like Xcode/Anaconda/etc messed up my system.

I'm using GNU Fortran (Homebrew GCC 9.3.0_1) 9.3.0, MacOS Mojave 10.14.6.

gfortran path is /usr/local/bin/gfortran Currently my gfortran is from 'brew install gcc'. I also tried manual download from https://github.com/fxcoudert/gfortran-for-macOS/releases, but it didn't worked either.

5
  • Can you comment out the write statement. I suspect you may be overflowing string2. Although, it should be caught with the fcheck=all. But hey, you never know. Next, I would comment out the read. Commented May 9, 2020 at 1:30
  • @Tarik When I commented one of them, I still get the SISEGV error. When I commented both, program runs, and I get '1669365499 f-' (second value changes everytime) But I don't see how that information helps... Commented May 9, 2020 at 1:35
  • Comment only one at a time to determine the cause. Commented May 9, 2020 at 1:36
  • @Tarik Sorry if I was not clear. When I comment either one of them, I still get the SIGSESV error, and they look same. Commented May 9, 2020 at 2:15
  • I always start investigating this way to get closer to the solution. The next thing I would do is increase the length of the strings to 15. It looks like an overflow. Commented May 9, 2020 at 10:29

1 Answer 1

3

As far as I can see the code is fine, and it complies and runs correctly on my system

ian@eris:~/work/stack$ cat busted.f90 program read_val integer value character(len=10) string, string2 string = '154' ! Convert a string to a numeric value read (string,'(I10)') value print *, value ! Convert a value to a formatted string write (string2,'(I10)') value print *, string2 end program read_val ian@eris:~/work/stack$ gfortran -std=f2008 -Wall -Wextra -fcheck=all -g busted.f90 ian@eris:~/work/stack$ ./a.out 154 154 

So as far as I can see your instillation of gfortran is broken. But please always use Implicit None

Sign up to request clarification or add additional context in comments.

1 Comment

I restarted computer and it worked... weird, but thanks for confirming me so I didn't wasted more time fiddling code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.