[vtkusers] Arrays and double arrays of VTK pointers

Shawn Waldon shawn.waldon at kitware.com
Mon Aug 22 09:55:07 EDT 2016


On Mon, Aug 22, 2016 at 9:47 AM, David Lonie <david.lonie at kitware.com>
wrote:

> On Fri, Aug 19, 2016 at 2:42 PM, Roberto De Leo
> <roberto.deleo at howard.edu> wrote:
> > <--- Example: 1-dim array of N pointers to vtkPolyData
> >
> > vtkPolyData *p[N];
> >  for( int i=0; i<N; i++ )
> >     p[i] = vtkPolyData::New()
>
> This looks good. Just be sure to have a similar loop somewhere that
> calls p[i]->Delete() to clean these up when you're done using them.
>
> > ---------------->
> >
> > <--- Example: 2-dim array of NxM pointers to vtkPolyData
> >
> > vtkPolyData ***p = new vtkPolyData** [N];
> > for( int i=0; i<N; i++ ){
> >    p[i] = new vtkPolyData* [M];
> >    for( int j=0; j<M; j++ )
> >        p[i][j] = vtkPolyData::New();
> > }
>
> I think you'd be better off with something like
>
> vtkPolyData *p[N][M];
>   for (... i to N ...)
>     for (...j to M...)
>       p[i][j] = vtkPolyData::New();
>
> This way you don't have to deal with allocating/freeing the N arrays
> of size M, and you'll actually get a contiguous block of vtkPolyData*
> pointers. (The code you posted actually allocates an array of N
> pointers to non-contiguous arrays of vtkPolyData*[M], rather than a
> single block of NxM vtkPolyData*).
>

I would use vtkSmartPointer or vtkNew so that you don't have to worry about
deleting them in a similar loop later.  vtkNew has the advantage that you
don't even have to write the allocation loop, but you have to call Get() to
pass it as a pointer to another function.

Like this:
  vtkSmartPointer<vtkPolyData> p[N][M];
  for (... i to N ...)
    for (... j to M ...)
      p[i][j] = vtkSmartPointer<vtkPolyData>::New();

or:
  vtkNew<vtkPolyData> p[N][M];  // vtkNew's constructor does the
allocation, so no need for an allocation loop

HTH,
Shawn

>
> HTH,
> Dave
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at http://www.kitware.com/
> opensource/opensource.html
>
> Please keep messages on-topic and check the VTK FAQ at:
> http://www.vtk.org/Wiki/VTK_FAQ
>
> Search the list archives at: http://markmail.org/search/?q=vtkusers
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/vtkusers
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20160822/96de9cb8/attachment.html>


More information about the vtkusers mailing list