Our product has code similar to the following. For me, the output is "0 1 2 3". But the output of a similar code is "1 1 1 1".
for(i = 0 ;i < 5;i++){ int j; if(i) printf("%d ",j); j = i; }
I understand that j is pushed onto the stack only once during the entire period of the 'for' loop, and the same value is used during iterations. Also, if I move ad j outside the loop, I get the expected result. What am I missing here?
PS - When I run the same code on my personal machine, I get the expected result. But in production it is different.
-, , C11, Β§6.2.4, ( )
C11
, static , [...]
static
,
, , , , , . ( , , .) , . .
, j. .
j
int j; //not initialized if(i) printf("%d ",j); //this one here
j, . undefined .
C11, Β§6.7.9
, UB, Β§J.2
, .
UB, .
OTOH, j , . , , j.
, , i, 0, if false, printf() , j . , , printf(), j , .
i
if
printf()
, , for :
i = 0; LoopStart: if(!(i<5)){ goto LoopEnd;} { int j; if(i) printf("%d ",j); j = i; } i++; goto LoopStart; LoopEnd:
, , : , , 5 . , , uninitialized j printf.
/. , - , j , , j, , , j, , .
Source: https://habr.com/ru/post/1628432/More articles:What is the difference between blob_url, raw_url and contents_url in the GitHub API? - gitAngular 2 allows the user to select and image from their local machine - angularMatplotlib value for numpy array image - pythonFibonacci recursion problem, cannot return 2 elements - javaAny reason to declare constexpr for a function that returns void? - c ++ΠΠ°ΠΊ ΠΎΡΠΊΠ»ΡΡΠΈΡΡ ΠΏΠ°Π½ΠΎΡΠ°ΠΌΠΈΡΠΎΠ²Π°Π½ΠΈΠ΅ Π»Π΅Π²ΠΎΠΉ ΠΊΠ½ΠΎΠΏΠΊΠΎΠΉ ΠΌΡΡΠΈ Π² d3 - javascriptusing an alias for static member functions? - c ++How to get a hostname that contains a specific string from a group in Ansible - pythonMagento2 Error: Sorry, an error occurred while creating this email - magento2Input sizes do not correspond to binary cross-entropy Lazan and Anano - dimensionAll Articles