We help IT Professionals succeed at work.

Create array with undefined dimension

gandhirs
gandhirs asked
on
Medium Priority
284 Views
Last Modified: 2012-05-04
I have a program with a declaration of a pointer to a class (cStdDev):

cStdDev *qstats;

Later, i use a member of the class:

qstats->collect(d);

Now i want to have many objects of the same class on an array, like this, but the dimension of the array is not know a priori (it will be given on a variable like int num_term).  I'd like to do something like this:

cStdDev *qstats[];
...
qstats[i]->collect(d);

How can implement it correctly? (pointers?)

Thanks.
Comment
Watch Question

In order to store data, the program must set aside some memory to
store it in.
You could start with very large dimensions, larger than you'll ever need.
Or you can use some version of alloc() after you know how much you need.
CERTIFIED EXPERT

Commented:
The easiest way to declare something like this is to build from typedefs.  You can do a proper declaration with parens to indicate precedence, but it can become hard to read.

typedef cStdDev* cStdDevPtr;
typedef cStdDevPtr* cStdDevList;

// same as cStdDev** list = new (cStdDev*)[100], but clearer
cStdDevList theList = new cStdDevPtr[100];
// initialize theList somewhere
// ...
doProcess(theList, 100, NULL);


void doProcess(cStdDevList myList, int len, void *d) {
     ...
     myList[i]->collect(d);
}

Remember to delete each of the elements of the list, then do an array delete on the list itself.

Commented:
cStdDev **qstats;

When you know the dimension at run time use num_term to allocate:

qstats = new *cStdDev[num_term];

for (int i = 0; i < num_term; i++)
  qstats[i] = new cStdDev;


Also free the memory when done:

for (int i = 0; i < num_term; i++)
  delete [] qstats[i];

delete [] qstats;

Commented:
Just to be picky, jtm111's freeing of memory should delete the single cStdDev object:

for (int i = 0; i < num_term; i++)
 delete qstats[i];

But there is an easier and safer way of doing it (only a suggestion if you can rewrite the bits of code relating to this - or maybe an idea for when you write code like this in the future)

If your class cStdDev has a default constructor, then you could use a std::vector to store the actual objects:

include <vector>

std::vector<cStdDev> qstats(starting_number);

then, when you know how many you want, just do

qstats.resize(num_term);

The benefits are clarity, ease of use, no need to delete the objects in the end, it is exception safe (no way you can avoid calling delete on the created objects) etc.

Of course, qstats[0] is an object of class cStdDev, rather than a pointer to the object.  If you want a hand doing it this way, post a comment.

Author

Commented:
Could not implement any of the sugestions very well.  

When i do qstats = new *cStdDev[num_term];
I receive "syntax error: *"

To use vector, I received too many warnings.

I also tried to use the typedef suggestions, but also received run-time errors.

Innstead of using a array of pointers to objects I just used an array of objects and it worked well.

cStdDev *qstats

qstats = new cStdDev[num_term
cStdDev StdDevObj
for (int i = 1; i < num_term; i++)
{
qstats[i] = StdDevObj;  // faz uma copia do objecto para a posicao do array indicada
          bstats[i] = StdDevObj;
     }

Author

Commented:
Could not implement any of the sugestions very well.  

When i do qstats = new *cStdDev[num_term];
I receive "syntax error: *"

To use vector, I received too many warnings.

I also tried to use the typedef suggestions, but also received run-time errors.

Innstead of using a array of pointers to objects I just used an array of objects and it worked well.

cStdDev *qstats

qstats = new cStdDev[num_term
cStdDev StdDevObj
for (int i = 1; i < num_term; i++)
{
qstats[i] = StdDevObj;  // faz uma copia do objecto para a posicao do array indicada
          bstats[i] = StdDevObj;
     }

Author

Commented:
I wanted to say:

cStdDev *qstats

qstats = new cStdDev[num_term
cStdDev StdDevObj
for (int i = 1; i < num_term; i++)
{
    qstats[i] = StdDevObj;  
}

Thanks for the help.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.