Fog Creek Software
Discussion Board




initialzing an array of pointers in a function

I have an array of pointers to objects declared like this:

MyClass** ppArray = NULL;

I want to write a function to initialize this array. Do I have to create a function that takes in a triple pointer?

void Initialize(MyClass*** pppArray);

This seems like the only way to do this to me, but I have never really seen triple pointers used before. Is there a better way to do it?

Modern Cowboy
Thursday, July 08, 2004

Perhaps a typedef would help.

Is this C or C++?

Almost Anonymous
Thursday, July 08, 2004

init( yourclass **pp) is ok.

redguardtoo
Thursday, July 08, 2004

In C, the triple pointer is what you need.

In C++, you could pass ppArray by reference.

The typedef comment is helpful. Whenever you get to triple pointers, you should start thinking about typedefs to remove some of the *'s.

Ian
Thursday, July 08, 2004

It is C++ and I used the pass by reference suggestion. It works. Thanks.

Still looks a little weird to me though. I have never seen it done before. My prototype now looks like:

void Initialize(MyClass**& ppArray)

Modern Cowboy
Thursday, July 08, 2004

Spice up your life a bit and use vector<CMyClass*>

free(malloc(-1))
Thursday, July 08, 2004

"It is C++ and I used the pass by reference suggestion. It works. Thanks."

That's terrible C++ code!  Use references, use vector<>, wrap it in the class or something.

Of course, you might not have much choice in the matter.  If you don't, the reference notation is the best option.

Almost Anonymous
Thursday, July 08, 2004

>>>
I have an array of pointers to objects declared like this:

MyClass** ppArray = NULL;
<<<

You do realize that this is not a declaration of an array of pointers, don't you?

Concerning arrays in C, the name of the array is a pointer to the array.  Taking the simple case of int

    /* Declare an array of 100 int (or anything at all) */
    int arrayOfInt[100];

    /* Pass a pointer to the array to a function */
    initArrayFunc( arrayOfInt );

    /* Define the function */
    initArrayFunc( int *a )
    {
        int i;
        for ( i = 0; i < 100; i++) a[i] = 1;
        return;
    }

It doesn't matter what it is an array of. 

Of course, you'll write something without the hardcoded constants.

mackinac
Thursday, July 08, 2004

>>>Still looks a little weird to me though. I have never seen it done before. My prototype now looks like:

void Initialize(MyClass**& ppArray)
<<<

Looks weird and is unnecessary.  You pass by reference if you are going to change the object within the function (or to avoid passing a large object on the stack).  You did not indicate any intention of changing ppArray.  That is, changing ppArray to point to a different array.  To access the array elements it is only necessary to pass ppArray.

mackinac
Thursday, July 08, 2004

It is pretty amazing how people who know even less than me have tried to answer this question and are completely wrong. Thanks to everyone who did help. I am learning slowly.

Modern Cowboy
Thursday, July 08, 2004

Ok, I took a second to sketch this thing out on paper. Unfortunately this forum doesn't handle art that well, so I'll have to just talk about my solutions.

First off, in such instances I resort to typedefs fairly quickly. Please excuse the ugly naming convention, I'm just doing this quickly.

So, you have some class:

class MyNiftyType { }

You want an array of *MyNiftyType. So, start with:

typedef MyNiftyType *MyNiftyTypePtr;

Now, the array pointer is:

MyNiftyTypePtr *arrayOfPtrs;

Or, with another typedef:

typedef MyNiftyTypePtr *ArrayOfMyNiftyTypePtrs;

Now, to initialize this thing, you've got three choices depending on who initializes the memory that arrayOfPtrs points at (the actual array of pointers, not the objects they point to).

1) The caller creates the array, then calls the function to fill in the contents. That's easy:

    arrayOfPtrs = new MyNiftyTypePtr[ numPtrs ];
    InitializeArray( arrayOfPtrs, numPtrs );

where the function is:

    void InitializeArray( ArrayOfMyNiftyTypePtrs p, size_t numPtrs );

2) The function allocates and returns the array of a given size, and does that such stuff.

    arrayOfPtrs = InitializeArray( numPtrs );

The function is:

    ArrayOfMyNiftyTypePtrs InitializeArray( size_t numPtrs );

3) You, for some reason, want to return the array through a parameter. For example, the array may already exist, so you need to clean it up first. The calling sequence generally looks like this:

  arrayOfPtrs = 0; // You don't use NULL in C++
  InitializeArray( &arrayOfPtrs, numPtrs );

The prototype of the function looks like this:

  void InitializeArray( ArrayOfMyNiftyTypePtrs *p, size_t numPtrs );

As a variation, you can instead pass the pointer p by reference, as in:

  void InitializeArray( ArrayOfMyNiftyTypePtrs &p, size_t numPtrs );

But that's ugly, confusing, and generally a bad idea.

In any kind of real C++ code I'd be using objects and collections and auto_ptrs all over the place, but I don't think that's what you want to hear right now. :-)

-Chris

Chris Tavares
Thursday, July 08, 2004

Thanks Chris!  Varied, well explained, and removes the grotty stuff fairly well.  Good job.

AllanL5
Friday, July 09, 2004

*  Recent Topics

*  Fog Creek Home