Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't know the cross-platform answer to your question. But this made me curious. So what do we do? Look at the stack!</p> <p>For the first iteration:</p> <p>test.c</p> <pre><code>int main(void) { return 0; } </code></pre> <p>test2.c</p> <pre><code>int main(int argc, char *argv[]) { return 0; } </code></pre> <p>And now look at the assembly output:</p> <pre><code>$ gcc -S -o test.s test.c $ cat test.s .file "test.c" .text .globl main .type main, @function main: pushl %ebp movl %esp, %ebp movl $0, %eax popl %ebp ret .size main, .-main .ident "GCC: (Ubuntu 4.4.3-4ubuntu5) 4.4.3" .section .note.GNU-stack,"",@progbits </code></pre> <p>Nothing exciting here. Except for one thing: <strong>both C programs have the same assembly output!</strong></p> <p>This basically makes sense; we never really have to push/pop anything off of the stack for main(), since it's the first thing on the call stack.</p> <p>So then I wrote this program:</p> <pre><code>int main(int argc, char *argv[]) { return argc; } </code></pre> <p>And its asm:</p> <pre><code>main: pushl %ebp movl %esp, %ebp movl 8(%ebp), %eax popl %ebp ret </code></pre> <p>This tells us that "argc" is located at <code>8(%ebp)</code> </p> <p>So now for two more C programs:</p> <pre><code>int main(int argc, char *argv[]) { __asm__("movl 8(%ebp), %eax\n\t" "popl %ebp\n\t" "ret"); /*return argc;*/ } int main(void) { __asm__("movl 8(%ebp), %eax\n\t" "popl %ebp\n\t" "ret"); /*return argc;*/ } </code></pre> <p>We've stolen the "return argc" code from above and pasted it into the asm of these two programs. When we compile and run these, and then invoke <code>echo $?</code> (which echos the return value of the previous process) we get the "right" answer. So when I run "./test a b c d" then <code>$?</code> gives me "5" for both programs - even though only one has argc/argv defined. This tells me that, on my platform, argc is for sure placed on the stack. I'd bet that a similar test would confirm this for argv.</p> <p>Try this on Windows!</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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