← Back to team overview

dolfin team mailing list archive

Re: std::vector and pointers

 



Garth N. Wells wrote:

Ola Skavhaug wrote:
On Wed, Aug 19, 2009 at 10:14 PM, <kent-and@xxxxxxxxx> wrote:
I didn't realise until recently that storage in std::vector is
guaranteed by the C++ standard to be contiguous (versus only being
contiguous in all known implementations). Using this, I think that we
can have safer and cleaner code by using more std::vector in the linear
algebra interface. For example, we could have

   GenericVector::get(std::vector<double>& );
   GenericVector::set(const std::vector<double>& );

instead of

   GenericVector::get(double* x);
   GenericVector::set(const double* x);

For backends that want a pointer to an array, we can pass &x[0]. Any
opinions?

Is the use of &x[0] safe ? Will it change if the vector is resized ? This
is maybe
nothing to worry about ?
Sure, this is something to worry about, and there will be no magic here :)
However, it will enable us to make range checks and issue error
messages more easily.


We wouldn't let any objects store a pointer to &x[0]. Just as we don't now - it's just data in, data out. On top of the points made by Ola, it will also reduce the likelihood of memory leaks. We can also eliminate some redundant copying of std::vectors into arrays and back.

Garth


I've created Blueprint for this:

   https://blueprints.launchpad.net/dolfin/+spec/stl-vectors-la

and set the status to 'Discussion'.

Garth

Ola

Kent

Garth
_______________________________________________
DOLFIN-dev mailing list
DOLFIN-dev@xxxxxxxxxx
http://www.fenics.org/mailman/listinfo/dolfin-dev

_______________________________________________
DOLFIN-dev mailing list
DOLFIN-dev@xxxxxxxxxx
http://www.fenics.org/mailman/listinfo/dolfin-dev




_______________________________________________
DOLFIN-dev mailing list
DOLFIN-dev@xxxxxxxxxx
http://www.fenics.org/mailman/listinfo/dolfin-dev




Follow ups

References