<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re:Bad codegen for automatic reallocated arrays results in stack overflow in Intel® Fortran Compiler</title>
    <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Bad-codegen-for-automatic-reallocated-arrays-results-in-stack/m-p/1733676#M178038</link>
    <description>&lt;P&gt;The fix is implemented and will be available in the next major compiler release.&lt;/P&gt;&lt;BR /&gt;</description>
    <pubDate>Thu, 15 Jan 2026 21:41:25 GMT</pubDate>
    <dc:creator>Igor_V_Intel</dc:creator>
    <dc:date>2026-01-15T21:41:25Z</dc:date>
    <item>
      <title>Bad codegen for automatic reallocated arrays results in stack overflow</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Bad-codegen-for-automatic-reallocated-arrays-results-in-stack/m-p/1703187#M176277</link>
      <description>&lt;P&gt;The following code generates a stack overflow on Windows (for a standard 1MB stack) even when compiled with default "Release" settings.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="fortran"&gt;    module test
    
    contains
    subroutine add_lines()
    character(500):: line
    integer:: loop_file
    character(500),dimension(:),allocatable:: c

    allocate(c(0))
    c = ''

    do loop_file=1,10000
        line = 'abcd'
        c = [character(len=500) :: c , line]
    end do

    end subroutine
    end module test

    
    program Console1
    use test
    implicit none

    
    call add_lines()

    end program Console1
    &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Jul 2025 16:20:18 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Bad-codegen-for-automatic-reallocated-arrays-results-in-stack/m-p/1703187#M176277</guid>
      <dc:creator>tobias-loew</dc:creator>
      <dc:date>2025-07-14T16:20:18Z</dc:date>
    </item>
    <item>
      <title>Re: Bad codegen for automatic reallocated arrays results in stack overflow</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Bad-codegen-for-automatic-reallocated-arrays-results-in-stack/m-p/1703596#M176295</link>
      <description>&lt;P&gt;This example is curious.&amp;nbsp; I also see the stack exhaustion.&amp;nbsp; I can prevent it by adding -heap-arrays.&amp;nbsp; But it still should not need this option.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need to investigate this more.&amp;nbsp; it's not obvious to me why the constructor would blow up stack.&amp;nbsp; It should only use 500 chars of storage.&amp;nbsp; Might be that the RHS constructor is not being freed up after the copy to the LHS.&amp;nbsp; Just a suspicion.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Jul 2025 00:32:22 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Bad-codegen-for-automatic-reallocated-arrays-results-in-stack/m-p/1703596#M176295</guid>
      <dc:creator>Ron_Green</dc:creator>
      <dc:date>2025-07-16T00:32:22Z</dc:date>
    </item>
    <item>
      <title>Re: Bad codegen for automatic reallocated arrays results in stack overflow</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Bad-codegen-for-automatic-reallocated-arrays-results-in-stack/m-p/1703653#M176296</link>
      <description>&lt;P&gt;Yes, I also just found out that&amp;nbsp;&lt;SPAN&gt;-heap-arrays (even with sizes far greater than the stack size) prevents it.&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;But without&amp;nbsp;-heap-arrays:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;While debugging through the loop, the&amp;nbsp;stack-pointer is growing all the time.&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;After leaving "add_lines" the stack-pointer has the correct value again (when the&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;loop-counter is below the stack-overflow limit, of course).&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Jul 2025 06:27:36 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Bad-codegen-for-automatic-reallocated-arrays-results-in-stack/m-p/1703653#M176296</guid>
      <dc:creator>tobias-loew</dc:creator>
      <dc:date>2025-07-16T06:27:36Z</dc:date>
    </item>
    <item>
      <title>Re:Bad codegen for automatic reallocated arrays results in stack overflow</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Bad-codegen-for-automatic-reallocated-arrays-results-in-stack/m-p/1704008#M176340</link>
      <description>&lt;P&gt;BugID is CMPLRLLVM-69147&lt;/P&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 17 Jul 2025 18:35:22 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Bad-codegen-for-automatic-reallocated-arrays-results-in-stack/m-p/1704008#M176340</guid>
      <dc:creator>Ron_Green</dc:creator>
      <dc:date>2025-07-17T18:35:22Z</dc:date>
    </item>
    <item>
      <title>Re: Bad codegen for automatic reallocated arrays results in stack overflow</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Bad-codegen-for-automatic-reallocated-arrays-results-in-stack/m-p/1718594#M177273</link>
      <description>&lt;P&gt;Let me explain in more detail what is happening in this case.&lt;/P&gt;
&lt;P&gt;First, 'c' is a local variable for subroutine add_lines.&amp;nbsp; Most compilers except Intel will allocate c from heap.&amp;nbsp; As you saw, Intel Fortran uses stack for local allocations by default.&amp;nbsp; So our behavior is different than other compilers by default.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;'c' is a array of rank 1, each element is 500 characters.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This code is creating 500 characters for each of the 10,000 elements that you create for c.&amp;nbsp; &amp;nbsp;If loop_file is larger, then c has more elements.&amp;nbsp; &amp;nbsp;the number of elements of c is the number of iterations of loop_file.&amp;nbsp; In effect, you are doing this:&lt;BR /&gt;&lt;BR /&gt;character(500), dimension(:), allocatable :: c&lt;/P&gt;
&lt;P&gt;allocate ( c(10000) )&amp;nbsp;&lt;BR /&gt;c(1:10000) = 'abcd'&amp;nbsp; &amp;nbsp; &amp;nbsp;! each element 500 chars beginning with abcd&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But your code, which I hope is not in any real application, does this :&lt;/P&gt;
&lt;LI-CODE lang="none"&gt;do loop_file=1,10000
        line = 'abcd'
        c = [character(len=500) :: c , line]
