Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing pointer to a stack allocated array to function
    primarykey
    data
    text
    <p>I have a function with the prototype below, the 3rd parameter (output variable) is the only relevant:</p> <pre><code>int ioman_write_pages(void**, unsigned, HDD_address**, /*rest is irrelevant*/) </code></pre> <p>The HDD_address type in case someone asks:</p> <pre><code>typedef union { unsigned long long address; struct { unsigned long long offset : 12; unsigned long long page_number : 26; unsigned long long : 0; } paddress; } HDD_address; </code></pre> <p><strong>Call scenario with heap allocation</strong> (Working perfectly):</p> <pre><code>HDD_address* page = (HDD_address*)malloc( pagecount * sizeof(HDD_address) ); memset( page, 0, pagecount * sizeof(HDD_address) ); ioman_write_pages( data, 3, &amp;page, block, NULL, NULL ); printf( "Pages written to: [%d, %d, %d]\n", page[0].address &gt;&gt; 9, page[1].address &gt;&gt; 9, page[2].address &gt;&gt; 9 ); </code></pre> <p>Output (Expected: 0, 16, 24):</p> <pre><code>Pages written to: [0, 16, 24] </code></pre> <p>The above heap allocation works no problem.</p> <p><strong>If however I attempt a stack allocation of "page":</strong></p> <pre><code>HDD_address page[pagecount]; memset( page, 0, pagecount * sizeof(HDD_address) ); ioman_write_pages( data, 3, &amp;page, block, NULL, NULL ); </code></pre> <p>Output, while data wise nonsensical, it is consistent:</p> <pre><code>Pages written to: [166956, 0, 0] </code></pre> <p>Compile warning when compiling the above:</p> <pre><code>note: expected ‘union HDD_address **’ but argument is of type ‘union HDD_address (*)[(sizetype)(pagecount)]’ </code></pre> <p><strong>Stack allocation, "fixing" the warning:</strong></p> <pre><code>DD_address page[pagecount]; memset( page, 0, pagecount * sizeof(HDD_address) ); HDD_address* ppage = page; int r = ioman_write_pages( data, 3, &amp;ppage, block, NULL, NULL ); </code></pre> <p>Output, again consistent every run but nonsense:</p> <pre><code>Pages written to: [1610376, 2856870, 610607] </code></pre> <p>I cannot make any sense of this at all, someone please enlighten me!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload