Click here to Skip to main content
Licence 
First Posted 2 May 2000
Views 68,505
Bookmarked 13 times

ISArray

By | 9 May 2000 | Article
A simple templated array class.
  • Download source files - 2 Kb
  • Introduction

    ISArray is a simple array class template. It is especially useful when you need to dynamically allocate arrays of objects in functions that have many possible return points. Instead of having to call delete [] pArray at each return point, you can let the ISArray destructor handle it for you.

    Example

    int MyFunction()
    {
        // declare an array of 256 chars called "charArray"
        ISArray < char > charArray(256);
    
        // init to 0's
        charArray.Fill( (char)0 );
    
        // get a pointer to the start of the array
        char *pArray = (char *)charArray;
    
        // lots of ugly code with lots of ways out...
        if (...)
        {
            for (...)
            {
               if (...)
               {
                  // error!
                  return -1;
               }
            }
    
            if (...)
            {
               // error!
               return -2;
            }
            else
            {  
               // no error
               return 1;
            }
        }
    
        return 1;
    }
    

    If you had allocated the array the traditional C++ way, with

    char *pArray = new char[256];

    or the traditional C way, using

    pArray = (char *)malloc(256);

    you would have had to call delete [] pArray; or free(pArray) at each return point. But, with ISArray, the ISArray destructor handles this for you. This insures against memory leaks, and makes your code much cleaner to look at. In extreme cases (such as the one we wrote it for) this class can actually cut down the size of your app, by eliminating all of the clean-up code.

    In addition, ISArray allows you to delay the allocation of the memory, instead of doing it in the ISArray constructor. This can be very handy if the allocation is conditional. Of course, you don't have to test that the array has been allocated before deleting it, the ISArray destructor handles this for you, too.

    We've also included some handy utility functions to copy one array to another, set/get the array size, etc..

    Usage

    // include the header
    #include "ISArray.h"
    
    void func()
    {
        // declare an array - allocate in the ctor
        ISArray <CString> stringArray(100);
    
        // declare some arrays - allocate later
        ISArray <BYTE> bytes1;
        ISArray <BYTE> bytes2;
    
        // allocate now
        bytes1.SetSize(1000);
        bytes1.Fill(0);
       
        // copy
        bytes2 = bytes1;
    }
    

    Remember - have fun.

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here

    About the Author

    Chris Losinger

    Software Developer

    United States United States

    Member

    Chris Losinger is the president of Smaller Animals Software, Inc..

    Sign Up to vote   Poor Excellent
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

     
    You must Sign In to use this message board. (secure sign-in)
     
    Search this forum  
     FAQ
        Noise  Layout  Per page   
      Refresh
    Questionabout GetPtr() ? PinmemberHunt Chang19:22 31 Dec '06  
    AnswerRe: about GetPtr() ? PinmemberChris Losinger6:20 1 Jan '07  
    GeneralRe: about GetPtr() ? PinmemberHunt Chang5:47 2 Jan '07  
    GeneralIssue about the Fill() function PinmemberChristian Skovdal Andersen11:57 5 Jun '01  
    GeneralRe: Issue about the Fill() function PinmemberChris Losinger8:24 13 Jun '01  
    GeneralUseful, but std::vector will work to PinsussWilliam Kempf12:17 3 May '00  
    GeneralRe: Useful, but std::vector will work to PinsussChris Losinger12:28 3 May '00  
    GeneralRe: Useful, but std::vector will work to PinsussWilliam Kempf4:19 10 May '00  
    Other than the typos, yes it's equivalent. The following is also equivalent, and is a tad less typing:
     
    std::vector bytearray(100);
     
    The second parameter you supplied is optional, and if not provided defaults to T(), where T is the template type. Since our template is of char type, this is char(). The surprising thing to some is that char() is the same as char(0). We are told we must initialize else our values will contain junk so often that we fail to realize that these two lines are quite different:
     
    char a; // un-initialized and may contain any value
    char a(); // initialized, and for built in integral types
    // will have a value of 0
     
    The only trick with std::vector is accessing the internal data directly. Currently, there is no way to do this portably according to the standard. However, there is a defect reported and this will likely be changed in the near future. Until then, I'm aware of no implementations that don't behave the way you'd expect in this regard despite what the standard says. So, you can feel relatively safe using vectors this way today.
     
    char* p = &myvector[0];
     
    Just remeber that if you do this, the resulting pointer has the same validity restrictions as does an iterator. Certain operations on the vector may invalidate it.
     
    BTW... iterators have nothing to do with speed. In fact, since ISArray can give you a pointer to the data, it does in fact have iterators. The built in pointer types are a type of iterator. In fact, for some containers in many implementations the "iterator" type defined by the container is nothing more than a typedef to a raw pointer. Given this, a std::vector can replace built in arrays with identical performance in nearly every case. The only exception is when you need to interface with legacy code that operates on raw pointers. This is why the defect was written on the standard, since the "trick" I've shown here will eliminate even this deficiency, allowing us to always use the safer and more robust vector instead of arrays
    GeneralRe: Useful, but std::vector will work to Pinsusschris losinger4:49 10 May '00  
    GeneralRe: Useful, but std::vector will work to PinsussWilliam Kempf12:52 10 May '00  
    GeneralRe: Useful, but std::vector will work to Pinsusschris losinger15:00 10 May '00  

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    Permalink | Advertise | Privacy | Mobile
    Web03 | 2.5.120529.1 | Last Updated 10 May 2000
    Article Copyright 2000 by Chris Losinger
    Everything else Copyright © CodeProject, 1999-2012
    Terms of Use
    Layout: fixed | fluid