From: Richard Howard on
I am currently working on some legacy software, and I tend to replace
calls to new/delete with auto_ptrs and new[]/delete[] with vectors ...
but how do I implement the following whist maintaining RAII, is there
a std method of doing this?


float (* coords)[3] = NULL;
....
long numPoints = sourceOfPoints->size();
coords = new float[numPoints][3]; // allocate the array of arrays
sourceOfPoints->GetPointAll(coords); // set the appropriate array
values on the array of arrays
....
delete [] coords;

I thought maybe

vector<float[3]>

but it complains at me

Thanks

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Daniel T. on
On Feb 26, 5:31 pm, Richard Howard <rhphotography....(a)googlemail.com>
wrote:
> I am currently working on some legacy software, and I tend to replace
> calls to new/delete with auto_ptrs and new[]/delete[] with vectors ...
> but how do I implement the following whist maintaining RAII, is there
> a std method of doing this?
>
> float (* coords)[3] = NULL;
> ...
> long numPoints = sourceOfPoints->size();
> coords = new float[numPoints][3]; // allocate the array of arrays
> sourceOfPoints->GetPointAll(coords); // set the appropriate array
> values on the array of arrays
> ...
> delete [] coords;
>
> I thought maybe
>
> vector<float[3]>
>
> but it complains at me

I would be inclined to do something like this:

struct Point3D
{
float value[3];
float operator[](int i) const { return value[i]; }
float& operator[](int i) { return value[i]; }
};


vector<Point3D> coords;
coords.resize(sourceOfPoints->size());
sourceOfPoints->GetPointAll(coords);

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: George Neuner on
On Fri, 26 Feb 2010 16:31:11 CST, Richard Howard
<rhphotography.net(a)googlemail.com> wrote:

>but how do I implement the following whist maintaining RAII, is there
>a std method of doing this?
>
>
> float (* coords)[3] = NULL;
>...
> long numPoints = sourceOfPoints->size();
> coords = new float[numPoints][3];
> sourceOfPoints->GetPointAll(coords);
>
>...
> delete [] coords;
>
>I thought maybe
>
> vector<float[3]>
>
>but it complains at me

This goes way back to C, but the only good solution I know of for
handling dynamic "shaped" arrays is to allocate a vector of equivalent
size and then access it through a shaped pointer.

e.g.,
vector<float> v( numPoints * 3 );
float (*coords)[3] = (float (*)[3]) &v[0];

George

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Mathias Gaunard on
On 26 f�v, 22:31, Richard Howard <rhphotography....(a)googlemail.com>
wrote:

> I thought maybe
>
> vector<float[3]>

Try
vector< array<float, 3> >

With array being from TR1 or boost.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Lance Diduck on

> I thought maybe
>
> vector<float[3]>
>
> but it complains at me
>

#include <tr1/array> //gcc
#include <array> //msvc

std::vector<std::tr1::array<float,3> >
Also, for older compilers, there is boost::array that is the same thing


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]