2
I have this code:
#include <iostream>
int main(int x=1) {
while (x <= 1000 && std::cout << x++ << std::endl) {}
}
I wrote it on Gedit, using Debian, and compiled it with g++ from Debian itself, without ever having changed anything. The compiler never presented problems in other codes. However, in my code the output I receive is the range from 2 to 1000.
And if I compile using this site https://ideone.com/4663hX, for example, the output is from 1 to 1000.
Something interesting is that: if I change the type of validation within the while
, less than or equal to 1000 for less than 1000, only the program now displays the number 1.
What is going on?
NOTE: The code was developed for a challenge. Therefore, there is no discussion of a better way to develop the same output.
Assembly code:
.file "tes.cpp"
.local _ZStL8__ioinit
.comm _ZStL8__ioinit,1,1
.text
.globl main
.type main, @function
main:
.LFB969:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl %esp, %ebp
.cfi_def_cfa_register 5
andl $-16, %esp
subl $16, %esp
.L4:
cmpl $1000, 8(%ebp)
jg .L2
movl 8(%ebp), %eax
addl $1, 8(%ebp)
movl %eax, 4(%esp)
movl $_ZSt4cout, (%esp)
call _ZNSolsEi
movl $_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, 4(%esp)
movl %eax, (%esp)
call _ZNSolsEPFRSoS_E
movl (%eax), %edx
subl $12, %edx
movl (%edx), %edx
addl %edx, %eax
movl %eax, (%esp)
call _ZNKSt9basic_iosIcSt11char_traitsIcEEcvPvEv
testl %eax, %eax
je .L2
movl $1, %eax
jmp .L3
.L2:
movl $0, %eax
.L3:
testb %al, %al
jne .L4
movl $0, %eax
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
.LFE969:
.size main, .-main
.type _Z41__static_initialization_and_destruction_0ii, @function
_Z41__static_initialization_and_destruction_0ii:
.LFB978:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl %esp, %ebp
.cfi_def_cfa_register 5
subl $24, %esp
cmpl $1, 8(%ebp)
jne .L6
cmpl $65535, 12(%ebp)
jne .L6
movl $_ZStL8__ioinit, (%esp)
call _ZNSt8ios_base4InitC1Ev
movl $__dso_handle, 8(%esp)
movl $_ZStL8__ioinit, 4(%esp)
movl $_ZNSt8ios_base4InitD1Ev, (%esp)
call __cxa_atexit
.L6:
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
.LFE978:
.size _Z41__static_initialization_and_destruction_0ii, .-_Z41__static_initialization_and_destruction_0ii
.type _GLOBAL__sub_I_main, @function
_GLOBAL__sub_I_main:
.LFB979:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl %esp, %ebp
.cfi_def_cfa_register 5
subl $24, %esp
movl $65535, 4(%esp)
movl $1, (%esp)
call _Z41__static_initialization_and_destruction_0ii
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
.LFE979:
.size _GLOBAL__sub_I_main, .-_GLOBAL__sub_I_main
.section .init_array,"aw"
.align 4
.long _GLOBAL__sub_I_main
.hidden __dso_handle
.ident "GCC: (Debian 4.7.2-5) 4.7.2"
.section .note.GNU-stack,"",@progbits
You can show the output for this:
int main(int x, char** v) { std::cout << x << std::endl; }
? Must be 1.– Guilherme Bernal
It’s a real one. And I already knew that. Something interesting is that: if I change the type of validation inside the while, from smaller or equal to smaller, only, the program starts to display the number 1.
– HiHello
What version of GCC?
– Guilherme Bernal
It is version 4.7.
– HiHello
I’m running out of possibilities... Compile your code for Assembly and add the result to the question.
g++ file.cpp -S -o file.s
– Guilherme Bernal
I want the Assembly code your compiler generates.
– Guilherme Bernal
I edited the question by adding the generated code.
– HiHello
Um... my impression is that you are passing an argument on the command line when the value starts with 2. Can you check this? If so, maybe your question has a very local context and should be closed. But, I’m not sure. Maybe it’s a good motivation to include knowledge about command line arguments (and so I posted a response myself).
– Luiz Vieira