how to loop an array of pointers passed to a function?


i have been trying to find a solution to this online with no luck, I guess i am trying to do something wrong (because my Python way of thinking probably).

I have a class that has a function that receives an array of pointers (to instances of other class). It needs to loop the array and do some calculations with each instance. I dont know how to loop it since I need to know the size of the array for the loop and i cannot get it (or i dont know how to).

As far as i undersntad I cannot get the size of the array because it is just a pointer to the first item of the array. (needless to say i dont fully understand pointers very well, still learning)

Is the only solution to pass the size of the array to the function together with the pointer?

not sure if i explained it very well. The array o pointers is declared in testApp.h

DraggableRect *stack[8];  

then initialised like this in testApp.cpp

for (int i=0; i<8; i++ ){  
       stack[i]= new DraggableRect();  

passed this way by testApp instance to Selection class instance


and this is the contains function declaration at Selection.h

void Selection::contains(DraggableRect *stack[]);  

and finally accesed like this inside the Selection::contains function (This is where the problem is!)

for (int i=0; i<THESIZEOFTHESTACK; i++ ){  

I would like to be able to get dinamically the size of the stack.
sizeof(stack) just returns the size of the pointer to the stack as far as i understand. I can loop it with a while but eventually i go further than the stack limit and the app crashes.

i have been checking the nice introduction to pointers in the wiki but still dont fully understand it i guess.




use std::vector instead.

#include <vector>  
// declare stack  
typedef std::vector<DraggableRect*> DraggableRectStack;  
DraggableRectStack stack;  
// fill stack  
for (int i=0; i<8; i++ ){   
       stack[i]= new DraggableRect();   
void Selection::contains(const DraggableRectStack& stack) {  
   for (int i=0; i<stack.size(); ++i ){   

Have a read about STL-container-classes, they are really powerful and the c+±way of handling dynamic collections.


like sth says, use vectors, they’re nicer than arrays.

but, in this: [quote author=“altern”]

void Selection::contains(DraggableRect *stack[]);  


when you say stack[] you imply a pointer already. so you either go DraggableRect* stack, which means a pointer to a DraggableRect, or DraggableRect stack[], which means… a pointer to a DraggableRect. but DraggableRect* stack[] means a pointer to a pointer to a DraggableRect, which is not what you want.

does that make sense? i find it helps when you’re dealing with pointers to put the ‘*’ next to the class, eg:

DraggableRect rect;  
DraggableRect* pointerToRect;  
pointerToRect = &rect;  

you’ll get it eventually…

…mmmm… i am a bit lost. i guess i need to test this with a simple example. the code i am dealing with is spread over few files already and it does not help.

thanks for the tips. i will write back when i get somewhere.

btw. the suggestion above by sth compiles but then the app crashes on startup.

Hey altern,

It’s quite hard to tell where the problem might be - can you zip up your source files, or a minimal set/toy example of them and post the link? That way we can get to the bottom of the problem efficiently.