Test case 12

Memory leakage case 2 - leak more (in a loop). Quite often, the buggy leaking code might only be leaking a small amount of memory, a few bytes, by itself. The problem is, what if this leaky function is called in a loop hundreds, or perhaps, thousands of times, during process execution? Now the leakage is significant, and unfortunately, not immediately apparent.

To emulate precisely this and more, we execute two test cases (for Option 12):

  • We allocate, and leak, a tiny amount of memory (32 bytes) but in a loop 100,000 times (so, yes, we end up leaking over 3 MB)
  • We allocate, and leak, a large amount of memory (1 MB) in a loop 12 times (so, we end up leaking 12 MB).

Here's the relevant code:

[...]/* test case 12 : memory leak ...

Get Hands-On System Programming with Linux now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.