From: Andrew Wall on

"Daniel T." <daniel_t(a)earthlink.net> wrote in message
news:61261907-55cc-41d9-945b-322c020e6a7e(a)e23g2000yqd.googlegroups.com...
> 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);
>

I can heartily recommend this method: Wrap primitives, like pointers and
arrays, within a struct or class. It always simplifies the syntax needed to
access to the underlying data.

Andrew Wall


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

From: Pierre Asselin on
Andrew Wall <wallguide-usenet1(a)yahoo.com> wrote:

> "Daniel T." <daniel_t(a)earthlink.net> wrote in message
> > 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);
> >

> I can heartily recommend this method: Wrap primitives, like pointers and
> arrays, within a struct or class. It always simplifies the syntax needed to
> access to the underlying data.

Does that guarantee contiguous storage as in a float[][3] (no
padding at the end of the struct) ? The OP didn't ask for it, but
give that he is working on legacy code he might need it.

--
pa at panix dot com

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

From: Richard Howard on
> 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);

But I believe the compiler is free to add padding to the struct,
therefore potentially making my vector<Point3D> substantially
different to the array of arrays of floats that it was expecting.


--
[ 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 1 mar, 21:26, Richard Howard <rhphotography....(a)googlemail.com>
wrote:
> > 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);
>
> But I believe the compiler is free to add padding to the struct,

Not in this case.


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

From: Francis Glassborow on
Mathias Gaunard wrote:
> On 1 mar, 21:26, Richard Howard wrote:
> > 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);
>> But I believe the compiler is free to add padding to the struct,
>
> Not in this case.

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