CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2008
    Posts
    11

    brain teaser (but issue for me)- write a C++ class with char * member

    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$
    Attached Files Attached Files

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: brain teaser (but issue for me)- write a C++ class with char * member

    Well, it doesn't look like it'd be a problem, but the first thing I'd do is declare (but don't define, yet), a copy constructor as a private member. This simply ensures that it can't be generated automatically doing who-knows-what. You may wish to actually define it.

    Actually, a common way to define operator= is to use the copy constructor to declare a temporary object on the stack, then do a "shallow swap" between the this pointer and this stack object, then allow the temporary to be destructed when the function exits. Nice and elegant. A question for those more experienced with this technique----any reason why the shallow swap has to be more complex than three memcpys, even with C++ objects?

    EDIT: No, it's simpler than that. Take a look:
    Code:
       hi = hi2;
    I think what you meant to do was:
    Code:
       *hi = *hi2;
    Last edited by Lindley; July 20th, 2008 at 09:49 PM.

  3. #3
    Join Date
    Jul 2008
    Posts
    11

    Re: brain teaser (but issue for me)- write a C++ class with char * member

    Quote Originally Posted by Lindley

    EDIT: No, it's simpler than that. Take a look:
    Code:
       hi = hi2;
    I think what you meant to do was:
    Code:
       *hi = *hi2;
    Well slap my silly and call me a mongoose...can I get a 'DOH!' in the house?

    Thanks! Appreciate it! Too much code staring way overlooked this...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured