0

C++ question re: pointers/array/initialization

With: int sum; cout << sum; there is an init warning but the value of sum is correctly reported as 0. With: int sum; for (int i = 0; i < 5; i++) { sum += i; } cout << sum; the output 10 is correct, no warning - sum not init (seems to) work ok. With: int arr[] = {19, 24, 36, 45, 56}; int *p = arr, sum; for(int i = 0;i < 5; i++) { sum += *p; p++; } cout << sum; no warning but the output is incorrect. If sum is init as sum = 0 it's correct. Why the difference?

4th Feb 2024, 6:51 AM
Scott D
Scott D - avatar
6 Antworten
+ 3
Generally this is a big question to compiler. The last example show that memory may have values from other calculations. Therefore, in similar cases, it would be better to initiate the variables with a value beforehand.
4th Feb 2024, 8:03 AM
JaScript
JaScript - avatar
+ 2
JaScript Thanks for the reply. Bob_Li Yes, I'm aware that adding the cout line where you did would show the init error, hence my first "With:" example. The question pertains to why there is a difference with how for loops that do or do not not use pointer based values respond differently, in that only one causes calculation errors but gives no warning. So it would seem the issue is possibly (likely?) due to uninitialized memory and might possibly (?) be somewhat compiler specific. Regardless, yes, it certainly appears that as you say it's a good idea to initialize variables. Especially when using them in pointer/memory based situations. Unfortunately, this isn't covered in the course.
5th Feb 2024, 4:36 AM
Scott D
Scott D - avatar
+ 2
Case in point, the Dynamic Memory lesson, final Code Coach. After I wrote my solution I decided to toy with the code as I often do for experience. One idea was to sum an array inside the loop instead of just reporting a single array value, but that gave unexpected sum results. Seeing the output, a sum far too high, I used cout after the uninitialized sum variable and saw that was the problem and that initializing sum resolved it.That's why I wrote the question, to try and determine why this is an issue but only in some usage situations. So basically I'm trying to avoid similar errors in the future by gaining a better understanding of what caused it. The Dynamic Memory section isn't bad per se, but the examples within use limited output. For example, in the final Code Coach the max variable is initialized int max = nums[0]. Yet if it's changed to int max; it still passes. However using the same code but instead summing the pointer based array won't work if the sum variable is initialized without a value.
5th Feb 2024, 5:11 AM
Scott D
Scott D - avatar
+ 1
try this: this time the compiler issues a warning, and you see what is stored in sum initially. int arr[] = {19, 24, 36, 45, 56}; int *p = arr, sum; cout<<sum<<endl; for(int i = 0;i < 5; i++) { sum += *p; p++; } cout << sum; That's why unitialized variables should be avoided.
4th Feb 2024, 10:28 AM
Bob_Li
Bob_Li - avatar
+ 1
Scott D Undefined behavior is one of the things to be careful about in C and C++. It is unfortunate that Sololearn did not do a great job in warning the students about it's consequences. And you have a good point about how they might be handled differently across different compilers. Avoiding compiler specific behavior for code portability is probably the best option, but what those specifically are requires advanced knowledge and experience, so there's a bit of a bumpy learning curve in there...
5th Feb 2024, 8:28 AM
Bob_Li
Bob_Li - avatar
+ 1
Bob_Li I'm certainly not upset that in an introductory course this wasn't covered. Dealing with all potential errors seems beyond the scope of these self-learning lessons which is why I'm greatful for the discuss forum. I completed the larger "full" C++ course last year and hadn't used used much C++ recently so am going thru the new broken up versions as a refresher. I'm finding they do offer some things I don't recall being covered in the older course (or maybe I just don't recall them) such as this concept of initializing an empty variable sized array, dealing with dynamic memory. Anyways, thanks for the input, I'm sure I'll be back with more questions. I never truly got a handle on classes, objects, methods & "this" etc. (which is kicking my butt in js...).
5th Feb 2024, 9:08 AM
Scott D
Scott D - avatar