C++

ERROR: conditional jump or move depends on the uninitialized value(s)

If you are preparing for the exam in C++, most of you will use Valgrind to analyze your contributions. Valgrind is a storage-related error-checking tool. A widespread myth is that Valgrind solely identifies memory leaks and, therefore, you should not receive Valgrind warnings when you are not using new in your code. Valgrind may discover a variety of memory issues besides leaks, like unsigned memory usage or accesses. Furthermore, when you’re not using new, you can leak storage if the code ends incorrectly and would be unable to clear up heap objects. Undetected errors or the usage of the quit method (that will not be castoff in C++) might lead to an inappropriate exit. Error messages from Valgrind may be lengthy and daunting. This post will show you how and where to manage the “Conditional jump or move depends on uninitialized value(s)” issue in Ubuntu 20.04 while working in C++.

Install Valgrind in Ubuntu 20.04:

The Valgrind package can highlight a warning or error on the shell while it may not be showing up with the C++ compiler on the Ubuntu terminal. Therefore, we need to install it first. So, you need to open the terminal of Ubuntu 20.04 with the “Ctrl+Alt+T” shortcut key. The Ubuntu’s apt package command is utilized for this purpose so far in the shell, as shown below.

$ sudo apt install valgrind

The error code can be seen as “Conditional jump or move depends on the uninitialized value(s),” which indicates Valgrind has discovered that the program’s outcome is dependent on unsigned storage. The notice “Use of an uninitialized value of size N” may also appear occasionally. The point over which the code relies on the unsigned variable will be reported by Valgrind. This will not indicate an error if uninitialized values are relocated and cloned around in storage as far as the application does not rely on them. It is difficult to locate the fault because it might be far distant from the page Valgrind reports. You can get help with Valgrind commands using the help command and the keyword “Valgrind” to use them on your shell. The output for the help command will open up Ubuntu’s manual for Valgrind, as you can already see in the attached photo below. In your Valgrind commands, utilize any of these arguments.

$ valgrind –-help

Example 01:

So, we will be looking at the maximum possible examples which can cause the Valgrind to show this error or warning on the terminal while utilizing the Valgrind command. So, we have created a file for C++ code using the Linux “touch” instruction. This newly created file has been located in the home folder and opened within the Nano editor to do code.

$ touch err.cc
$ nano err.cc

For instance, we have been starting the code with iostream and the namespace of C++. These are necessary to make one code work. Within a main() function, we have simply declared an integer variable, and on the next line, we have incremented the variable with 3 without initializing it. The cout statement is utilized to display the increment variable “x” value.

#include<iostream>
using namespace std;
int main() {
int x;
   x += 3; // error
   cout << x << endl;
}

There is no error found so far in some situations like below on executing this code with a simple G++ compiler of C++ language. It will also display the incremented value as shown.

$ g++ err.cc
$ ./a.out

On the other hand, Valgrind permits us to increase or allocate x+3 to “x” since the program’s apparent performance is not altered till you try to print the vector’s contents. Valgrind will only show you the line number on which the conditional jump error has been found so far and make you understand that you are using an unsigned variable somewhere. However, determining which uninitialized variable or why it was not initialized may require significant forensic work.

$ valgrindtool=memcheck –leak-check=yes ./err.cc

Example 02:

Let’s take a look at another program with a different way to get the same error. We have included the standard “std” namespace for C++, and the iostream via the “#include” within this program. We have commented on the main() method declaration and declared an integer variable “x.” The “while” loop is used to check a condition and perform the decrement.

#include
using namespace std;
int main() {
   int x;
  while (x > 4) { // error
  x--;
} //}

It will not only make Valgrind cause the error “Conditional jump or move depends on uninitialized value” but will also cause an error with G++, as shown below. To resolve this issue, you need to uncomment the commented area of code and run it.

$ g++ err.cc

Conclusion:

This was all about the Valgrind error: “Conditional jump or move depends on uninitialized value” in C++ code. We have maintained the sequence by installing the Valgrind, opening its manual for help, creating a C++ code, and executing it with G++ and Valgrind on the shell to understand it all better and resolve it. We have added two unique illustrations for a better understanding of this error. All these examples are implemented on Ubuntu 20.04 system. However, you can utilize any other distribution as well. We believe that it will be really beneficial.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.