Free memory

struct testB;
struct testA
{
int digit;
testB *C;
};
testA A;

struct testB
{
int digit2;
testA *D;
};
testB B;
/////////////////////////////
int main()
{
A.C = &B;
B.D = &A;
here goes more code
}
I don't know how to free memory of testB *C and testA *D pointers,
or maybe i'm doing something wrong. Valgrind shows posible memory leaks of this code.
Are you sure? Running valgrind on that gives me "All heap blocks were freed -- no leaks are possible"
There aren't any memory leaks there, at least I don't see any.
How can you leak memory without dynamic allocation?
here goes all code.

#include <iostream>
using namespace std;

struct testB;
struct testA
{
int digit;
testB *C;
};
testA A;

struct testB
{
int digit2;
testA *D;
};
testB B;

int main()
{
A.C = &B;
B.D = &A;

cout << " 2 structs test" << endl;
cout << "Enter digit A and digit B " << endl;
cin >> A.digit >> B.digit2;
cout << endl;
cout << "A.digit : " << A.digit << " \t B.digit2 : " << B.digit2 << endl;
cout << "#######################################" << endl;
cout << endl;
cout << " through struct B " << endl;
cout << "B.D->dig : " << B.D->digit << " \t B.dig2 : " << B.digit2 << endl;
cout << "#######################################" << endl;
cout << endl;
cout << " through struct A " << endl;
cout << "A.dig : " << A.digit << " \t A.C->dig2 : " << A.C->digit2 << endl;
return 0;
}
1,856 bytes in 1 blocks are still reachable in loss record 1 of 1
==3929== at 0x402425F: calloc (vg_replace_malloc.c:467)
==3929== by 0x424B2EB: monstartup (gmon.c:136)
==3929== by 0x80487F5: __gmon_start__ (in /home/indian/Projects/C++/LD9.4/bin/Debug/LD9)
==3929== by 0x4023C01: ??? (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==3929== by 0x400DBBB: call_init (dl-init.c:70)
==3929== by 0x400DD80: _dl_init (dl-init.c:100)
==3929== by 0x400088E: ??? (in /lib/ld-2.11.1.so)
==3929==
==3929== LEAK SUMMARY:
==3929== definitely lost: 0 bytes in 0 blocks
==3929== indirectly lost: 0 bytes in 0 blocks
==3929== possibly lost: 0 bytes in 0 blocks
==3929== still reachable: 1,856 bytes in 1 blocks // still reachable not released memory
==3929== suppressed: 0 bytes in 0 blocks

So, not all memory are released. it's posible memory leak or no ??
Last edited on
There is definitely no problem with the code, see the Valgrind FAQ:

"still reachable" means your program is probably ok -- it didn't free some memory it could have. This is quite common and often reasonable. Don't use --show-reachable=yes if you don't want to see these reports.
Thank you for your help. So I don't have worry about it. Thanks a lot.
Topic archived. No new replies allowed.