2

I was writting some simple code in C to test some memory allocation and pointers:

#include <stdlib.h>
#include <stdio.h>


int *randomAlloc(int n) {
    int *address = NULL, i = 0;

    address = malloc (n * sizeof(int));
    for (i = 0; i < n ; i++){
        *(address + i) = i ;
    }
    return address;

}

int main(int argc, char* argv[] ) {

    int *address;
    int n;
    printf("Type vector size: ");
    scanf("%d", &n);
    address = randomAlloc(n);

    free(address);
}

Yet for some reason when I type 4 as input valgrind outputs:

==2375== Memcheck, a memory error detector
==2375== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==2375== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info
==2375== Command: ./a.out
==2375== 
Type vector size: 4
==2375== 
==2375== HEAP SUMMARY:
==2375==     in use at exit: 0 bytes in 0 blocks
==2375==   total heap usage: 3 allocs, 3 frees, 2,064 bytes allocated
==2375== 
==2375== All heap blocks were freed -- no leaks are possible
==2375== 
==2375== For counts of detected and suppressed errors, rerun with: -v
==2375== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

There is only one alloc and one free at the code. As n = 4, I'd expect it to alloc 4*4(sizeof(int))=16 bytes. Where is this comming from?

bla
  • 1,840
  • 1
  • 13
  • 17
gmelodie
  • 411
  • 4
  • 18
  • Type `1` or `2` ...it will still be the same. The only thing which it will change is `2,064 bytes allocated`. Now take a look and think, why. – Michi May 30 '17 at 21:17
  • I cannot reproduce the behavior you describe. When I build your code and run it under Valgrind, it reports one allocation and one free, just as you expect. – John Bollinger May 30 '17 at 21:18
  • @JohnBollinger Not on my system (linux mint). if I type `2` I have: `total heap usage: 3 allocs, 3 frees, 2,056 bytes allocated` if I type `3` I have: `total heap usage: 3 allocs, 3 frees, 2,060 bytes allocated` ..... and so on. `2,056` => `2,060` => ` 2064` – Michi May 30 '17 at 21:20
  • What @duskwuff explained is that if you remove the `scanf()` part you will have only two allocs instead of three. Or better remove `printf()` too. Try the following [Program](http://ideone.com/TycpTO) and you will see the difference like [Here](https://pastebin.com/raw/xZJ8MSri) – Michi May 30 '17 at 21:55

2 Answers2

5

Valgrind keeps track of all memory allocations which occur in your application, including ones made internally by the C library. It is not (and cannot) be limited to allocations you make explicitly, as the C library can return pointers to memory which it has allocated internally.

Many standard I/O implementations will allocate buffers for use by printf() and/or scanf(), which is probably what accounts for the numbers you're seeing.

0

you should only have 1 memory alloc for the 'address' pointer's memory space. the other 2 memory allocs are for the printf and scanf functions.

to proof this, comment out the printf and scanf statements and you should see 1 alloc and 1 free when you use valgrind to execute the program...

hussein
  • 11
  • 2
  • That answer was already given, upvoted and accepted 5 years ago. Please, when answering to old questions, be sure to provide a new information that was not given at the time. – chrslg Nov 27 '22 at 17:35