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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include "clog.h"

char* repeat(char char_to_repeat, int num_times_to_repeat) { // returns memory on HEAP
    int num_bytes_to_allocate = num_times_to_repeat + 1;  // include space for '\0'
    // NAMING TIP: ∙ Use ▒_bytes to include the number of bytes including '\0'.
    //             ∙ Use ▒_chars to include the number of characters that would be printed,
    //               excluding the '\0'
    //             ∙ Names like str_size can be ambiguous and lead to bugs.

    // Alocate a new string sufficient for 'num_times_to_repeat' chars and
    // assign the address of the first character to 'ch_repeated_str'.
    char* ch_repeated_str = malloc(num_bytes_to_allocate * sizeof(*ch_repeated_str));
    for(int i = 0; i < num_times_to_repeat; i++) {
        ch_repeated_str[i] = char_to_repeat;  // copy one character
    }
    ch_repeated_str[num_times_to_repeat] = '\0';  // Write null terminator at end of string

    return ch_repeated_str;
}

int main(int argc, char* argv[]) {
    char* s = repeat('*', 5);  // 1st call to malloc(…)
    log_str(s);
    free(s);
    // IMPORANT: Call free(…) on every block that was allocated by malloc(…).
    //  └─ Corollary: Number of calls to malloc(…) should equal number of calls to free(…).
    
    s = repeat('3', 3);  // 2nd call to malloc(…)  -- WARNING: not freed
    log_str(s);

    s = repeat('4', 4);  // 3rd call to malloc(…)  -- WARNING: not freed
    log_str(s);

    s = repeat('5', 5);  // 4th call to malloc(…)  -- WARNING: not freed
    log_str(s);

    // WARNING:  We are missing three calls to free(…).
    return EXIT_SUCCESS;
}

// Autocomplete:  Ctrl-P
/*
$ valgrind ./f.repeat
==33934== Memcheck, a memory error detector
==33934== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==33934== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==33934== Command: ./f.repeat
==33934==
s == "*****"
s == "333"
s == "4444"
s == "55555"
==33934==
==33934== HEAP SUMMARY:
==33934==     in use at exit: 15 bytes in 3 blocks
==33934==   total heap usage: 4 allocs, 1 frees, 21 bytes allocated
==33934==
==33934== 4 bytes in 1 blocks are definitely lost in loss record 1 of 3
==33934==    at 0x4C29F73: malloc (vg_replace_malloc.c:309)
==33934==    by 0x40063A: repeat (f.repeat.c:16)
==33934==    by 0x4006DE: main (f.repeat.c:32)
==33934==
==33934== 5 bytes in 1 blocks are definitely lost in loss record 2 of 3
==33934==    at 0x4C29F73: malloc (vg_replace_malloc.c:309)
==33934==    by 0x40063A: repeat (f.repeat.c:16)
==33934==    by 0x400716: main (f.repeat.c:35)
==33934==
==33934== 6 bytes in 1 blocks are definitely lost in loss record 3 of 3
==33934==    at 0x4C29F73: malloc (vg_replace_malloc.c:309)
==33934==    by 0x40063A: repeat (f.repeat.c:16)
==33934==    by 0x40074E: main (f.repeat.c:38)
==33934==
==33934== LEAK SUMMARY:
==33934==    definitely lost: 15 bytes in 3 blocks
==33934==    indirectly lost: 0 bytes in 0 blocks
==33934==      possibly lost: 0 bytes in 0 blocks
==33934==    still reachable: 0 bytes in 0 blocks
==33934==         suppressed: 0 bytes in 0 blocks
==33934==
==33934== For lists of detected and suppressed errors, rerun with: -s
==33934== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 0 from 0)

>>>>> DO NOT WORRY ABOUT "(suppressed: ▒ from ▒)"
 */
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */

© Copyright 2022 Alexander J. Quinn         This content is protected and may not be shared, uploaded, or distributed.