1 / 15

Object oriented analysis and design

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

kirkan
Download Presentation

Object oriented analysis and design

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object oriented analysis and design Memory Checking tools Itay levy

  2. 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..

  3. 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..

  4. 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.

  5. 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.

  6. The memory checking tools • Existing tools are : • Valgrind • BoundsChecker • Rational purify • And many more

  7. 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

  8. 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.

  9. 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.

  10. 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.

  11. 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.

  12. 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); }

  13. 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

  14. Example – Rational Purify(2)

  15. Example – Rational purify(3)

More Related