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

Thread: ArrayList class

  1. #1
    Join Date
    Apr 2010
    Posts
    9

    ArrayList class

    I just finished building an arraylist class in c++ and was wondering if i did the add method right. I did java programming before but am new with pointers. If anyone has any time could you check out the add method of my code and tell me if you see any flaws. If i did it right then it should double the arrays size every time it fills up and then have my lits point to it.

    template <class element>
    void ArrayList<element>::add(const element& item)
    {
    if(length == maxSize)
    {
    maxSize *= 2;
    element *temp = new element[maxSize];

    for(int i = 0; i < length; i++)
    {
    temp[i] = list[i];
    }

    delete [] list;
    list = temp;
    temp = NULL;
    }

    list[length] = item;
    length++;
    }

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,893

    Re: ArrayList class

    That looks fine. However, are you aware you're duplicating the functionality of the std::vector template?

  3. #3
    Join Date
    Apr 2010
    Posts
    9

    Re: ArrayList class

    Yes i know about the vector class I was just trying to learn more about c++ and how there template and pointer system worked. Ive done all my programming in java so far lol

  4. #4
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,359

    Re: ArrayList class

    Quote Originally Posted by rkennedy9064 View Post
    I just finished building an arraylist class in c++ and was wondering if i did the add method right. I did java programming before but am new with pointers. If anyone has any time could you check out the add method of my code and tell me if you see any flaws. If i did it right then it should double the arrays size every time it fills up and then have my lits point to it.

    Code:
    template <class element>
    void ArrayList<element>::add(const element& item)
    {
        if(length == maxSize)
        {
            maxSize *= 2;
            element *temp = new element[maxSize];
    
            for(int i = 0; i < length; i++)
            {
                temp[i] = list[i];
            }
    
            delete [] list;
            list = temp;
            temp = NULL;
        }
        
        list[length] = item;
        length++;
    }
    If this is not an exercise, then you did it wrong. The correct way to do it would have been to use a vector.

    That said, from a coding point of view, your code works, but it is not memory leak free in case of exceptions. If you have an exception before the end of the function, you will exit before having deleted temp.

    There are two ways to solve this:
    1 - (IMO the "bad" way): use try catch and always delete.
    2 - (IMO the "good" way): Create the memory in a RAII object. For example, another ArrayList (we'll call it arrayList2). Simply fill this arrayList2, while making sure it has pre-allocated maxSize*2. Once you are done, just swap the list pointers from both objects. Your current object will be bigger, and the arrayList2 will contain the "old" data, and will be destroyed along with arrayList2 on leaving the method. If there is ANYTHING that goes wrong, and you prematurely exit, rest assured that ArrayList's destructor will be doing the cleanup. Another advantage is this gives you a "strong" exception safety. Basically, you are guaranteeing that should an exception occur, your *this object will not be modified.

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,893

    Re: ArrayList class

    I would point out that it's unnecessary to set temp to NULL there, since it's about to go out of scope anyway. Doesn't really hurt though.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,446

    Re: ArrayList class

    Quote Originally Posted by rkennedy9064 View Post
    I just finished building an arraylist class in c++ and was wondering if i did the add method right. I did java programming before but am new with pointers.
    There is a time and place for usage of pointers in a C++ program.

    One of those times is when you are using polymorphism, where you need a pointer (or reference) to a parent class. The other is if you really do need an object to outlast the lifetime of the local scope where it's declared. Another reason to use a pointer is if the function you're calling requires a pointer (obviously).

    All of the above scenarios occur in most non-trivial C++ programs, so you aren't going to miss the chance to use pointers.

    However, trying to introduce yourself to pointers by writing container classes, is IMO not the way to go. There are already classes that do what your code is attempting to do, such as vector (which has already been mentioned), and they work already.

    It is impossible for a beginner to write a container class correctly-- it isn't impossible for a beginner to actually start using the existing container classes correctly. Instead of wasting a lot of time writing container classes, learn to actually use the container classes that exist to your advantage, along with the various algorithms that you can use with the containers. Also, if you want to study pointers, study why pointer members (such as your class has) can be flawed. You don't need to physically code a vector class to learn the pitfalls of such classes.

    I'll give you some advice -- every C++ interview I've taken part in (both interviewing and being interviewed) required extensive knowledge of the container classes and algorithms. Not one question asked me (or asked to the applicaant) required coding a vector class, linked list, etc., As a matter of fact, if you boasted about writing your own container classes, and you knew little or nothing about the standard containers and algorithms, that was a strike against you.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; April 23rd, 2010 at 08:08 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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center