Use valgrind and find memory errors

he segmentation fault is the most frequently occurred runtime error in the C/C++ program compiled using gcc/g++. I have seen that students use TurboC++ compiler because it doesn’t show the error like “segmentation fault”. Today’s engineering students does not take any efforts to analyze why such kinds of problems occurs?

A program use memory space allocated to it which uses the stack and heap area. The memory is allocated here using runtime. For this we use the malloc function or new operator. Now, when the following situations occur, the program will show the “segmentation fault”.
– Memory is not released using delete/free.
– Using the array index which is not in the specified range.
– The uninitialized pointer is referenced in the program.
– Read only memory is attempted to be used for writing.
– Already freed pointer is dereferenced.
In order to find the reason of such different kinds of memory problem, we may use the valgrind which is a memory checker utility provided by the Linux system. It is used along with GDB. It is not possible for GDB find the memory errors in one stroke, so valgrind can be used. It is useful in many scenarios of C/C++ programming. Few of these are discussed in this blog post. Valgrind is used to notify the user with all the errors as given above. Generally, memory leakage problems can be easily detected by the valgrind. As the gcc is very robust compiler, the system tool is very much useful in the programming.
In order to download the valgrind latest version, use the following command on debian based Linux systems such as Ubuntu/Mint.
sudo apt-get install valgrind
or use following the install it on Redhat based Linux.
sudo yum install valgrind
As C/C++ don’t have any automatic garbage collector, valgrind can be used the identify the garbages in the program. Lets write a simple program in C++.
using namespace std;
int main()
     int *x;
     x = new int(10);
     return 0;
This program is creating an array of 10 numbers using pointers. So, the memory of 10 locations is allocated to variable x using new operator. Now, compile the program by enabling the debugger to it using following command.
g++ -g prog.cpp -o prog
It will create the executable file named prog. Now use valgrind to check for the memory problems.
valgrind –tool=memcheck –leak-check=yes ./prog
This uses the tool “memcheck” by enabling the checking of the memory leakage ability.
==3550== Memcheck, a memory error detector
==3550== Copyright (C) 2002-2011, and GNU GPL’d, by Julian Seward et al.
==3550== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==3550== Command: ./prog
==3550== HEAP SUMMARY:
==3550== in use at exit: 4 bytes in 1 blocks
==3550== total heap usage: 1 allocs, 0 frees, 4 bytes allocated
==3550== 4 bytes in 1 blocks are definitely lost in loss record 1 of 1
==3550== at 0x402B733: operator new(unsigned int) (in /usr/lib/valgrind/vgpreload _memcheck
==3550== by 0x80485B0: main (prog.cpp:6)
==3550== LEAK SUMMARY:
==3550== definitely lost: 4 bytes in 1 blocks
==3550== indirectly lost: 0 bytes in 0 blocks
==3550== possibly lost: 0 bytes in 0 blocks
==3550== still reachable: 0 bytes in 0 blocks
==3550== suppressed: 0 bytes in 0 blocks
==3550== For counts of detected and suppressed errors, rerun with: -v
==3550== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Here, 3550 is the process ID of your program. It has shown that the program has definitely lost 4 bytes in 1 block. This is shown because that the memory allocated is not freed. After adding ‘delete x’ statement at the end of the program, we will get this message using valgrind.
All heap blocks were freed — no leaks are possible
Lets use the variable x[15] from the given array. It is not possible to use this variable from the array! I will write the following statement in the program.
x[15] = 34;
Now, valgrind shows the following statements:
==3608== Invalid write of size 4
==3608== at 0x80485C2: main (prog.cpp:7)
==3608== Address 0x4330064 is not stack’d, malloc’d or (recently) free’d
It means the line number 7 of prog.cpp has “invalid write of size 4 bytes”! We will easily identify that the memory is used is some wrong way in the program. Remember it is not a syntactical mistake!
Now when we use the uninitialized data in the program such as,
if (x[1]==1)
    x[1] = 0;
As the array is not initialized, the x[1] won’t contain any value in it. When you compile the program and analyze it by valgrind, you will get the following output.
==3661== Invalid read of size 4
==3661== at 0x80485C2: main (prog.cpp:7)
==3661== Address 0x433002c is 0 bytes after a block of size 4 alloc’d
==3661== at 0x402B733: operator new(unsigned int) (in /usr/lib/valgrind/
==3661== by 0x80485B0: main (prog.cpp:6)
This is the error for uninitialized data in the program. You will identify that the statement on line number 6 has some uninitialized data variable in it.
I think using valgrind improves the quality of software development. A software developer must use it in order to solve the memory leakage problems. It has many features, you may use ‘man valgrind’ to see all these features of it.
Enjoy good programming!

Basic aim of this…

Imagination power of human being is very high. I salute all those scientists, who are the originators of various concepts in this universe. To create is the act of imagination. We know that Charles Babbage is very well known as father of computer. He had given birth to world’s first computer. Though, the computer born that time was a mechanical computer, it was the start of a new era of human being. If we see in recent years this computing technology has reached higher and higher with the imagination power of human being. Every machine needs some force to drive it. As computer is also a machine, it also requires something to drive it. That’s what an operating system is! In initial stage of computers they were evolved from mechanical to electromechanical then from analog electronics to digital electronics. Today our computer lives in strong world of digital electronics. But it was the requirement of generations of computer to evolve from such stages. At every stage, computer required certain force to drive it, to make it useful to human kind, to make the tasks simpler. The changing forces of computer had given birth to the generations of computer operating system, which is software today that drives the computer system. Several complicated process and algorithms are involved in the generations of the computer operating systems. As it is said that need gives birth to the inventions. The same is applicable to operating system also. So, today operating systems of the computer are approaching with various novel technologies removing more percentage of bugs from computer systems. When computers were in the generation of vacuum tubes, they needed the electronic circuits to drive it. Almost for two decades this process was continued. When transistors were introduced to the computing world, novel concept of batch system was used to drive the computers to achieve the final aim. In computing world, the new technologies are introduced to achieve the efficient output with faster speed. As hardware technology changes, it also forces to change the software technology. Batch system was the necessity of computing world when they entered in faster world of transistors from vacuum tubes. Third generation gave birth to the multiprogramming concept. This was the first time, computers were used to process more than one tasks at a time. If we see today, every system is multitasking system. Motivational concepts in these generations were used by many of the computer developers, researchers and scientists. World’s first successful operating system UNIX was born in this third generation of computer systems. Still today, it has the same significance that it was having that time. Many companies and research institutes such as AT&T Bell, IBM has great contribution to develop the force to drive the computer systems. UNIX is the operating system which has motivated many of computer researchers to work in this field and develop new concepts. We are in forth generations of computer as well as its operating system. The force now drives the personal computers today. We are watching the competition between the operating system which are open source and which are not. But, as the engineer is considered, he always chooses the operating system which is open source. So he can study, learn and accordingly design newer concepts in this field. Hope, this blog will be much useful for those who work in this field. Or this can give you certain motivation to work on newer operating systems.


This is operating system blog….

Hello world!

Welcome to After you read this, you should delete and write your own post, with a new title above. Or hit Add New on the left (of the admin dashboard) to start a fresh post.

Here are some suggestions for your first post.

  1. You can find new ideas for what to blog about by reading the Daily Post.
  2. Add PressThis to your browser. It creates a new blog post for you about any interesting  page you read on the web.
  3. Make some changes to this page, and then hit preview on the right. You can alway preview any post or edit you before you share it to the world.