public inbox for gsl-discuss@sourceware.org
 help / color / mirror / Atom feed
* gsl_vector_const_view: pointer to constant or constant pointer
@ 2002-08-09 21:20 Fleur Kelpin
  2002-08-09 23:29 ` Fleur Kelpin
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Fleur Kelpin @ 2002-08-09 21:20 UTC (permalink / raw)
  To: gsl-discuss

Hi all!

Compiling

#include <gsl/gsl_vector.h>

int main()
{
  const double x[]={1,2,3,4};
  gsl_vector_const_view xview;
  xview = gsl_vector_const_view_array(x,4);
  return 0;
}

gives a
warning: assignment of read-only variable `xview'
since (in gsl_vector_double.h) gsl_vector_const_view is declared as a
constant pointer to a _gsl_vector_const_view:

typedef struct
{
  gsl_vector vector;
} _gsl_vector_const_view;

typedef const _gsl_vector_const_view gsl_vector_const_view;
        ^^^^^

Is this as intended? The manual states that 'Vector views can be defined
for both constant and non-constant vectors, using separate types that
preserve constness'. It seems to me that the point of the special const
type is to prevent changes to the vector viewed, not also changes to the
pointer to the view. I could easily prevent such changes to the pointer
myself when necessary, declaring

const gsl_vector_const_view xview;

But currently you cannot choose!

Am I missing something or can/could/should this be fixed?
(I think this can be done by moving the const inside the struct:

typedef struct
{
  const gsl_vector vector;
} _gsl_vector_const_view;

typedef _gsl_vector_const_view gsl_vector_const_view;

)

Greetings,
Fleur

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: gsl_vector_const_view: pointer to constant or constant pointer
  2002-08-09 21:20 gsl_vector_const_view: pointer to constant or constant pointer Fleur Kelpin
@ 2002-08-09 23:29 ` Fleur Kelpin
  2002-08-11  7:46 ` M Joonas Pihlaja
  2002-08-11 15:32 ` Brian Gough
  2 siblings, 0 replies; 6+ messages in thread
From: Fleur Kelpin @ 2002-08-09 23:29 UTC (permalink / raw)
  To: gsl-discuss

Hi again!

Obviously the point I missed is that there are no pointers being used so
moving the const inside the struct makes no difference at all.

Next time I'll drink my coffee first, then post to the list.

Greetings,
Fleur

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: gsl_vector_const_view: pointer to constant or constant pointer
  2002-08-09 21:20 gsl_vector_const_view: pointer to constant or constant pointer Fleur Kelpin
  2002-08-09 23:29 ` Fleur Kelpin
@ 2002-08-11  7:46 ` M Joonas Pihlaja
  2002-08-11 15:32 ` Brian Gough
  2 siblings, 0 replies; 6+ messages in thread
From: M Joonas Pihlaja @ 2002-08-11  7:46 UTC (permalink / raw)
  To: gsl-discuss

On Sat, 10 Aug 2002, Fleur Kelpin wrote:

> Hi all!

[snip code with gsl_vector_const_view]

> Am I missing something or can/could/should this be fixed?

IMHO, the 'const' structures in GSL should be avoided.  The basic
trouble is the constness-of-container vs. constness-of-containee
difficulty as you point out, punctuated by the fact that vector
views are themselves 'containers' for vectors (that in turn are
containers for the vector data itself).  C (and arguably C++,
without jumping through hoops) does not really facilite making
the difference.

Regards,

Joonas Pihlaja

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: gsl_vector_const_view: pointer to constant or constant pointer
  2002-08-09 21:20 gsl_vector_const_view: pointer to constant or constant pointer Fleur Kelpin
  2002-08-09 23:29 ` Fleur Kelpin
  2002-08-11  7:46 ` M Joonas Pihlaja
@ 2002-08-11 15:32 ` Brian Gough
  2002-08-12 12:05   ` Fleur Kelpin
  2 siblings, 1 reply; 6+ messages in thread
From: Brian Gough @ 2002-08-11 15:32 UTC (permalink / raw)
  To: Fleur Kelpin; +Cc: gsl-discuss

Fleur Kelpin writes:
 > Am I missing something or can/could/should this be fixed?
 > (I think this can be done by moving the const inside the struct:
 > 
 > typedef struct
 > {
 >   const gsl_vector vector;
 > } _gsl_vector_const_view;

It's not possible to initialise the struct element with this form in
ANSI C (although it is possible in gcc).  This is why the const'ing is
done at a higher level in a wrapper.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: gsl_vector_const_view: pointer to constant or constant pointer
  2002-08-11 15:32 ` Brian Gough
@ 2002-08-12 12:05   ` Fleur Kelpin
  2002-08-13 11:14     ` Brian Gough
  0 siblings, 1 reply; 6+ messages in thread
From: Fleur Kelpin @ 2002-08-12 12:05 UTC (permalink / raw)
  To: Brian Gough; +Cc: gsl-discuss

> It's not possible to initialise the struct element with this form in
> ANSI C (although it is possible in gcc).  This is why the const'ing is
> done at a higher level in a wrapper.

I found this out later, when I tried and considered some of the
alternatives and I think I discovered some of the the reasons for the gsl
vector setup.

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?

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.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: gsl_vector_const_view: pointer to constant or constant pointer
  2002-08-12 12:05   ` Fleur Kelpin
@ 2002-08-13 11:14     ` Brian Gough
  0 siblings, 0 replies; 6+ messages in thread
From: Brian Gough @ 2002-08-13 11:14 UTC (permalink / raw)
  To: Fleur Kelpin; +Cc: gsl-discuss

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;
}

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2002-08-13 18:14 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-08-09 21:20 gsl_vector_const_view: pointer to constant or constant pointer Fleur Kelpin
2002-08-09 23:29 ` Fleur Kelpin
2002-08-11  7:46 ` M Joonas Pihlaja
2002-08-11 15:32 ` Brian Gough
2002-08-12 12:05   ` Fleur Kelpin
2002-08-13 11:14     ` Brian Gough

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).