This is the mail archive of the
gsl-discuss@sources.redhat.com
mailing list for the GSL project.
Re: gsl_vector_const_view: pointer to constant or constant pointer
Fleur Kelpin writes:
> Is it correct that the definition
> typedef struct
> {
> const gsl_vector *vector;
> } gsl_vector_const_view;
> would have the advantage that gsl_vector_const_view s can be initialized
> and reassigned but the disadvantage that memory is allocated for the
> vectors in the views and will need to be freed after use?
Views are intended to be lightweight objects -- this is the main
reason they are allocated on the stack, to avoid the overhead of
calling malloc. It also means that they don't have to be freed.
> I ended up changing to non-const gsl_vector_view and casting the const
> from my arrays, as Joohnas Pihlaja suggested. The other solution I could
> think of was sticking to const double * s and creating the
> gsl_vector_const_view only last minute when I need it.
The latter is definitely preferred. Views should always be
initialised at the same time they are defined, opening up a new scope
if needed. It should not be necessary to cast away const when using
GSL.
e.g.
int main()
{
const double x[]={1,2,3,4};
...
{
gsl_vector_const_view xview = gsl_vector_const_view_array(x,4);
....
}
return 0;
}