Help c++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Help c++

Why in this code the x is not printed and what happened? edit:What is this random numbers? https://code.sololearn.com/co0Br4L56MUL/?ref=app

3rd Aug 2018, 10:53 PM
I Am Arthur
I Am Arthur - avatar
11 Answers
+ 4
James Jason Edelson Thanks for the help guys
3rd Aug 2018, 11:31 PM
I Am Arthur
I Am Arthur - avatar
+ 4
Because the integer type's upper bound is limited to `2147483647`. When the result of `b + c` exceeds the upper range, the outcome will be an overflow (a.k.a. those negative values). one solution would be declaring all variables as unsigned long long to increase the acceptable range to `18446744073709551615`. Another solution would be adding a second condition inside the loop to check against integer overflow in each iteration as while ( x < n && (long long)b + c <= numeric_limits<int>::max() ) Also adding the #include <limits> is mandatory at the top of the program.
4th Aug 2018, 2:57 PM
Babak
Babak - avatar
+ 3
James No, when the tutorial put a code with a "try yourself" i like to always change things in there
3rd Aug 2018, 11:29 PM
I Am Arthur
I Am Arthur - avatar
+ 3
Arthur If you ever happened to inspect the memory contents in an IDE or a Hex editor, you probably come up with a series of hexadecimal values. If you defined an integer array with 5 elements, then the compiler sets aside 20 bytes (assuming the size of int is 4 byte) of memory for the array. int myArr[5] = {1, 2, 3, 4, 5}; int x = 8; 0xfff1011c 08 f2 a0 41 // garbage 0xfff10120 01 00 00 00 // [0] 0xfff10124 02 00 00 00 // [1] 0xfff10128 03 00 00 00 // [2] 0xfff1012c 04 00 00 00 // [3] 0xfff10130 05 00 00 00 // [4] 0xfff10134 ba f3 21 00 // garbage 0xfff10138 08 00 00 00 // x ( garbage from the array POV ) The allocated space begins from and ends to a particular address. Any value before and after those boundary are considered garbage value from the array point of view, even if they get allocated in your program by another variable immediately after the array. So, if the loop violates the array's boundary, chances are the index lands on x's value.
4th Aug 2018, 8:23 AM
Babak
Babak - avatar
+ 3
It's simple Arthur ! myArr just refers to the base address of the array or simply &myArr[0] or &myArr which gives you 0xfff10120 based on my earlier example. On the other hand, myArr[x], "dereferences" the content of the current offset from the base address indicated by range index "x", that is, *(myArr + x). Also, range = {x | 0 <= x < array size}
4th Aug 2018, 12:13 PM
Babak
Babak - avatar
+ 2
Jason Edelson Ok,but whats is this random numbers?
3rd Aug 2018, 11:06 PM
I Am Arthur
I Am Arthur - avatar
+ 2
In c++ you cant whole arrays like you can in python, your currently outputting x followed by : followed by values in array that were never assigned followed by memory pointer for array on a new line
3rd Aug 2018, 11:16 PM
JME
+ 2
Jason Edelson I know, i was testing what happening if i past the array but i forgot the myarr [x]= 42 and this happened
3rd Aug 2018, 11:30 PM
I Am Arthur
I Am Arthur - avatar
+ 1
The x is printed, you can see it if you look right before the colon 0: 5059648 0x23fe301: 0 0x23fe302: 3 0x23fe303: 0
3rd Aug 2018, 11:02 PM
JME
+ 1
C++ Soldier (Babak) And what about the myarr[x]? Why is it different of myarr?
4th Aug 2018, 11:45 AM
I Am Arthur
I Am Arthur - avatar
+ 1
C++ Soldier (Babak) Can you help me in another thing?Why in this code with you put long number the output is negative? https://code.sololearn.com/cw25FKRTCGA0/?ref=app
4th Aug 2018, 1:19 PM
I Am Arthur
I Am Arthur - avatar