+ 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; }

10th Feb 2019, 2:48 AM
punnoose
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.
10th Feb 2019, 7:33 AM
David Carroll
David Carroll - avatar
+ 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.
11th Feb 2019, 6:36 PM
David Carroll
David Carroll - avatar
+ 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. 😉
11th Feb 2019, 10:13 PM
David Carroll
David Carroll - avatar
+ 3
David Carroll I was wondering if It is only happening to A, why not to C?
10th Feb 2019, 7:36 AM
Raj Chhatrala
Raj Chhatrala - avatar
+ 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
11th Feb 2019, 8:15 PM
peter
peter - avatar
+ 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
11th Feb 2019, 11:14 PM
peter
peter - avatar
+ 2
this is creapy peter
11th Feb 2019, 11:15 PM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 1
Well David Carroll I'm confused too
10th Feb 2019, 3:03 AM
Raj Chhatrala
Raj Chhatrala - avatar
+ 1
punnoose this question have been treated a lot of time in Q&A let me help you find the discussion
11th Feb 2019, 4:30 PM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 1
punnoose it is compiler dependent
11th Feb 2019, 4:31 PM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 1
https://www.sololearn.com/Discuss/1640690/?ref=app check that post and they are also others too, punnoose
11th Feb 2019, 4:37 PM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 1
that's quite impressive peter
11th Feb 2019, 8:19 PM
✳AsterisK✳
✳AsterisK✳ - avatar
0
2? All other lines doesn't affect a.
10th Feb 2019, 2:55 AM
Jomari Pantorilla
Jomari Pantorilla - avatar
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]...?
10th Feb 2019, 2:58 AM
punnoose
0
Answer should be 2
10th Feb 2019, 3:02 AM
Akash
Akash - avatar
0
I understand, actually variable store in memory as a stack.
10th Feb 2019, 8:40 AM
Akash
Akash - avatar
0
David Carroll thanks for the answer.. but how can we predict that output.. there should be some logic behind this, right?
10th Feb 2019, 8:53 AM
punnoose
0
i wrote a simple program for under stand the memory allocation https://code.sololearn.com/coR7rRtcs55d/?ref=app
10th Feb 2019, 9:08 AM
Akash
Akash - avatar
0
Dynamic memory allocation. My require pointer.
11th Feb 2019, 6:51 AM
Basavaraj Hunshal
Basavaraj Hunshal - avatar