memusage.1: EXAMPLES: remove doubled calculations

The same calculations are repeated in malloc() and printf() calls.
For better readability, do the calculations once.

Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
This commit is contained in:
Michael Kerrisk 2020-09-04 09:49:16 +02:00
parent 8d635e0a7e
commit 15fc4aab1f
1 changed files with 10 additions and 6 deletions

View File

@ -245,10 +245,12 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int i, j; int i, j;
size_t size;
int *p; int *p;
printf("malloc: %zd\en", sizeof(*p) * 100); size = sizeof(*p) * 100;
p = malloc(sizeof(*p) * 100); printf("malloc: %zd\en", size);
p = malloc(size);
for (i = 0; i < CYCLES; i++) { for (i = 0; i < CYCLES; i++) {
if (i < CYCLES / 2) if (i < CYCLES / 2)
@ -256,11 +258,13 @@ main(int argc, char *argv[])
else else
j--; j--;
printf("realloc: %zd\en", sizeof(*p) * (j * 50 + 110)); size = sizeof(*p) * (j * 50 + 110);
p = realloc(p, sizeof(*p) * (j * 50 + 100)); printf("realloc: %zd\en", size);
p = realloc(p, size);
printf("realloc: %zd\en", sizeof(*p) * ((j+1) * 150 + 110)); size = sizeof(*p) * ((j + 1) * 150 + 110);
p = realloc(p, sizeof(*p) * ((j + 1) * 150 + 110)); printf("realloc: %zd\en", size);
p = realloc(p, size);
} }
free(p); free(p);