Fixing Valgrind Errors

Question

1. Run the first test:

This program is supposed to be adding up 7 random numbers between 0 and 99 and print out the sum. However, now let’s see if there are any hidden memory issues. Let’s run Valgrind:

You should see 2 errors (an Invalid Read and a block of memory that is definitely lost). An invalid read means you are trying to access a piece of memory that is not allocated to you. Usually, it’s because you have a bad pointer or access things beyond the end of an array.

Use the line numbers in the error messages as clues and try to fix the code (Google the error message if you are having trouble understanding it). Recompile memleak.cpp and rerun Valgrind until you have 0 errors.

2. Run the 2nd test:

This program should ask you for a word, then print it, and print it in reverse. You may see a spew of text as the program completes. Scroll to the top and you’ll see a message like:

This is indicative of dynamic memory problems. Let’s run Valgrind

You should see 3 errors. Try to read the messages (and line numbers) carefully to understand what it is saying. Some errors may be due to the same root cause. So as you fix one, re-run to see if it helps.

Recompile and rerun Valgrind until you have 0 errors.

Help me find the Error in this code according to the above image. Can you please specify what is the problem and where is it?

Summary

In this question, we have to fix the Valgrind errors. We have defined three cases here according to the command-line argument. It says that if the user enters the argument as 1, then it simply takes 7 random numbers that are below 100 puts them in an array, and then prints the sum of them. At last, it outputs them to the console. And if the user enters an argument as 2, then it takes a string from the user, and then prints the string, after printing the string it prints the reverse of that string too. If the user enters an argument that is greater than 1 as well as 2, then it prints a prompt that says that an argument is an unknown number.

Explanation

In this question, we have two types of Valgrind errors. The first error is because of memory that is a memory leak.

If we allocate the memory dynamically in a program, and if we do not free the memory after usage, then it can cause a leak. But, this is not an urgent situation in which someone reacts.

In this code, in the function test2_reverse, we have created a character array with the name otherword, for which we have allocated the memory dynamically, also, we have allocated the memory dynamically to the parameter it passes. Once we copy the reverse of the word to otherword, the word has no significance later, so, it frees the memory of the word.

Also, in the main method, we have created two-character pointers for which we allocate the memory dynamically. But, in the end, we deallocate the allocated memory, and for deallocating the memory we use the delete keyword.

On the other hand, there is a second error, in which if we access some data, and that data is not actually initialized, or reading freed data, it can cause a memory error. This is the second Valgrind error. Here are not present both types of errors.

Code

#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
// sum an array of numbers
int test1(int *data, int len)
{
 int sum = 0;
 for(int i=0; i <= len; i++){
   sum += data[i];
 }
 return sum;
}
// Allocate a random number array
char* test2_init(int len)
{
 char buf[80];
 cout << "Enter a word: ";
 cin >> buf;
 char* mydat = new char[strlen(buf)+1];
 strcpy(mydat, buf);
 return mydat;
}
char* test2_reverse(char* word)
{
 int len = strlen(word);
 char* otherword = new char[len+1];
 
 for(int i=0; i < strlen(word); i++){
   otherword[i] = word[len-i-1];
 }
 otherword[len+1] = '\0';
 delete [] word;
 return otherword;
}
int main(int argc, char* argv[])
{
 if(argc < 2){
   cerr << "Please enter the test number you want to run [1-2]" << endl;
   return 1;
 }
 const int len = 7;
 int test = atoi(argv[1]);
 if(test == 1){
   // Test should sum up the array values and print it
   int *data = new int[len];
   for(int i=0; i < len; i++){
     data[i] = rand()%100;
   }
   int sum = test1(data, len);
   cout << "Test 1 sum is " << sum << endl;
 }
 else if(test == 2){
   // Test should create a random string & then reverse a copy
   char* myword = test2_init(len);
   cout << "myword is " << myword << endl;

   char* otherword = test2_reverse(myword);    
   cout << "otherword is " << otherword << endl;
  
   delete [] myword;
   delete [] otherword;
 }
 else {
   cout << "Unknown test number" << endl;
 }
 return 0;
}

Output

Fixing Valgrind Errors o/p1

output1

Fixing Valgrind Errors o/p 2

output2

 

Also read, Must use header file, implementation file, the main program

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *