M
MN
Hi there,
I've done a small test for memory allocation/free and I can't
understand why I'm getting only 50 % freed.
In the next code the value of "max" and "s" can be changed during
runtime.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const int max = 15;
const int s = 2;
int* table(int max)
{
int i = 0;
int* table = NULL;
if (table != NULL)
free(table);
table = malloc (sizeof(int*)* max);
for (i = 0; i < max; i++)
{
if (i == 0)
table = 0;
else
{
if (i == 1)
table= 1;
else
table = table[i-1]*2;
}
}
return (table);
}
char** test_foo(int max, int s)
{
int i = 0;
char** test = NULL;
test = (char**)malloc(sizeof(char**) * max);
for (i = 0; i< max; ++i)
test = (char*)malloc(sizeof(char*) * s+1);
for (i = 0; i< max; ++i)
strcpy((char*)&test[0], "22");
return (test);
}
int main ()
{
int* table_tmp = NULL;
char** test_tmp = NULL;
int j = 0;
if (table_tmp != NULL)
free(table_tmp);
table_tmp = malloc (sizeof (int*) * max);
table_tmp = table (max);
for (j = 0; j < max; ++j)
{
printf("table_tmp [%d] = %d\n", j, table_tmp[j]);
}
free (table_tmp);
printf("\n");
test_tmp = malloc (sizeof (char**) * max);
for(j = 0; j < max; ++j)
test_tmp[j] = malloc (sizeof (char*) * s+1);
test_tmp = test_foo(max, s);
for (j = 0; j < max; ++j)
{
printf("test_tmp [%d] = %s\n", j, (char*)&test_tmp[j][0]);
}
for (j = 0; j < max; ++j)
{
free (test_tmp[j]);
}
free (test_tmp);
return 0;
}
I'm using valgrind tool to detect memory leak, like this:
valgrind --tool=memcheck --leak-check=full --leak-resolution=high --
num-callers=20 ./free_variable&> dump; gedit dump
as result:
==28879== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 5 from
1)
==28879== malloc/free: in use at exit: 495 bytes in 17 blocks.
==28879== malloc/free: 34 allocs, 17 frees, 990 bytes allocated.
==28879== For counts of detected errors, rerun with: -v
==28879== searching for pointers to 17 not-freed blocks.
==28879== checked 65,528 bytes.
==28879==
==28879== 120 bytes in 1 blocks are definitely lost in loss record 1
of 3
==28879== at 0x4A05809: malloc (vg_replace_malloc.c:149)
==28879== by 0x40071F: main (test_free_variable.c:60)
==28879==
==28879==
==28879== 375 (120 direct, 255 indirect) bytes in 1 blocks are
definitely lost in loss record 2 of 3
==28879== at 0x4A05809: malloc (vg_replace_malloc.c:149)
==28879== by 0x400793: main (test_free_variable.c:71)
==28879==
==28879== LEAK SUMMARY:
==28879== definitely lost: 240 bytes in 2 blocks.
==28879== indirectly lost: 255 bytes in 15 blocks.
==28879== possibly lost: 0 bytes in 0 blocks.
==28879== still reachable: 0 bytes in 0 blocks.
==28879== suppressed: 0 bytes in 0 blocks.
==28879== Reachable blocks (those to which a pointer was found) are
not shown.
==28879== To see them, rerun with: --show-reachable=yes
I've done a small test for memory allocation/free and I can't
understand why I'm getting only 50 % freed.
In the next code the value of "max" and "s" can be changed during
runtime.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const int max = 15;
const int s = 2;
int* table(int max)
{
int i = 0;
int* table = NULL;
if (table != NULL)
free(table);
table = malloc (sizeof(int*)* max);
for (i = 0; i < max; i++)
{
if (i == 0)
table = 0;
else
{
if (i == 1)
table= 1;
else
table = table[i-1]*2;
}
}
return (table);
}
char** test_foo(int max, int s)
{
int i = 0;
char** test = NULL;
test = (char**)malloc(sizeof(char**) * max);
for (i = 0; i< max; ++i)
test = (char*)malloc(sizeof(char*) * s+1);
for (i = 0; i< max; ++i)
strcpy((char*)&test[0], "22");
return (test);
}
int main ()
{
int* table_tmp = NULL;
char** test_tmp = NULL;
int j = 0;
if (table_tmp != NULL)
free(table_tmp);
table_tmp = malloc (sizeof (int*) * max);
table_tmp = table (max);
for (j = 0; j < max; ++j)
{
printf("table_tmp [%d] = %d\n", j, table_tmp[j]);
}
free (table_tmp);
printf("\n");
test_tmp = malloc (sizeof (char**) * max);
for(j = 0; j < max; ++j)
test_tmp[j] = malloc (sizeof (char*) * s+1);
test_tmp = test_foo(max, s);
for (j = 0; j < max; ++j)
{
printf("test_tmp [%d] = %s\n", j, (char*)&test_tmp[j][0]);
}
for (j = 0; j < max; ++j)
{
free (test_tmp[j]);
}
free (test_tmp);
return 0;
}
I'm using valgrind tool to detect memory leak, like this:
valgrind --tool=memcheck --leak-check=full --leak-resolution=high --
num-callers=20 ./free_variable&> dump; gedit dump
as result:
==28879== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 5 from
1)
==28879== malloc/free: in use at exit: 495 bytes in 17 blocks.
==28879== malloc/free: 34 allocs, 17 frees, 990 bytes allocated.
==28879== For counts of detected errors, rerun with: -v
==28879== searching for pointers to 17 not-freed blocks.
==28879== checked 65,528 bytes.
==28879==
==28879== 120 bytes in 1 blocks are definitely lost in loss record 1
of 3
==28879== at 0x4A05809: malloc (vg_replace_malloc.c:149)
==28879== by 0x40071F: main (test_free_variable.c:60)
==28879==
==28879==
==28879== 375 (120 direct, 255 indirect) bytes in 1 blocks are
definitely lost in loss record 2 of 3
==28879== at 0x4A05809: malloc (vg_replace_malloc.c:149)
==28879== by 0x400793: main (test_free_variable.c:71)
==28879==
==28879== LEAK SUMMARY:
==28879== definitely lost: 240 bytes in 2 blocks.
==28879== indirectly lost: 255 bytes in 15 blocks.
==28879== possibly lost: 0 bytes in 0 blocks.
==28879== still reachable: 0 bytes in 0 blocks.
==28879== suppressed: 0 bytes in 0 blocks.
==28879== Reachable blocks (those to which a pointer was found) are
not shown.
==28879== To see them, rerun with: --show-reachable=yes