- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I have been struggling with a bug in my program where, after calling a function which allocated a number of data structure arrays, the program crashed later on with what looks like a stack corruption error when trying to initialise a dialog box a second time:
0xc0000005: access violation reading location 0xFFFFFFFFFFFFFFFF
After spending most of the day trying to get to the bottom of this I think I've found the cause.
One of the allocatable arrays was incorrectly allocated with a zero size i.e.
allocate(array(0),stat=ier)though it was never used. By resolving this I hope the bug has been fixed as program continues as expected but I wondered:
Is it legal Fortran to assign allocatable arrays with 0 size. If not why aren't such errors trapped by the compiler?
It is ok to allocate an array with the statement such as allocate(array(0:100),stat=ier) as this results in an array with size=101.
I'm using latest version of IFORT on Windows.
Has anyone got any thoughts on this?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Yes zero size array allocation is legal. I do not see why this would cause a crash if you did not try to use it. Some usage cases would be a problem. If you have stack or other corruption ANY code change can make the code magically work cause you have move things in the memory organisation and you now are corrupting something that is not important. Can you make a reproducer of this problem?
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Yes zero size array allocation is legal. I do not see why this would cause a crash if you did not try to use it. Some usage cases would be a problem. If you have stack or other corruption ANY code change can make the code magically work cause you have move things in the memory organisation and you now are corrupting something that is not important. Can you make a reproducer of this problem?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
>>It is ok to allocate an array with the statement such as allocate(array(0:100),stat=ier) as this results in an array with size=101.
Yes. You can also
allocate(array(-50:50)
do i = -50, 50
array(i) = i
end do
Jim Dempsey
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks Andrew & Jim,
I vaguely remember Steve Lionel responded to me many years ago to a similar question regarding the allocation of zero sized arrays but I wasn't sure of the context. Having looked further into my code the zero allocated array was being used after all so that was probably the likely cause of the corruption. The program is huge so trying to create a reproducer will take too long and as everything was working fine until I added this new function I think, fingers crossed, the issue should be resolved now.
Thanks again for taking time to reply
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
>>Having looked further into my code the zero allocated array was being used after all so that was probably the likely cause of the corruption.
If you run your application in Debug mode with all runtime checks (in particular indexing an array out of bounds), this will catch cases where an array, allocated to 0 size, being indexed, will generate a runtime error. And provide you with information as to where this occurred. You can then examine the state of the program to see how you got there. And if necessary, insert some diagnostic code to help insert a proper break point prior to the error condition. This too will help you determine what is happening and lead to a solution.
Jim Dempsey
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks Jim,
I did have the array bounds check enabled in debug mode but this didn't flag anything up. I am using whole array methods to copy data rather than indexing so not sure if the checker will detect any issues.
One thing I did notice was that any attempt to allocate anything after the corruption would fail.
I just ended up commenting out code where I suspected to problem was and eventually homed in on the zero sized array declaration.
Since making the array a finite size everything has been working fine.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
>>I am using whole array methods to copy data rather than indexing so not sure if the checker will detect any issues.
Then should the zero length allocatable array be on the LHS it would(should) get reallocated without error (barring allocation failure).
If used on the RHS, then... not sure as to what should happen.
Note, there is another post on this forum regarding maxloc and minloc when supplied with a zero length array. The language standard apparently hasn't defined the behavior in this case. I am unaware of what should happen when the LHS is
a) an allocatable array
b) an array with defined size
c) a zero length array.
I suggest you identify the sensitive placed in your code, and write your preference as to what to do in this situation....
Raise an error, use default values, use unusual values, use Signaling NAN's, ...
Jim Dempsey

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page