tags:

views:

67

answers:

1

I have code that calls an ENTRY in a SUBROUTINE before the SUBROUTINE. Are the variables allocated?

SUBROUTINE foo
character*5 A,B,C
DIMENSION IA(50),IB(50)
print* A,B,C
RETURN
ENTRY bar
DO 50 I=1,50
TOTAL = TOTAL + IA(I)
50 CONTINUE
print* TOTAL
RETURN
END

So if I CALL bar before foo is IA allocated?

+1  A: 

As far as I know, no initialization takes place on stack allocated data unless you have a special switch during compilation, and this is not existent in all compilers (IBM xlf has it but I don't remember it).

That means that those arrays will be full of random garbage.

In any case, I strongly suggest you not to use ENTRY unless your life depends on it. As you are presenting it, I don't really see a reason to use it, except setting the state beforehand and then calling the ENTRY symbol, but there are much better, cleaner alternatives.

If you mean allocated, then it definitely does. This code

  program hello
  call bar
  end
  SUBROUTINE foo
  character A(12345)
  a(1) = "hello"
  ENTRY bar
  print *, a(1)
  RETURN
  END

gets compiled to this code (long stuff)

.globl _bar_
_bar_:
    pushl   %ebp
    movl    %esp, %ebp
    subl    $24, %esp
    movl    $0, -8(%ebp)
    movl    $1, -4(%ebp)
    subl    $4, %esp
    pushl   $0
    leal    -12(%ebp), %eax
    pushl   %eax
    leal    -4(%ebp), %eax
    pushl   %eax
    call    ___g95_master_0__
    addl    $16, %esp
    movl    %eax, -8(%ebp)
    movl    -8(%ebp), %eax
    leave
    ret
.globl _foo_
_foo_:
    pushl   %ebp
    movl    %esp, %ebp
    subl    $24, %esp
    movl    $0, -8(%ebp)
    movl    $0, -4(%ebp)
    subl    $4, %esp
    pushl   $0
    leal    -12(%ebp), %eax
    pushl   %eax
    leal    -4(%ebp), %eax
    pushl   %eax
    call    ___g95_master_0__
    addl    $16, %esp
    movl    %eax, -8(%ebp)
    movl    -8(%ebp), %eax
    leave
    ret

Which as you can see is basically the same (namely, ENTRY are just "copied" routines for the initialization part, and then fork out later on) the actual allocation happens in ___g95_master_0_

___g95_master_0__:
    pushl   %ebp
    movl    %esp, %ebp
    pushl   %ebx
    subl    $12372, %esp
    call    L8

Where you see that stack pointer decremented, and you can see it gets called in both routines.

Of course, if your first part contains an ALLOCATABLE variable and an ALLOCATE, then things change. in that case, I am faily sure it won't get allocated. and you will have a crash, but that's another question.

Stefano Borini
Sorry for the bad terminology. I meant allocated. Will those references be valid even though I have not run that initial bit of code?
Clint