+ 2
Predict the output and explain
I have attend this question in c challenge. But I am not sure of that answer int main() { int a=2; int b[]={2,3}; int c=4; b[3]=10; printf ("%d",a); return 0; }
24 Answers
+ 9
This is due to undefined behavior from index out of range.
The array b[] is initialized for 2 positions. When attempting to access the 4th position of the array as b[3], strange behavior like the memory address of int a might be accessed and modified.
+ 6
punnoose Since this is undefined behavior, you can't reliably predict the output beyond any given compiler, compile options, usage in code, and possibly environment.
While the result in this code sample running on the current servers hosting playground might correlate b[3] with a, this may not necessarily be the case in a variant scenario.
The compiler will optimize the code based on their specific handling of this undefined behavior. That handling could be different across compiler venders, versions, OSs, compile options, and hardware like CPU.
I've seen the behavior differ with slight variations in the same code. For example, changing the order of when the array is declared or the use of new variables or different array sizes may alter the behavior inconsistent from what you might expect given the previous behavior.
The only constant I've been able to observe is running the same code on the same server with the same compiler will result in the same behavior. Beyond that, it's unreliable.
+ 5
"An electron at the edge of the universe... must figure into the calculations..." 😂
peter Nice... thanks for sharing that anecdote. It was thoroughly enjoyed. 😉
+ 3
David Carroll
I was wondering if It is only happening to A, why not to C?
+ 3
lol is it ok to throw in an anecdote in predicting the unknown and just how complex that can become? lol
soon as i saw undefined behaviour and the word predict one famous man came to mind like a bolt of lightning lol
calculations were done by Prof. Sir Michael Berry in 1978 in his paper Regular and Irregular Motion, in Nonlinear Mechanics even in black swan.
billard balls
If you know a set of basic parameters concerning the ball at rest, can compute the resistance of the table (quite elementary), and can gauge the strength of the impact, then it is rather easy to predict what would happen at the first hit. The second impact becomes more complicated, but possible; and more precision is called for. The problem is that to correctly compute the ninth impact, you need to take account the gravitational pull of someone standing next to the table (modestly, Berry’s computations use a weight of less than 150 pounds). And to compute the fifty-sixth impact, every single elementary particle in the universe needs to be present in your assumptions! An electron at the edge of the universe, separated from us by 10 billion light-years, must figure in the calculations, since it exerts a meaningful effect on the outcome.
now predict the future, sorry i couldnt resist lol
+ 2
if any of you wish to exolore the actual brain frying material involved in probability esp the bayes are conditional probability then this article may help point people into the maze lol
https://medium.com/brandons-computer-science-notes/an-introdcution-to-probability-45a64aee7606
bayes returns probability of creation a god if you like is 67% probable 33% not, a Physicist from Manchester uni used it to write a book about that “the probability of god” ... there is some good areas in the article leaving god out of things lol
dont waste time in the monty hall area it will fry your mind, be easier to convince people dinosaurs are a conspiracy to prevent time travel than solve the monty hall thing lol
https://medium.com/brandons-computer-science-notes/an-introdcution-to-probability-45a64aee7606
+ 2
this is creapy peter
+ 1
Well David Carroll
I'm confused too
+ 1
punnoose this question have been treated a lot of time in Q&A let me help you find the discussion
+ 1
punnoose it is compiler dependent
+ 1
https://www.sololearn.com/Discuss/1640690/?ref=app
check that post and they are also others too, punnoose
+ 1
that's quite impressive peter
0
2? All other lines doesn't affect a.
0
No .. it is 10 actually.. but I don't know why.. as my knowledge if it is b[2]=10 ,it can affect a, but how b[3]...?
0
Answer should be 2
0
I understand, actually variable store in memory as a stack.
0
David Carroll thanks for the answer.. but how can we predict that output.. there should be some logic behind this, right?
0
i wrote a simple program for under stand the memory allocation
https://code.sololearn.com/coR7rRtcs55d/?ref=app
0
Dynamic memory allocation. My require pointer.