UC3M

Telematic/Audiovisual Syst./Communication Syst. Engineering

Systems Architecture

September 2017 - January 2018

17.5.5.  Final auto-evaluation questions

When executing the following code using Valgrind, you get the following errors. Answer the questions looking at the errors and the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
    int main()
    {
      char *p;

      // Allocation #1 of 19 bytes
      p = (char *) malloc(13);

      // Allocation #2 of 12 bytes
      p = (char *) malloc(11);
      free(p);

      // Allocation #3 of 16 bytes
      p = (char *) malloc(16);

      return 0;
    }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
==7279== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==7295== Memcheck, a memory error detector
==7295== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==7295== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==7295== Command: ./a
==7295== 
==7295== 
==7295== HEAP SUMMARY:
==7295==     in use at exit: 29 bytes in 2 blocks
==7295==   total heap usage: 3 allocs, 1 frees, 40 bytes allocated
==7295== 
==7295== 13 bytes in 1 blocks are definitely lost in loss record 1 of 2
==7295==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==7295==    by 0x8048428: main (in /home/gradient/Escritorio/ValgrindErrors/a)
==7295== 
==7295== 16 bytes in 1 blocks are definitely lost in loss record 2 of 2
==7295==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==7295==    by 0x8048454: main (in /home/gradient/Escritorio/ValgrindErrors/a)
==7295== 
==7295== LEAK SUMMARY:
==7295==    definitely lost: 29 bytes in 2 blocks
==7295==    indirectly lost: 0 bytes in 0 blocks
==7295==      possibly lost: 0 bytes in 0 blocks
==7295==    still reachable: 0 bytes in 0 blocks
==7295==         suppressed: 0 bytes in 0 blocks
==7295== 
==7295== For counts of detected and suppressed errors, rerun with: -v
==7295== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)

  1. Look at the error Allocation #1 (13 byte leak) and answer.

    • Allocation #1 (13 byte leak) is lost because p is pointed elsewhere before the memory from Allocation #1 is free'd.

    • Allocation #1 (13 byte leak) is lost because the way in which the memory for p is allocated is not correct.

    • Allocation #1 (13 byte leak) is lost, it is the Allocation #2 the one that is lost.

  2. Look at the error Allocation #3 (16 byte leak) and answer.

    • Allocation #3 (16 byte leak) lists two memory leaks because it has detected two new memory leak errors.

    • Allocation #3 (16 byte leak) lists two memory leaks because it refers also to the previous leak.

    • Allocation #3 (16 byte leak) lists two memory leaks because it counts also as an memory leak of the Allocation #2.

  3. Look at the following code. What is the error that will result if we compile with Valgrind?

    1
    2
    3
    4
    5
    6
    #include <stdio.h>
              int main()
              {
              int x;
              printf ("x = %d\n", x);
              }

    • Conditional jump or move depends on uninitialised value(s)

    • Invalid free()

    • Syscall param write(buf) contains uninitialised or unaddressable byte(s)