If you are getting trouble with the error “Conditional jump or move depends on uninitialised value(s)”, don’t worry and keep calm, then follow our article to know how to deal with it.
Reason for The ERROR “conditional jump or move depends on the uninitialized value(s)“
In C++, when you use a variable which has been initialised, you may get the result 0 or a number corresponding to the memory. In fact, this error is not reported by the compiler. It is detected by “Valgrind” tool.
Example:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
int arr[n+1];
int sum=0;
for(int i=1; i<=n; i++){
arr[i] = i;
sum += arr[i];
}
cout << sum << endl;
}
Then, run the following commands in your terminal :
g++ -g example.cpp
valgrind ./a.out
Result:
==20423== Memcheck, a memory error detector
==20423== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==20423== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==20423== Command: ./a.out
==20423==
==20423== Conditional jump or move depends on uninitialised value(s)
==20423== at 0x109259: main (example.cpp:8)
==20423==
==20423== Conditional jump or move depends on uninitialised value(s)
==20423== at 0x109285: main (example.cpp:8)
==20423==
==20423== Use of uninitialised value of size 8
==20423== at 0x109293: main (example.cpp:8)
==20423==
==20423== Conditional jump or move depends on uninitialised value(s)
==20423== at 0x1092BE: main (example.cpp:10)
==20423==
==20423== Use of uninitialised value of size 8
==20423== at 0x1092F2: main (example.cpp:14)
==20423==
==20423== Use of uninitialised value of size 8
==20423== at 0x48F4044: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28)
==20423==
0
==20423==
==20423== HEAP SUMMARY:
==20423== in use at exit: 0 bytes in 0 blocks
==20423== total heap usage: 2 allocs, 2 frees, 73,728 bytes allocated
==20423==
==20423== All heap blocks were freed -- no leaks are possible
==20423==
==20423== Use --track-origins=yes to see where uninitialised values come from
==20423== For lists of detected and suppressed errors, rerun with: -s
==20423== ERROR SUMMARY: 6 errors from 6 contexts (suppressed: 0 from 0)
Solutions to solve this problem
Remember that this error results from using a variable without an initial value. So, the solution for this error turns out to be simple. The only thing you have to do is find the variable and give it a value.
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
n = 22;
int arr[n+1];
int sum=0;
for(int i=1; i<=n; i++){
arr[i] = i;
sum += arr[i];
}
cout << sum << endl;
}
Result:
253
Summary
Conditional jump or move depends on uninitialised value(s) caused by using a variable that is forgotten to declare initial value is very common, which leads to unexpected results. Through our article, I hope you understand the error clearly and never forget to define variables.
Maybe you are interested:
- Print a vector in C++
- How To Split String By Space In C++
- Error: lvalue required as left operand of assignment
My name is Robert Collier. I graduated in IT at HUST university. My interest is learning programming languages; my strengths are Python, C, C++, and Machine Learning/Deep Learning/NLP. I will share all the knowledge I have through my articles. Hope you like them.
Name of the university: HUST
Major: IT
Programming Languages: Python, C, C++, Machine Learning/Deep Learning/NLP