Click here to Skip to main content
15,880,427 members
Articles / Programming Languages / C++
Article

ISArray

Rate me:
Please Sign up or sign in to vote.
3.67/5 (3 votes)
9 May 2000 85.3K   446   13   11
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


    Written By
    Software Developer
    United States United States
    Chris Losinger was the president of Smaller Animals Software, Inc. (which no longer exists).

    Comments and Discussions

     
    Questionabout GetPtr() ? Pin
    Hunt Chang31-Dec-06 19:22
    Hunt Chang31-Dec-06 19:22 
    AnswerRe: about GetPtr() ? Pin
    Chris Losinger1-Jan-07 6:20
    professionalChris Losinger1-Jan-07 6:20 
    GeneralRe: about GetPtr() ? Pin
    Hunt Chang2-Jan-07 5:47
    Hunt Chang2-Jan-07 5:47 
    GeneralIssue about the Fill() function Pin
    Christian Skovdal Andersen5-Jun-01 11:57
    Christian Skovdal Andersen5-Jun-01 11:57 
    GeneralRe: Issue about the Fill() function Pin
    Chris Losinger13-Jun-01 8:24
    professionalChris Losinger13-Jun-01 8:24 
    memset doesn't work with classes. (unless you want to treat your classes as C-structs and fill them solid with the same byte value)

    -c

    ------------------------------
    Smaller Animals Software, Inc.
    http://www.smalleranimals.com
    GeneralUseful, but std::vector will work to Pin
    William Kempf3-May-00 12:17
    sussWilliam Kempf3-May-00 12:17 
    GeneralRe: Useful, but std::vector will work to Pin
    Chris Losinger3-May-00 12:28
    professionalChris Losinger3-May-00 12:28 
    GeneralRe: Useful, but std::vector will work to Pin
    William Kempf10-May-00 4:19
    sussWilliam Kempf10-May-00 4:19 
    GeneralRe: Useful, but std::vector will work to Pin
    Chris Losinger10-May-00 4:49
    professionalChris Losinger10-May-00 4:49 
    GeneralRe: Useful, but std::vector will work to Pin
    William Kempf10-May-00 12:52
    sussWilliam Kempf10-May-00 12:52 
    GeneralRe: Useful, but std::vector will work to Pin
    Chris Losinger10-May-00 15:00
    professionalChris Losinger10-May-00 15:00 

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

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