Dynamic Arrays Holding Objects



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 28/08/2011 at 11:30, xxxxxxxx wrote:

    User Information:
    Cinema 4D Version:   12 
    Platform:   Windows  ;   
    Language(s) :     C++  ;

    ---------
    Hey guys,

    Noobish question.
    I'm having trouble creating a dynamic array with C++ that is holding objects in it.
    I can make dynamic integer arrays just fine. But it doesn't work when I use the BaseObject type for my array:

        BaseObject *countsteps = doc->SearchObject("Steps")->GetDown(); //Get the first child under the parent object called "Steps"  
      int stepcount = 0;  
      
      BaseObject *steparr[3];                     //This works....But it's not dynamic  
      //BaseObject *steparr[stepcount];           //<---------Does not work!!!   
      
      while(countsteps)  
        {  
         stepcount ++;                            //Counts the number of child objects(steps) in the group              
         countsteps = countsteps->GetNext();  
        }  
      
      BaseObject *steps = doc->SearchObject("Steps")->GetDown(); //Get the first child of the group  
      int j=0;  
      for(j=0; j < stepcount; j++)   
       {  
        steparr[j] = steps;                    //Fills the array with the child objects each time it loops      
        steps = steps->GetNext();      
       }  
       
      GePrint(steparr[0]->GetName());  
      GePrint(steparr[1]->GetName());  
      GePrint(steparr[2]->GetName());
    

    All this code does is fill an array with the child objects that are under a parent object.
    It works fine if I use a set constant array size. But I'd like to know how to make it dynamic so I don't have to set the array size to some arbitrary higher value than what I need.

    -ScottA



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 28/08/2011 at 12:24, xxxxxxxx wrote:

    Hy Scott,

    in C++ there is no such thing as a dynamic Array. Before you can fill an array, you have to allocate the memory.

    But you could use for example the STL (Standard Template Library). It will handle the Memory Management and GC for you ... but

    http://www.sgi.com/tech/stl/

    ... actually I think, that is not what you want. If you look up the c4d_memery class in the help and if you do not understand why you have to use one of these functions or not, I recommend to read about pointers in C and C++. This is very important to handle exactly such things.

    This seems a good start:
    http://cslibrary.stanford.edu/102/PointersAndMemory.pdf

    You try to define a local variable with a size which is not known at compile time. This will not work. If you need memory during runtime, you will have to allocate it and not forget to free it afterwards.

    >> All this code does is fill an array with the child objects that are under a parent object.

    Actually, it fills an array of pointers with pointers. There are no objects involved here.

    Cheers,
    maxx



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 28/08/2011 at 12:38, xxxxxxxx wrote:

    Use the GeDynamicArray and GeAutoDynamicArray in the SDK to do this. The auto version works particularly well (seems to for my purposes, anyway).



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 28/08/2011 at 14:00, xxxxxxxx wrote:

    Thanks Steve,

    I tend to use lots of dynamic arrays in Coffee and Python. And GeDynamicArray and GeAutoDynamicArray looks like something very useful that I would get a lot of use out of.
    But there are no examples of how to use them in the SDK other than the ge_dynamicarray.h file that has the actual code in it.

    From what I gather from the .h file. BaseObject isn't a supported type. Just things like an object's container and matrix?
    Without seeing an example. I'm lost at how to get started using it to hold object references like the objects I grab from a loop.

    Any chance of seeing a small example of that?
    Nothing fancy. Just something to help get the idea how to construct it.

    -ScottA



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 28/08/2011 at 14:14, xxxxxxxx wrote:

    Here an example:

      
    int stepcount = 0;  
    BaseObject *nextObjP = doc->SearchObject("Steps")->GetDown();   
      
    while(nextObjP )  
    {  
         stepcount ++;                            //Counts the number of child objects(steps) in the group              
         nextObjP = nextObjP ->GetNext();  
    }  
      
      
    GeDynamicArray<BaseObject *> objectPointers(stepcount);    
      
    nextObjP = doc->SearchObject("Steps")->GetDown();  
    for(int i = 0; i <stepcount;i++)  
    {  
      objectPointers[i] = nextObjP;  
      nextObjP = nextObjP->GetNext();  
    }  
      
    

    Now you end up with the objectPointers array filled with the object-pointers.

    Cheers,
    maxx

    Btw. thanks for pointing out the GeDynamicArray class, had no need yet but good to know cinema got its own classes for this.



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 28/08/2011 at 14:49, xxxxxxxx wrote:

    Thanks Maxx.
    I'm sure I'll be using this a lot once I get better with C++.

    -ScottA



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 29/08/2011 at 03:59, xxxxxxxx wrote:

    Just a final point, if you use GeAutoDynamicArray you don't even have to count the initial size. You can do but you don't have to. Just create the array and start adding items to it using the [] notation.



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 29/08/2011 at 05:48, xxxxxxxx wrote:

    Howdy,

    For nodes, it's probably easier to use the AtomArray class, since all nodes inherit from C4DAtom. 😉

    Adios,
    Cactus Dan


Log in to reply