This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: how to cast away 'volatile'?


Ian Lance Taylor writes:
 > Andrew Haley <aph@redhat.com> writes:
 > 
 > > Matthew Woehlke writes:
 > >  > I have a code snippet that looks roughly like this:
 > >  > 
 > >  > extern void my_free(void* ptr);
 > >  > 
 > >  > int foo()
 > >  > {
 > >  >      volatile long * bar;
 > >  >      ...
 > >  >      my_free(bar);
 > >  > }
 > >  > 
 > >  > This generates the warning 'cast discards qualifiers from pointer target 
 > >  > type' at my_free() (gcc 3.4.3). Other than disabling the flag that 
 > >  > generates the warning (in general I want these warnings!), how do I 
 > >  > suppress this? my_free is of course a free() wrapper, i.e. I don't see 
 > >  > any problems discarding the qualifier at this point. I already tried 
 > >  > this, which seems like it used to work (I am moving the code from one 
 > >  > project to another):
 > >  > 
 > >  >      my_free(void*)bar);
 > >  > 
 > >  > ...and it doesn't help.
 > > 
 > > I think this is bad practice.  You'd be far better advised to malloc()
 > > the memory, keep a void* pointer to it, but cast to volatile long *
 > > when volatile is actually required.
 > 
 > Note that this is also somewhat dubious on a strict reading of the C
 > standard.  The standard discusss access to volatile qualified objects,
 > and it says that casting away volatile is undefined.  The standard
 > says absolutely nothing about volatile qualified pointers to
 > non-volatile objects.  So you should not expect any particular
 > semantics from a volatile-qualified pointer which points to a
 > non-volatile-qualified object.

Mmmm, but let's say you want a dynamic shared buffer between threads.
How else would you do it, other than a malloc() and a cast?

Andrew.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]