Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This answer attempts to address some of the sources of variation. It is a follow-up to Daniel Fischer’s answer and some comments to it.</p> <p>As I do not work with Linux, I cannot give a definitive answer. For a <code>printf</code> later in a large application, there would be a myriad of sources of potential variation. This early in a small application, there should be only a few.</p> <p>Address space layout randomization (ASLR) is one: The operating system deliberately rearranges some memory randomly to prevent malware for knowing what addresses to use. I do not know if Linux 3.4.4-2 has this.</p> <p>Another is environment variables. Your shell environment variables are copied into processes it spawns (and accessible through the <code>getenv</code> routine). A few of those might change automatically, so they would have slightly different values. This is unlikely to directly affect what <code>printf</code> sees when it attempts to use a missing integer argument, but there could be cascading effects.</p> <p>There may be a shared-library loader that runs either before <code>main</code> is called or before <code>printf</code> is called. For example, if <code>printf</code> is in a shared library, rather than built into your executable file, then a call to <code>printf</code> likely actually results in a call to a stub routine that calls the loader. The loader looks up the shared library, finds the module containing <code>printf</code>, loads that module into your process’ address space, changes the stub so that it calls the newly loaded <code>printf</code> directly in the future (instead of calling the loader), and calls <code>printf</code>. As you can imagine, that can be a fairly extensive process and involves, among other things, finding and reading files on disk (all the directories to get to the shared library and the shared library). It is conceivable that some caching or file operations on your system result in slightly different behavior in the loader.</p> <p>So far, I favor ASLR as the most likely candidate of the ones above. The latter two are likely to be fairly stable; the values involved would usually change occasionally, not frequently. ASLR would change each time, and simply leaving an address in a register would suffice to explain the <code>printf</code> behavior.</p> <p>Here is an experiment: After the initial <code>printf</code>, insert another <code>printf</code> with this code:</p> <pre><code>printf("%d\n", 2.443); int a; printf("%p\n", (void *) &amp;a); </code></pre> <p>The second printf prints the address of <code>a</code>, which is likely on the stack. Run the program two or three times and calculate the difference between the value printed by the first <code>printf</code> and the value printed by the second <code>printf</code>. (The second <code>printf</code> is likely to print in hexadecimal, so it might be convenient to change the first to "%x" to make it hexadecimal too.) If the value printed by the second <code>printf</code> varies from run to run, then your program is experiencing ASLR. If the values change from run to run but the difference between them remains constant, then the value that <code>printf</code> has happened upon in the first <code>printf</code> is some address in your process that was left lying around after program initialization.</p> <p>If the address of <code>a</code> changes but the difference does not remain constant, you might try changing <code>int a;</code> to <code>static int a;</code> to see if comparing the first value to different part of your address space yields a better result.</p> <p>Naturally, none of this is useful for writing reliable programs; it is just educational with regard to how program loading and initialization works.</p>
    singulars
    1. This table or related slice is empty.
    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.
    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