357mag 3 Posted January 4 I've never used range-based for loops so I don't know how to write one that is keeping track of the sum of the elements in an array: int main() { int myArray[] = {2, 4, 7, 8, 9}; cout << "The values in the array are: "; for(int n : myArray) cout << n << " "; int sum = 0; for(int x : myArray) { x += myArray[x]; // don't know what to write here } cout << "The sum is: " << x << endl; // use of undeclared identifier system("pause"); return 0; } Share this post Link to post
Lajos Juhász 316 Posted January 4 It was a couple a decade ago when I wrote my previous C code. At that moment x is out of scope You should use sum to hold the result: sum +=myArray[x] cout << "The sum is: " << sum << endl; Share this post Link to post
357mag 3 Posted January 4 Something is still off. My console window is saying that sum is equal to 1985788. Share this post Link to post
Remy Lebeau 1568 Posted January 4 I find it interesting that you clearly knew how to access the element values in the 1st range-for loop, but didn't know how to do the exact same thing in the 2nd range-for loop. 2 Share this post Link to post
Rolf Fankhauser 1 Posted Friday at 06:55 PM (edited) I wonder why there is no AV or exception in the case of "sum +=myArray[x]" when the index is out of range? Because it's C++? Edited Friday at 09:09 PM by Rolf Fankhauser Share this post Link to post
Remy Lebeau 1568 Posted Friday at 07:38 PM (edited) 3 hours ago, Rolf Fankhauser said: I wonder why there is no AV or exception in the case of "sum +=myArray[x]" when the index is out of range? Because it's C++? Accessing an array out of bounds is Undefined Behavior. Anything can happen. An AV/exception is not guaranteed. C++ does not have bounds checking on vanilla C-style arrays. If you want that, switch to std::array or std::vector and use their at() method. In this particular example, the first few array values are valid indexes into the array, and the remaining values are small enough that the code would exceed the bounds of the array into surrounding memory but stay within the bounds of the calling thread's stack, It's valid memory access as far as the OS is concerned, thus no AV is thrown, but it's still Undefined Behavior as far as C++ is concerned. Edited Friday at 10:45 PM by Remy Lebeau Share this post Link to post
Rolf Fankhauser 1 Posted Friday at 09:05 PM (edited) Thanks Remy for the explanation! I run it several times and got different results for the invalid indexes. Index 7 always returned 14880996, the other results seem to be random : 7 (because myArray[2] = 7) 9 (because myArray[4] = 9) 14880996 16789504 16448156 7 9 14880996 3264512 2095744 7 9 14880996 4907008 7338908 Edited Friday at 09:11 PM by Rolf Fankhauser Share this post Link to post
Remy Lebeau 1568 Posted Friday at 10:47 PM (edited) 1 hour ago, Rolf Fankhauser said: I run it several times and got different results for the invalid indexes. Index 7 always returned 14880996, the other results seem to be random : That's like the very definition of Undefined Behavior. Accessing the array out of bounds is reaching into surrounding memory, and the content of that memory is random/indeterminate on each new run. It could be random bytes left over from a previous run/process. It could be bytes belonging to other local variables on the stack. You just don't know. myArray[2] = 7 myArray[4] = 9 myArray[7] = out of bounds, UB! myArray[8] = out of bounds, UB! myArray[9] = out of bounds, UB! Edited Friday at 10:51 PM by Remy Lebeau Share this post Link to post