qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Qemu-devel] [PATCH] const / static (against current CVS)


From: Jamie Lokier
Subject: Re: [Qemu-devel] [PATCH] const / static (against current CVS)
Date: Wed, 24 Aug 2005 21:35:40 +0100
User-agent: Mutt/1.4.1i

Paul Brook wrote:
> Contrary to popular belief the "const" qualifier on pointers has
> absolutely no effect on optimization. It's simply a debugging aid so
> the compiler will generate an error if you accidentally assign to
> it.

That's only true when the "const" applies to pointer targets, as in:

   const char * ptr = "foo";

It does not apply when the "const" applies to the pointer itself.
This puts the pointer "ptr" into RO storage:

   const char * const ptr = "foo";

Hence the large number of times that pattern occurs in the patch.

> It's perfectly legal to cast a (const char *) to a (char *) then
> dereference and write to it, provided the object the object it
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> points to is modifiable.
  ^^^^^^^^^^^^^^^^^^^^^^^

Precisely.  Global and static variables that are declared "const" are
not "modifiable", and are put in RO storage, so they cannot be written
to.  Taking their address creates a "const" pointer which must not be
derefenced and written to.

-- Jamie




reply via email to

[Prev in Thread] Current Thread [Next in Thread]