Undefined behavior for intent(out) spliced array in fortran 90?
I am experiencing very weird behavior when I do this. I assume this is the origin of the issue, but I could be wrong. If any of you can confirm this is indeed undefined behavior, I would at least know what's going on
suppose I have (does not pretend to compile)
subroutine X
real, allocatable :: block(:,:)
allocate(block(20,20))
call Sub(block(1:5, 1:5))
! here is undefined behavior
end subroutine
subroutine Sub(b)
real, intent(out) :: b(:,:)
b = 0.0
end subroutine
My question is: am I doing something weird ? I have the feeling that the intent(out) undefines the whole block, even if I passed a slice, and I need an inout. Can you confirm from the standard ?
Ed开发者_JS百科it: inout gives the same undefined behavior, but passing a non-sliced array to Sub works. Does this mean that it is a violation of the standard to pass sliced arrays for initialization of subblocks ?
Thanks
First of all, if you pass a slice, I suspect you need to use intent(inout) because you're not going to assign the whole array, although I'm not entirely sure about that, intent(out) might be correct in this case.
But isn't the source of your problem is that you are making the mistake of using an assumed-shape array in a subroutine without having an explicit interface or putting it in a module? Or did you omit that part of the code?
I added an explicit interface to your code, assigned 1 to block, compiled, and then I could define any kind of slice, it was set correctly to 0. So if you are doing things correctly, maybe you should elaborate on what kind of undefined behaviour you are seeing exactly?
精彩评论