Fortran 90 中意图(out)拼接数组的未定义行为?

发布于 2024-10-05 18:02:58 字数 515 浏览 3 评论 0原文

当我这样做时,我遇到了非常奇怪的行为。我认为这是问题的根源,但我可能是错的。如果你们中的任何人都可以确认这确实是未定义的行为,我至少会知道发生了什么

假设我有(不假装编译)

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

我的问题是:我在做一些奇怪的事情吗?我有一种感觉,即使我传递了一个切片,意图(输出)也未定义整个块,并且我需要一个输入输出。可以从标准上确认吗?

编辑: inout 给出了相同的未定义行为,但将非切片数组传递给 Sub 是有效的。这是否意味着传递切片数组来初始化子块违反了标准?

谢谢

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 ?

Edit: 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

轻拂→两袖风尘 2024-10-12 18:02:58

首先,如果你传递一个切片,我怀疑你需要使用intent(inout),因为你不会分配整个数组,尽管我不完全确定,intent(out)可能是正确的这个案例。

但问题的根源不是在于您在子例程中使用了假定形状的数组而没有显式接口或将其放入模块中吗?或者你省略了那部分代码?

我在你的代码中添加了一个显式接口,分配 1 给块,编译,然后我可以定义任何类型的切片,它被正确设置为 0。所以,如果你正确地做事,也许你应该详细说明什么样的未定义你到底看到了什么行为?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文