CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Threaded View

  1. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Help reviewing C++ Test

    Quote Originally Posted by jingato View Post
    Well, I was trying to only send it to the poeple who were interested, but I guess If I don't give the name of the company it was for it should be ok.
    1) Your class lacks a copy constructor, assignment operator, and destructor that would handle the dynamically allocated memory.

    2) The class has memory leaks since you didn't implement the destructor in CContainer. You also don't delete the CItem you attempt to remove in the Remove() function.

    Without 2), any utility the class may have becomes useless. Even if you had a perfectly running Add() function, you can't use such a class in a real program, since it would leak memory. Once you implement 2), you then run into the problem of item 1), which is the copying aspect.

    I think what may have cost you is the lack of attention to not leak memory (there isn't a single call to "delete" as far as I can tell). That is a bad sign and a major oversight for anyone applying for a job as a C++ programmer. As far as the test goes, anyone can make a logic mistake, but the lack of cleanup of memory is not a mistake in logic.

    As to the implementation of the destructor, if you implemented the destructor, then I could easily crash your code with a two or three line main program.
    Code:
    int main()
    {
        char pbuffer[] = "abc123";
        CContainer<int> c1(pbuffer, sizeof(pbuffer));
        CContainer<int> c2(pbuffer, sizeof(pbuffer));
        c1 = c2;
    }
    If the destructor were implemented, then the program above would have a memory leak and a double deletion error, possibly causing a crash on the return of main(). The way you avoid the crash is to implement the copy-assignment operations, or make these operations private functions and unimplemented.

    Since you didn't post any code showing your main() program (a class can't live just by itself, it needs a driver to test it), I'm showing how it is very easy to render the code you have now very breakable. To get a complete, bug free (or close to bug free) implementation, you need to address the two items above.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; September 6th, 2012 at 04:38 PM.

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