why2jjj
July 20th, 2008, 09:25 PM
I decided to try a brain teaser to test my knowledge of C++. The goal is to write a Point class with x, y float coordinates and a third member which is a string in char * format.
I think I got it all but valgrind using libc.so.6 running Ubuntu Linux 8.04 is telling me otherwise, which looks like it's within my assignment constructor method definition. Does anyone know what I am doing wrong? I am almost thinking valgrind is wrong (or libc.6.so because it also blows up when I just type ./a.out)!:
Code is attached. Valgrind output is below...thanks!:
valgrind output:
freyguy@indurain:~/Desktop/Practice/NamedPoint$ g++ -g namedpoint.cpp
freyguy@indurain:~/Desktop/Practice/NamedPoint$ valgrind ./a.out --leak-check=full
==24600== Memcheck, a memory error detector.
==24600== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
==24600== Using LibVEX rev 1804, a library for dynamic binary translation.
==24600== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
==24600== Using valgrind-3.3.0-Debian, a dynamic binary instrumentation framework.
==24600== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
==24600== For more details, rerun with: -v
==24600==
name: hello
name(hi): raw
==24600== Invalid read of size 4
==24600== at 0x804883F: namedPoint::~namedPoint() (namedpoint.cpp:43)
==24600== by 0x804878C: main (namedpoint.cpp:135)
==24600== Address 0x42ad0a8 is 8 bytes inside a block of size 12 free'd
==24600== at 0x40222EC: operator delete(void*) (vg_replace_malloc.c:342)
==24600== by 0x8048775: main (namedpoint.cpp:134)
==24600==
==24600== Invalid free() / delete / delete[]
==24600== at 0x40222EC: operator delete(void*) (vg_replace_malloc.c:342)
==24600== by 0x8048797: main (namedpoint.cpp:135)
==24600== Address 0x42ad0a0 is 0 bytes inside a block of size 12 free'd
==24600== at 0x40222EC: operator delete(void*) (vg_replace_malloc.c:342)
==24600== by 0x8048775: main (namedpoint.cpp:134)
==24600==
==24600== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 17 from 1)
==24600== malloc/free: in use at exit: 18 bytes in 2 blocks.
==24600== malloc/free: 4 allocs, 3 frees, 34 bytes allocated.
==24600== For counts of detected errors, rerun with: -v
==24600== searching for pointers to 2 not-freed blocks.
==24600== checked 109,452 bytes.
==24600==
==24600== LEAK SUMMARY:
==24600== definitely lost: 18 bytes in 2 blocks.
==24600== possibly lost: 0 bytes in 0 blocks.
==24600== still reachable: 0 bytes in 0 blocks.
==24600== suppressed: 0 bytes in 0 blocks.
==24600== Rerun with --leak-check=full to see details of leaked memory.
freyguy@indurain:~/Desktop/Practice/NamedPoint$
I think I got it all but valgrind using libc.so.6 running Ubuntu Linux 8.04 is telling me otherwise, which looks like it's within my assignment constructor method definition. Does anyone know what I am doing wrong? I am almost thinking valgrind is wrong (or libc.6.so because it also blows up when I just type ./a.out)!:
Code is attached. Valgrind output is below...thanks!:
valgrind output:
freyguy@indurain:~/Desktop/Practice/NamedPoint$ g++ -g namedpoint.cpp
freyguy@indurain:~/Desktop/Practice/NamedPoint$ valgrind ./a.out --leak-check=full
==24600== Memcheck, a memory error detector.
==24600== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
==24600== Using LibVEX rev 1804, a library for dynamic binary translation.
==24600== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
==24600== Using valgrind-3.3.0-Debian, a dynamic binary instrumentation framework.
==24600== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
==24600== For more details, rerun with: -v
==24600==
name: hello
name(hi): raw
==24600== Invalid read of size 4
==24600== at 0x804883F: namedPoint::~namedPoint() (namedpoint.cpp:43)
==24600== by 0x804878C: main (namedpoint.cpp:135)
==24600== Address 0x42ad0a8 is 8 bytes inside a block of size 12 free'd
==24600== at 0x40222EC: operator delete(void*) (vg_replace_malloc.c:342)
==24600== by 0x8048775: main (namedpoint.cpp:134)
==24600==
==24600== Invalid free() / delete / delete[]
==24600== at 0x40222EC: operator delete(void*) (vg_replace_malloc.c:342)
==24600== by 0x8048797: main (namedpoint.cpp:135)
==24600== Address 0x42ad0a0 is 0 bytes inside a block of size 12 free'd
==24600== at 0x40222EC: operator delete(void*) (vg_replace_malloc.c:342)
==24600== by 0x8048775: main (namedpoint.cpp:134)
==24600==
==24600== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 17 from 1)
==24600== malloc/free: in use at exit: 18 bytes in 2 blocks.
==24600== malloc/free: 4 allocs, 3 frees, 34 bytes allocated.
==24600== For counts of detected errors, rerun with: -v
==24600== searching for pointers to 2 not-freed blocks.
==24600== checked 109,452 bytes.
==24600==
==24600== LEAK SUMMARY:
==24600== definitely lost: 18 bytes in 2 blocks.
==24600== possibly lost: 0 bytes in 0 blocks.
==24600== still reachable: 0 bytes in 0 blocks.
==24600== suppressed: 0 bytes in 0 blocks.
==24600== Rerun with --leak-check=full to see details of leaked memory.
freyguy@indurain:~/Desktop/Practice/NamedPoint$