C++Guns – RoboBlog

04.04.2012

Scheiß Fortran

Filed under: Allgemein — Tags: — Thomas @ 10:04

Ich hasse es, wirklich.
Welche Ausgabe erwartet ihr bei diesem Programm?


subroutine func()
  implicit none
  real :: time = 0;
  time = time + 1
  write(*,*) time
end subroutine

program test
  implicit none
  integer :: i
  do i = 1, 10
    call func()
  end do
end

Zehn mal die Ausgabe von "1"?
Nein, falsch!

thomas@cluster:~$ ./a.out 
   1.000000    
   2.000000    
   3.000000    
   4.000000    
   5.000000    
   6.000000    
   7.000000    
   8.000000    
   9.000000    
   10.00000 

Dann ist time also keine lokale Variable die bei jedem Aufruf von func() auf dem Stack gelegt wird? Und schon garnicht wird sie mit 0 initialisiert. Ist sie static und existiert die ganze Zeit über? Dann aber nicht auf dem Stack. Das nervt...

// edit
Die Variable wird statisch, sobald man sie bei der deklaration auch initialisiert.
http://www.math.uni-leipzig.de/~hellmund/Vorlesung/scr4.pdf Seite 25 und 26

Durch die Initialisierung wird die entsprechende Variable statisch! Die Initialisierung wird nur einmal, beim Programmstart, durchgeführt.
...
Variablen sind statisch, wenn sie
a) das save-Attribut tragen
integer, save :: j
b) bei der De?nition initialisiert werden (s. oben) oder
c) nach der De?nition mit einer data-Anweisung initialisiert werden.

No Comments »

No comments yet.

RSS feed for comments on this post.

Leave a comment

You must be logged in to post a comment.

Powered by WordPress