Fortran common blocks with diferent sizes? 
Author Message
 Fortran common blocks with diferent sizes?

fortran language says that named common blocks must be the same size,
but one program I have has different size in different subroutines
for the same named commons, it seems work fine. The systems I used are
Sun and AIX, is this because the compilers are intelligent enough
to make the common to be the largest size declared, or just by
accident? The subroutines are within separated files. Thanks very much.


Thu, 28 Aug 1997 11:16:30 GMT  
 Fortran common blocks with diferent sizes?


Quote:
> Fortran language says that named common blocks must be the same size,
> but one program I have has different size in different subroutines
> for the same named commons, it seems work fine. The systems I used are
> Sun and AIX, is this because the compilers are intelligent enough
> to make the common to be the largest size declared, or just by
> accident? The subroutines are within separated files. Thanks very much.

        It's usually the _linker_,  not  the compiler, that resolves the
    varying  sizes  of  a  particular named COMMON  and  comes  up  with
    something that works for you, typically aloocating enough space  for
    the largest declared size.  And while it works on all platforms I've
    encountered  (so  far),  it is _not_ standard and should be avoided.
    In fact, I seem  to  recall  someone  posting that somebody's linker
    used  the  _first_  declaration of a named COMMON, rather  than  the
    largest...

        The way  to  avoid  having  different  subroutines  declare  the
    COMMONs  differently is by putting the declaration for the COMMON in
    an include file, and then INCLUDE that  in  all  subroutines,  etc.,
    that need to reference it.

        -Ken
--

 SLAC, P.O.Box 4349, MS 46  |  DECnet:   45537::FAIRFIELD (45537=SLACVX)
 Stanford, CA   94309       |  Voice:    415-926-2924    FAX: 415-926-3515
 -------------------------------------------------------------------------
 These opinions are mine, not SLAC's, Stanford's, nor the DOE's...



Thu, 28 Aug 1997 12:50:13 GMT  
 Fortran common blocks with diferent sizes?

Quote:



>> Fortran language says that named common blocks must be the same size,
>> but one program I have has different size in different subroutines
>> for the same named commons, it seems work fine. The systems I used are
>> Sun and AIX, is this because the compilers are intelligent enough
>> to make the common to be the largest size declared, or just by
>> accident?
>    In fact, I seem  to  recall  someone  posting that somebody's linker
>    used  the  _first_  declaration of a named COMMON, rather  than  the
>    largest...

I would say that it works fine both
        a) because the linkers are intelligent enough
and
        b) because you are lucky

In the case of some linkers you will find that it is the common
block size in the routine where data stored in that common
block are initialized (e.g.; with BLOCK DATA) that is assumed.

This can obviously lead to very bad cases of overwriting memory.
--  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --
Steve Allen          UCO/Lick Observatory       Santa Cruz, CA 95064

Notice:  The{*filter*}.ucsc.edu domain is now changing to ucolick.org



Fri, 29 Aug 1997 04:58:00 GMT  
 Fortran common blocks with diferent sizes?

Quote:

>Fortran language says that named common blocks must be the same size,
>but one program I have has different size in different subroutines
>for the same named commons, it seems work fine. The systems I used are
>Sun and AIX, is this because the compilers are intelligent enough
>to make the common to be the largest size declared, or just by
>accident? The subroutines are within separated files. Thanks very much.

On AIX, the size of a named common block will be set to the largest size
declared among input files.


Sat, 30 Aug 1997 05:33:42 GMT  
 Fortran common blocks with diferent sizes?

Quote:

> Fortran language says that named common blocks must be the same size,
> but one program I have has different size in different subroutines
> for the same named commons, it seems work fine. The systems I used are
> Sun and AIX, is this because the compilers are intelligent enough
> to make the common to be the largest size declared, or just by
> accident? The subroutines are within separated files. Thanks very much.

As others have said, although linkers often handle this problem by using the
largest declared size for the common block, you should consider yourself
lucky.  Consider the following example:

