160 likes | 299 Vues
Memory-related errors can significantly impact program functionality, leading to issues like memory leaks, access violations, and uninitialized variable usage. This introduction elaborates on common memory errors and highlights essential memory checking tools such as Valgrind and Rational Purify. These tools assist programmers in identifying and resolving memory issues swiftly, thereby reducing hours of debugging. Examples demonstrate how to utilize Valgrind to detect memory leaks and access errors, showcasing its effectiveness. Understanding these tools is crucial for software reliability and performance.
E N D
Object oriented analysis and design Memory Checking tools Itay levy
Introduction • While a programmer is writing a code for an application that he creates, he can sometimes make an error that will cause a malefaction in his program. • Syntax error • Memory access errors • Memory leaks • Logical errors • Etc..
Memory errors • Memory access errors and memory leaks are caused by various reasons like: • Using an unallocated variable • Double freeing • Not freeing at all • Etc..
Memory errors(1) • The programmer will not receive any error massages from the compiler if he makes a memory access error or memory leak , he will have to waste a lot of time on debugging.
The memory checking tools • The memory checking tools are a memory related errors detection tools that will help the programmer to find and fix the memory related error in a short time , Instead of wasting hours of debugging in order to find and fix the problems.
The memory checking tools • Existing tools are : • Valgrind • BoundsChecker • Rational purify • And many more
Example - ValGrind Here's an example C program with a memory error and a memory leak. #include <stdlib.h> void f(void) { int* x = malloc(10 * sizeof(int)); x[10] = 0; } int main(void) { f(); return 0; } // problem 1: heap block overrun // problem 2: memory leak -- x not freed
Example – Valgrind (1) • First compile the program. • Then use the Command: valgrind --leak-check=yes myprog arg1 arg2 in order to run the valgrind on your program.
Example – valgrind(2) • The error massages of our program will be: ==19182== Invalid write of size 4 ==19182== at 0x804838F: f (example.c:6) ==19182== by 0x80483AB: main (example.c:11) ==19182== Address 0x1BA45050 is 0 bytes after a block of size 40 alloc'd ==19182== at 0x1B8FF5CD: malloc (vg_replace_malloc.c:130) ==19182== by 0x8048385: f (example.c:5) ==19182== by 0x80483AB: main (example.c:11) • The first line tells us what kind of error we got. • Below the first line is a stack trace telling you where the problem occurred • Some error messages have a second component which describes the memory address involved.
Example – valgrind(3) • Memory leak messages look like this: ==19182== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1 ==19182== at 0x1B8FF5CD: malloc (vg_replace_malloc.c:130) ==19182== by 0x8048385: f (a.c:5) ==19182== by 0x80483AB: main (a.c:11) • The stack trace tells you where the leaked memory was allocated. • He will not tell you why the memory was leaked.
A few things about valgrind • Valgrind works only on linux/unix. • Valgrind Can also be used as a cache profiler as well. • Valgrind can also be used to find data races in multithreaded programs.
Example – Rational Purify Lets run the Rational purify on the next HelloWorld program: #include <stdio.h> #Include <malloc.h> Static char *HelloWorld = “Hello, World”; Void main() { char *mystr = malloc(strlen(HelloWorld)); strncpy(mystr, HelloWorld,12); printf(“%s\n”,mystr); }
Example – Rational Purify (1) • Compile and link the program under Purify using the -g option to collect debug data:purify cc -g <myprog>.o • Then Run the program : % a.out