Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 3 Mar 2002 11:05:00 -0500
From:      Brian T.Schellenberger <bts@babbleon.org>
To:        conrads@cox.net, freebsd-hackers@FreeBSD.ORG
Subject:   Re: A few questions about a few includes
Message-ID:  <20020303160501.6D87EBA03@i8k.babbleon.org>
In-Reply-To: <XFMail.20020303091938.conrads@cox.net>
References:  <XFMail.20020303091938.conrads@cox.net>

next in thread | previous in thread | raw e-mail | index | archive | help
On Sunday 03 March 2002 10:19 am, Conrad Sabatier wrote:
> Am I just completely stupid, or do we have a few things that could use a
> little cleaning up in /usr/include as well as in the man page for kvm_*?
>
> System: FreeBSD 4.5-STABLE
>
> 2) If compiling with the -pedantic switch, one might see something like
>    this:
>
> In file included from main.c:151:
> /usr/include/sys/proc.h:108: warning: ANSI C forbids zero-size array
> `ar_args'
>
> In <sys/proc.h>:
>
> /*
>  * pargs, used to hold a copy of the command line, if it had a sane
>  * length
>  */
> struct  pargs {
>         u_int   ar_ref;         /* Reference count */
>         u_int   ar_length;      /* Length */
>         u_char  ar_args[0];     /* Arguments */
> };
>
> This does indeed seem to make little or no sense.  Could someone explain
> this?  Is ar_args supposed to be a pointer or what?

I can explain this one; it's a moderately-common C programming technique.

This strucuture will be allocated dynamically.  I could be declared like

struct pargs { ... u_char *ar_args; }

and allocated like

pargs = malloc(sizeof(struct pargs));
pargs->ar_args = malloc(args_size);

but it's a lot more efficient to declare it as above and allocate it as

pargs = malloc(sizeof(struct pargs) + args_+size);

since pointers & arrays are equivalent, except that array data is at the 
location of the name rather than having a pointer to the data at that 
location, a [0] array has exactly the right semantics for this.  You can 
declare a [1] array for this to make ANSI happy, but then you have to 
subtract in the allocation and besides that the [0] is "self-documenting" for 
those who are familir with the technique--after all, there isn't anything 
*else* you can do with a [0]-sized array.

> 3) Furthermore, on including <sys/user.h>, one then sees this:
>
> In file included from /usr/include/sys/user.h:40,
>                  from main.c:153:
> /usr/include/machine/pcb.h:90: warning: struct has no members
>
> In <machine/pcb.h>:
>
> /*
>  * The pcb is augmented with machine-dependent additional data for
>  * core dumps. For the i386: ???
>  */
> struct md_coredump {
> };
>
> Nowhere under /usr/include is a more complete definition of md_coredump to
> be found.  This looks awfully "kludgy" to me.

A little guesswork here, but this seems pretty self-explanatory to me, too.
The md_coredump{} is a structure that includes the *extra* information 
(beyond what's already in the portable structure) for coredumps on a given 
architecture.  In the case of the 386, there is no useful extra information, 
so the structure is empty, but since there's a structure someplace that has 
an instance of struct md_coredump, it must be declared.  Since there's no 
useful content, it is declared as being empty.

Seems pretty elegant to me.

If I'm off-base here, somebody please jump in!

>
> I do hope someone can shed some light here.  Thanks.

[Your point #1 seems valid to me, though.]

-- 
Brian T. Schellenberger . . . . . . .   bts@wnt.sas.com (work)
Brian, the man from Babble-On . . . .   bts@babbleon.org (personal)
                                ME -->  http://www.babbleon.org
http://www.eff.org   <-- GOOD GUYS -->  http://www.programming-freedom.org 

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20020303160501.6D87EBA03>