Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>I always get 0 from Integers and false from Booleans for all record members. </p> <p>I tried turning various compiler options (debugging, optimizations, etc.) on and off, but there was no difference. All my record members are being initialized.</p> <p>What am I missing?</p> </blockquote> <p>Well, apart from your test using global instead of local variables: the important thing that you are missing is the distinction between variables that <em><strong>coincidentally appear</strong></em> to be initialised, and variables that <em><strong>actally are</strong></em> initialised.<br> <strong>BTW</strong>: This is the reason programmers who don't check their warnings make the common mistake of assuming their poorly written code is behaving correctly when the few tests they do; happen to have 0 and False defaults.... <code>Want To Buy: random initialisation of local variables for debug builds.</code></p> <p>Consider the following variation on your test code:</p> <pre><code>program LocalVarInit; {$APPTYPE CONSOLE} procedure DoTest; var I, J, K, L, M, N: Integer; S: string; begin Writeln('Test default values'); Writeln('Numbers: ', I:10, J:10, K:10, L:10, M:10, N:10); Writeln('S: ', S); I := I + 1; J := J + 2; K := K + 3; L := L + 5; M := M + 8; N := N + 13; S := 'Hello'; Writeln('Test modified values'); Writeln('Numbers: ', I:10, J:10, K:10, L:10, M:10, N:10); Writeln('S: ', S); Writeln(''); Writeln(''); end; begin DoTest; DoTest; Readln; end. </code></pre> <p>With the following sample output:</p> <pre><code>Test default values Numbers: 4212344 1638280 4239640 4239632 0 0 S: Test modified values Numbers: 4212345 1638282 4239643 4239637 8 13 //Local vars on stack at end of first call to DoTest S: Hello Test default values Numbers: 4212345 1638282 4239643 4239637 8 13 //And the values are still there on the next call S: Test modified values Numbers: 4212346 1638284 4239646 4239642 16 26 S: Hello </code></pre> <p><strong>Notes</strong></p> <ul> <li>The example works best if you compile with optimisation off. Otherwise, if you have optimisation on: <ul> <li>Some local vars will be manipulated in CPU registers.</li> <li>And if you view the CPU stack while stepping through the code you'll note for example that <code>I := I + 1</code> doesn't even modify the stack. So obviously the change cannot be carried through.</li> </ul></li> <li>You could experiment with different calling conventions to see how that affects things.</li> <li>You can also test the effect of setting the local vars to zero instead of incrementing them.</li> <li>This illustrates how you are entirely dependent on what found its way onto the stack <em>before</em> your method was called.</li> </ul>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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