program main
integer a(5),b
common /x/ a,b
do i = 1,5
   a(i) = i
enddo
call sub1



Mon, 01 Sep 1997 12:01:54 GMT  
 Fortran common blocks with diferent sizes?

Quote:

>    The way  to  avoid  having  different  subroutines  declare  the
>    COMMONs  differently is by putting the declaration for the COMMON in
>    an include file, and then INCLUDE that  in  all  subroutines,  etc.,
>    that need to reference it.

Or use blank common ! Then you don't need SAVE, either :-)
--
Sepe Susi ("Zeke the Wolf")
alias Tero Tommila
KCL/Finland



Fri, 05 Sep 1997 16:38:26 GMT  
 Fortran common blocks with diferent sizes?
THE TOP LISTING DOES NOT WORK AND THE BOTTOM ONE DOES!
THE ONLY LINE DIFFERENT IS THE FORMAT LINE.
DOES ANYONE KNOW WHY???

*** THIS PROGRAM DOES NOT READ AND WRITE THE DATA FILE BELOW ***

       PROGRAM READIT
       CHARACTER*132 LINE1
       OPEN(16,FILE='TEST.IN')
       DO 100 I=1,5
       READ (16,1000)LINE1(1:10)
100    PRINT *,LINE1(1:132)
1000   FORMAT(A132)
       END

*** THIS PROGRAM READS AND WRITES THE DATA FILE BELOW ***

       PROGRAM READIT
       CHARACTER*132 LINE1
       OPEN(16,FILE='TEST.IN')
       DO 100 I=1,5
       READ (16,1000)LINE1(1:10)
100    PRINT *,LINE1(1:132)
1000   FORMAT(A10)
       END

TEST.IN DATA FILE

ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890
ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890
ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890
ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890
ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890
ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890



Sun, 14 Sep 1997 06:17:02 GMT  
 Fortran common blocks with diferent sizes?

Quote:
>THE TOP LISTING DOES NOT WORK AND THE BOTTOM ONE DOES!
>THE ONLY LINE DIFFERENT IS THE FORMAT LINE.
>DOES ANYONE KNOW WHY???

Yes.  Answer below (code included for those who follow the followup).

Quote:
>*** THIS PROGRAM DOES NOT READ AND WRITE THE DATA FILE BELOW ***
>       PROGRAM READIT
>       CHARACTER*132 LINE1
>       OPEN(16,FILE='TEST.IN')
>       DO 100 I=1,5
>       READ (16,1000)LINE1(1:10)
>100    PRINT *,LINE1(1:132)
>1000   FORMAT(A132)
>       END
>*** THIS PROGRAM READS AND WRITES THE DATA FILE BELOW ***
>       PROGRAM READIT
>       CHARACTER*132 LINE1
>       OPEN(16,FILE='TEST.IN')
>       DO 100 I=1,5
>       READ (16,1000)LINE1(1:10)
>100    PRINT *,LINE1(1:132)
>1000   FORMAT(A10)
>       END
>TEST.IN DATA FILE
>ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890
>ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890
>ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890
>ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890
>ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890
>ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890

In the first example, you use the FORMAT A132 to read something on to
a CHARACTER variable of length 10.  This means that the 10 rightmost
elements, nos. 123--132 in your example, will go into LINE(1:10).  Since
they're blanks in your example, you won't see anything.  If L is the length
of your variable, and W the `width' after the A in your FORMAT statement,
then the first (W-L) positions will be ignored.  IF (W .EQ. L) then no
leading positions will be ignored, as in your second example, and you
get what you expect.

This isn't what one would expect compared to the CHARACTER assignment
or list-directed CHARACTER input, where you get the leftmost stuff.

In most cases this problem can be avoided by using A without any
qualification in your FORMAT specifier.



Sun, 14 Sep 1997 13:12:40 GMT  
 Fortran common blocks with diferent sizes?

|> THE TOP format statement DOES NOT WORK AND THE BOTTOM ONE DOES!
|>
|>        PROGRAM READIT
|>        CHARACTER*132 LINE1
|>        OPEN(16,FILE='TEST.IN')
|>        DO 100 I=1,5
|>        READ (16,1000)LINE1(1:10)
|> 100    PRINT *,LINE1(1:132)
|> *** with this line it DOES NOT READ AND WRITE THE DATA FILE BELOW ***
|> 1000   FORMAT(A132)
|> *** with this it READS AND WRITES THE DATA FILE BELOW ***
|> 1000   FORMAT(A10)
|>
|> TEST.IN DATA FILE
|> ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890

   Why are you reading only the first 10 characters of LINE1 and
   then pretending to print it all without having initialized ?

   Anyhow you could just use FORMAT(A) and save lot of troubles.

   In the non working case you are trying to read a 10-char string
   specifying a format for a longer string, obviously doesn't work !

--
----------------------------------------------------------------------------
       A member of  G.ASS : Group for Astronomical Software Support          
----------------------------------------------------------------------------
Lucio Chiappetti - IFCTR/CNR     | Ma te' vugl' da' quost avis a ti' Orsign  
via Bassini 15 - I-20133 Milano  | Buttet rabios intant te se' pisnign      

Decnet:   IFCTR::LUCIO           |             (Rabisch, II 46, 119-120)    
----------------------------------------------------------------------------



Mon, 15 Sep 1997 03:00:00 GMT  
 Fortran common blocks with diferent sizes?
: THE TOP LISTING DOES NOT WORK AND THE BOTTOM ONE DOES!
: THE ONLY LINE DIFFERENT IS THE FORMAT LINE.
: DOES ANYONE KNOW WHY???
:
:
:
: *** THIS PROGRAM DOES NOT READ AND WRITE THE DATA FILE BELOW ***
:
:        PROGRAM READIT
:        CHARACTER*132 LINE1
:        OPEN(16,FILE='TEST.IN')
:        DO 100 I=1,5
:        READ (16,1000)LINE1(1:10)
: 100    PRINT *,LINE1(1:132)
: 1000   FORMAT(A132)
:        END
:
:
:
:
: *** THIS PROGRAM READS AND WRITES THE DATA FILE BELOW ***
:
:        PROGRAM READIT
:        CHARACTER*132 LINE1
:        OPEN(16,FILE='TEST.IN')
:        DO 100 I=1,5
:        READ (16,1000)LINE1(1:10)
: 100    PRINT *,LINE1(1:132)
: 1000   FORMAT(A10)
:        END
:
:
: TEST.IN DATA FILE
:
: ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890
: ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890
: ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890
: ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890
: ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890
: ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890
:

The first program does read the data, but with the A132 format, the data
is read as a 132 wide character string that is right justified. Then when
you read LINE1(1:10) it takes the first 10 characters of that character string
which are blank.
The second program's format statement exactly matches the length of the
character string in your read statement, so there is no problem.
Some compilers allow you to specify a FORMAT(A) so that your read statement
will always match the format.

--
Kevin Golde
University of Rhode Island
Graduate School of Oceanography Computer Center



Fri, 26 Sep 1997 03:00:00 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. Named Common Blocks: How is there size determined?

2. Passing fixed size arrays in common blocks.

3. Sharing Fortran COMMON blocks with Ada

4. Linking data to a Fortran Common Block

5. Visual FORTRAN Console Apps sharing a Common Block

6. Common-block in fortran-dll

7. Fortran common blocks in shared libraries on OSF/1

8. VBA/FORTRAN/COMMON BLOCKS

9. Multi-threading with common blocks in Fortran

10. Common blocks to a FORTRAN DLL

11. Sun Fortran and block data for common

12. IBM AIX problem: Fortran common blocks in C++/F77 shared lib

 

 
Powered by phpBB® Forum Software