Code:
void foo()
{
char x[3]="XY";
cout<<x<<endl;
y=x;
}
int main()
{
char * y;
foo();
cout<<y<<endl;
return 0;
}
Interesting thing is that y can be correctly printed, which means the allocated memory is not freed after foo();
But I use valgrind to check the code and it reports no error:
==1529== Memcheck, a memory error detector.
==1529== Copyright (C) 2002-2005, and GNU GPL'd, by Julian Seward et al.
==1529== Using LibVEX rev 1471, a library for dynamic binary translation.
==1529== Copyright (C) 2004-2005, and GNU GPL'd, by OpenWorks LLP.
==1529== Using valgrind-3.1.0-Debian, a dynamic binary instrumentation framework.
==1529== Copyright (C) 2000-2005, and GNU GPL'd, by Julian Seward et al.
==1529== For more details, rerun with: -v
==1529==
XY
XY
==1529==
==1529== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 17 from 1)
==1529== malloc/free: in use at exit: 0 bytes in 0 blocks.
==1529== malloc/free: 0 allocs, 0 frees, 0 bytes allocated.
==1529== For counts of detected errors, rerun with: -v
==1529== No malloc'd blocks -- no leaks are possible.