simple c program. How does the memory size calculated in Valgrind

jian he <[email protected]>
Newsgroups gmane.comp.debugging.valgrind
Message-ID <CACJufxEn4OzDzaSn2jAMz01GZOLR4n8cPQhcC-hNinm6e=UqFA@mail.gmail.com>
helloc$valgrind ./a.out
==123254== Memcheck, a memory error detector
==123254== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==123254== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright
info
==123254== Command: ./a.out
==123254==
enter string (EOF) to quit): test1
enter string (EOF) to quit): test2
enter string (EOF) to quit): (all done)

test1
test2
==123254==
==123254== HEAP SUMMARY:
==123254==     in use at exit: 0 bytes in 0 blocks
==123254==   total heap usage: 6 allocs, 6 frees, 2,084 bytes allocated
==123254==
==123254== All heap blocks were freed -- no leaks are possible
==123254==
==123254== For lists of detected and suppressed errors, rerun with: -s
==123254== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
-----------------------------------------------------------------------------------------------------------------
simple c program source code:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAXC 1024
int main(void){
    char buf[MAXC],**arr = NULL;
    size_t nstr = 0;    /* counter for number of string stored */
    for(;;){
        size_t len;     /* var to hold length of string after n removeal */
        fputs("enter string (EOF) to quit): ",stdout);
        if(!fgets(buf,MAXC,stdin)){
            puts("(all done)\n");
            break;
        }
        buf[len = strcspn(buf,"\r\n")] = 0;
    /*always realloc using temp pointer to avoid mem-leak on reallco
failure*/
    void *tmp = realloc(arr,(nstr+1) * sizeof *arr);
    if(!tmp){
        perror("realloc-tmp");
        break;
    }
    arr = tmp;
    if(!(arr[nstr] = malloc(len + 1))){
        perror("malloc-arr[str]");
        break;
    }
    memcpy(arr[nstr++], buf,len + 1);
}
    for(size_t i = 0; i < nstr; i++){
        puts(arr[i]);
        free(arr[i]);
    }
    free(arr);
    return 0;
}
---------------------------------------------

New to  C,   I am not sure the following:
total heap usage: 6 allocs, 6 frees, 2,084 bytes allocated

I guess 6 allocs is 3 times mallocs called plus 3 times puts function
called?
But I don't know where 2084 comes from.


-- 
 I recommend David Deutsch's <<The Beginning of Infinity>>

  Jian

_______________________________________________
Valgrind-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/valgrind-users
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.