This is an archived copy of the Xen.org mailing list, which we have preserved to ensure that existing links to archives are not broken. The live archive, which contains the latest emails, can be found at http://lists.xen.org/
Home Products Support Community News


RE: [Xen-devel] mini-os: C programming

To: "PUCCETTI Armand" <armand.puccetti@xxxxxx>, "Keir Fraser" <keir@xxxxxxxxxxxxx>
Subject: RE: [Xen-devel] mini-os: C programming
From: "Petersson, Mats" <Mats.Petersson@xxxxxxx>
Date: Thu, 15 Mar 2007 16:22:36 +0100
Cc: xen-devel@xxxxxxxxxxxxxxxxxxx
Delivery-date: Thu, 15 Mar 2007 08:23:47 -0700
Envelope-to: www-data@xxxxxxxxxxxxxxxxxx
In-reply-to: <45F96225.9070104@xxxxxx>
List-help: <mailto:xen-devel-request@lists.xensource.com?subject=help>
List-id: Xen developer discussion <xen-devel.lists.xensource.com>
List-post: <mailto:xen-devel@lists.xensource.com>
List-subscribe: <http://lists.xensource.com/cgi-bin/mailman/listinfo/xen-devel>, <mailto:xen-devel-request@lists.xensource.com?subject=subscribe>
List-unsubscribe: <http://lists.xensource.com/cgi-bin/mailman/listinfo/xen-devel>, <mailto:xen-devel-request@lists.xensource.com?subject=unsubscribe>
Sender: xen-devel-bounces@xxxxxxxxxxxxxxxxxxx
Thread-index: AcdnFD3mNaX3eie4RNuUdxd8BLsT/AAAGmAw
Thread-topic: [Xen-devel] mini-os: C programming

> -----Original Message-----
> From: xen-devel-bounces@xxxxxxxxxxxxxxxxxxx 
> [mailto:xen-devel-bounces@xxxxxxxxxxxxxxxxxxx] On Behalf Of 
> Sent: 15 March 2007 15:12
> To: Keir Fraser
> Cc: xen-devel@xxxxxxxxxxxxxxxxxxx
> Subject: Re: [Xen-devel] mini-os: C programming
> Keir Fraser a écrit :
> > On 15/3/07 13:39, "PUCCETTI Armand" <armand.puccetti@xxxxxx> wrote:
> >
> >   
> >> extern char *stack;
> >>     
> >
> > Yes, this one is bogus.
> >
> >   
> >> 2. In file xen-3.0.3/extras/mini-os/gnttab.c:140: the 
> const variable
> >> gnttabop_error_msgs
> >> is declared as
> >>
> >> static const char *gnttabop_error_msgs[] = GNTTABOP_error_msgs;
> >>
> >> shouldn't that instead be declared:
> >>
> >> static const char * const gnttabop_error_msgs[] = 
> GNTTABOP_error_msgs;
> >>     
> >
> > It doesn't really matter, does it? Personally I hate 
> scattering 'const' all
> > over the place.
> >
> >  -- Keir
> >
> >
> >   
> It doesn't cause any crash probably, but according to the 
> semantics that you
> wish gnttabop_error_msgs to have, all pointers of this array 
> _should_ be 
> constant
> (I guess you're not going to change the error messages dynamically)
> and not only the strings refered by these pointers. The type of both 
> declarations
> is given by cdecl:
> $ cdecl
> char * const gnttabop_error_msgs[] ;
> declare gnttabop_error_msgs as array of const pointer to char;
> const char * gnttabop_error_msgs[];
> declare gnttabop_error_msgs as array of pointer to const char;

There are two different reasons to use const:
1. To tell the compiler to generate warnings when you change a const, e.g. 

const int x = 7;

x = 42;
Should give a warning about changing the value of a const. 

2. To give the compiler a clue to optimize code better. For example, the 
initial declaraton of x = 7 can be replaced by the constant value 7 by the 
compiler whenever it finds this as a more useful way to do things. Similarly, a 
const character array passed into a function can be expected to not have 
changed by the called function (so strcpy() for example isn't allowed to change 
the second parameter, as it's a const). 

I'm pretty sure most usages of const in Xen is for the purpose of the latter. 
Of course, getting a compiler warning when you've modified something that is 
supposed to be const isn't a bad thing. 

In the case of error messages, it doesn't really make much sense whether the 
compiler can generate the perfect code for it or not - it's going to take much 
longer to actually put it in a human readable place (such as the screen or 
serial port) than it takes to find the actual error message anyways. And I 
think this is what Keir ment with "it doesn't matter". 

Yes, setting the error message value for error 6 will not generate a compiler 
warning (as long as the pointer to char you set it to is a "constant"), but 
it's not a very likely thing to be done in the code anyways. 

> _______________________________________________
> Xen-devel mailing list
> Xen-devel@xxxxxxxxxxxxxxxxxxx
> http://lists.xensource.com/xen-devel

Xen-devel mailing list

<Prev in Thread] Current Thread [Next in Thread>