M
Mug
hi all;
i have a little question on the behaviors of free and delete.(C and C+
+)
here's a little program: test.cc
#include <stdlib.h>
int main(int argc, const char *argv[])
{
char *tmp1 = (char*) malloc(10);
char * tmp2 = new char [10];
free(tmp2);
delete(tmp1);
return 0;
}
i always have impression that free is equivalent to delete,at least in
some case.
sure the program compiled and execute without any complain.
but take an insight look with valgrind :
zsh/2 9182 % valgrind ./a.out
==14833== Memcheck, a memory error detector
==14833== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et
al.
==14833== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright
info
==14833== Command: ./a.out
==14833==
==14833== Mismatched free() / delete / delete []
==14833== at 0x4023516: free (in /usr/lib/valgrind/
vgpreload_memcheck-x86-linux.so)
==14833== by 0x8048578: main (in /media/data/Master1_td_tps/C++/tp4/
a.out)
==14833== Address 0x42ca068 is 0 bytes inside a block of size 10
alloc'd
==14833== at 0x4023D04: operator new[](unsigned int) (in /usr/lib/
valgrind/vgpreload_memcheck-x86-linux.so)
==14833== by 0x8048568: main (in /media/data/Master1_td_tps/C++/tp4/
a.out)
==14833==
==14833== Mismatched free() / delete / delete []
==14833== at 0x402322D: operator delete(void*) (in /usr/lib/
valgrind/vgpreload_memcheck-x86-linux.so)
==14833== by 0x8048584: main (in /media/data/Master1_td_tps/C++/tp4/
a.out)
==14833== Address 0x42ca028 is 0 bytes inside a block of size 10
alloc'd
==14833== at 0x40238FC: malloc (in /usr/lib/valgrind/
vgpreload_memcheck-x86-linux.so)
==14833== by 0x8048558: main (in /media/data/Master1_td_tps/C++/tp4/
a.out)
==14833==
==14833==
==14833== HEAP SUMMARY:
==14833== in use at exit: 0 bytes in 0 blocks
==14833== total heap usage: 2 allocs, 2 frees, 20 bytes allocated
==14833==
==14833== All heap blocks were freed -- no leaks are possible
==14833==
==14833== For counts of detected and suppressed errors, rerun with: -v
==14833== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 19 from
8)
sure all blocks are freed , but why valgrind complain on it?
can some one kindly give an explication, i may be should post the
question on C++ forum, but i suppose
in C forum u guys may understand better the things inside.
Mug
i have a little question on the behaviors of free and delete.(C and C+
+)
here's a little program: test.cc
#include <stdlib.h>
int main(int argc, const char *argv[])
{
char *tmp1 = (char*) malloc(10);
char * tmp2 = new char [10];
free(tmp2);
delete(tmp1);
return 0;
}
i always have impression that free is equivalent to delete,at least in
some case.
sure the program compiled and execute without any complain.
but take an insight look with valgrind :
zsh/2 9182 % valgrind ./a.out
==14833== Memcheck, a memory error detector
==14833== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et
al.
==14833== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright
info
==14833== Command: ./a.out
==14833==
==14833== Mismatched free() / delete / delete []
==14833== at 0x4023516: free (in /usr/lib/valgrind/
vgpreload_memcheck-x86-linux.so)
==14833== by 0x8048578: main (in /media/data/Master1_td_tps/C++/tp4/
a.out)
==14833== Address 0x42ca068 is 0 bytes inside a block of size 10
alloc'd
==14833== at 0x4023D04: operator new[](unsigned int) (in /usr/lib/
valgrind/vgpreload_memcheck-x86-linux.so)
==14833== by 0x8048568: main (in /media/data/Master1_td_tps/C++/tp4/
a.out)
==14833==
==14833== Mismatched free() / delete / delete []
==14833== at 0x402322D: operator delete(void*) (in /usr/lib/
valgrind/vgpreload_memcheck-x86-linux.so)
==14833== by 0x8048584: main (in /media/data/Master1_td_tps/C++/tp4/
a.out)
==14833== Address 0x42ca028 is 0 bytes inside a block of size 10
alloc'd
==14833== at 0x40238FC: malloc (in /usr/lib/valgrind/
vgpreload_memcheck-x86-linux.so)
==14833== by 0x8048558: main (in /media/data/Master1_td_tps/C++/tp4/
a.out)
==14833==
==14833==
==14833== HEAP SUMMARY:
==14833== in use at exit: 0 bytes in 0 blocks
==14833== total heap usage: 2 allocs, 2 frees, 20 bytes allocated
==14833==
==14833== All heap blocks were freed -- no leaks are possible
==14833==
==14833== For counts of detected and suppressed errors, rerun with: -v
==14833== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 19 from
8)
sure all blocks are freed , but why valgrind complain on it?
can some one kindly give an explication, i may be should post the
question on C++ forum, but i suppose
in C forum u guys may understand better the things inside.
Mug