end do&lt;/LI-CODE&gt;
&lt;P&gt;&lt;BR /&gt;On line 3, the following happens:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;for the constructor, a temporary is created for c.&amp;nbsp; C is growing in each iteration.&amp;nbsp; for loop_file=1, C has 1 element ( C(1:1) ).&amp;nbsp; At each iteration C grows by another element (row or line if you want to think of it this way).&amp;nbsp; So the constructor is making a temporary each time for C, and C is growing in size by 500 x value of loop_file.&amp;nbsp; &amp;nbsp;Gfortran, NAG, others allocate that temp in heap.&amp;nbsp; Intel is growing the stack.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;After the temp is created, the LHS old data in C is deallocated, and reallocated for the size of the C-temporary on the RHS.&amp;nbsp;&lt;BR /&gt;Then the elements/data of C-temporary are copied into this new empty allocation of C.&amp;nbsp;&amp;nbsp;&lt;BR /&gt;Since stack is limited, you eventually blow out of the stack with Intel.&amp;nbsp; In gfortran and NAG, you are just sloppily growing heap.&amp;nbsp; And all along the way you are doing deallocates and reallocates and copies on each iteration.&amp;nbsp; Horrible coding.&amp;nbsp;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Fine, but if you want to do this filling in C with a loop going over each line that is your call.&amp;nbsp; Most of us would do 1 allocation of C with the number of element (rows or lines) that you want and do this just once.&amp;nbsp; Then fill the elements or lines one by one without the need for reallocation.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;So you want Intel Fortran to behave like gfortran or NAG or any other Fortran compiler?&amp;nbsp; Do this change for the declaration of C&lt;/P&gt;
&lt;P class="p1"&gt;&lt;SPAN class="s1"&gt;character(500),dimension(:),allocatable,&lt;STRONG&gt;save&lt;/STRONG&gt;:: c&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="p1"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class="p1"&gt;the SAVE attribute will cause Intel Fortran to use heap instead of stack.&amp;nbsp; Just like the -heap-arrays option.&amp;nbsp; And just like the other compilers.&amp;nbsp; Keep in mind, this is horrifically inefficient coding.&amp;nbsp; Nonetheless, it will give you the results you seek.&amp;nbsp; Of these 2 options, the SAVE attribute will work without the need for compiler option -heap-arrays.&amp;nbsp; Options are notorious in that sometime in the future you or someone following you will forget to add that option.&lt;BR /&gt;&lt;BR /&gt;We did find a small efficiency in our own creation of the C-temporary for the constructor.&amp;nbsp; we were able to reduce our temp size usage by 1/2.&amp;nbsp; Still, if you increased loop_file you will eventually hit stack limit without the 2 workarounds described.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 23 Sep 2025 16:45:20 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Bad-codegen-for-automatic-reallocated-arrays-results-in-stack/m-p/1718594#M177273</guid>
      <dc:creator>Ron_Green</dc:creator>
      <dc:date>2025-09-23T16:45:20Z</dc:date>
    </item>
    <item>
      <title>Re: Bad codegen for automatic reallocated arrays results in stack overflow</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Bad-codegen-for-automatic-reallocated-arrays-results-in-stack/m-p/1720904#M177385</link>
      <description>&lt;P&gt;Many thanks for the clarification. The code was originally from a students project.&lt;/P&gt;&lt;P&gt;But please correct if I'm wrong: using "save" will make the variable static (in the C/C++ sense) and will not work with parallel exection then.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Oct 2025 16:24:42 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Bad-codegen-for-automatic-reallocated-arrays-results-in-stack/m-p/1720904#M177385</guid>
      <dc:creator>tobias-loew</dc:creator>
      <dc:date>2025-10-07T16:24:42Z</dc:date>
    </item>
    <item>
      <title>Re:Bad codegen for automatic reallocated arrays results in stack overflow</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Bad-codegen-for-automatic-reallocated-arrays-results-in-stack/m-p/1733676#M178038</link>
      <description>&lt;P&gt;The fix is implemented and will be available in the next major compiler release.&lt;/P&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 15 Jan 2026 21:41:25 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Bad-codegen-for-automatic-reallocated-arrays-results-in-stack/m-p/1733676#M178038</guid>
      <dc:creator>Igor_V_Intel</dc:creator>
      <dc:date>2026-01-15T21:41:25Z</dc:date>
    </item>
    <item>
      <title>Re: Bad codegen for automatic reallocated arrays results in stack overflow</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Bad-codegen-for-automatic-reallocated-arrays-results-in-stack/m-p/1733724#M178045</link>
      <description>&lt;P&gt;Great! Thanks for the info!&lt;/P&gt;</description>
      <pubDate>Fri, 16 Jan 2026 07:13:45 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Bad-codegen-for-automatic-reallocated-arrays-results-in-stack/m-p/1733724#M178045</guid>
      <dc:creator>Tobias_Loew</dc:creator>
      <dc:date>2026-01-16T07:13:45Z</dc:date>
    </item>
  </channel>
</rss>

