apr-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Greg Stein <gst...@lyra.org>
Subject Re: [PATCH] Tweaks to checked-in apr_getopt_long
Date Sat, 25 Nov 2000 18:57:17 GMT
On Fri, Nov 24, 2000 at 09:13:45PM -0600, Karl Fogel wrote:
> Applied (totally agree about apr_getopt_option_t, that's a better name).
> Given the interface we've decided on, the copying of argv seems
> inevitable (I suppose we could defer copying until we know we'll need
> to permute... but that's getting silly :-) ).

Well, the real reason to copy the pointer-array is that you simply don't
know if it is modifiable (when the array is the "true" argv). It appears to
be on Linux, but what about VMS? Or Windows? Or AIX?

[ the *array* is modifiable on Linux; the string contents are not ]
[ well, APR doesn't yet work on VMS that I know of, but I bet some poor VMS
  sap out there will do the work... ]

The other point about making it const gives us much more flexibility.
Consider the following:

static const * const cmdline[] = {
#define CMDLINE_ARGC (sizeof(cmdline)/sizeof(cmdline[0]))

I can pass the above to a "const char * const *" function. If the function
was declared simply as "char * const *", then I'd need to cast first. But
that wouldn't be safe because the function is saying "I might modify the
string contents". Of course, it better not because the compiler/linker put
cmdline into a read-only constant data segment.

Karl's "rule" about const-ness is also pretty nice here. Let's say that I
had the following declaration:

    char **cmdline;

I could pass the above to a function which was declared with any of the
following: "const char * const *", or "const char **", or "char * const *",
or "char **". The ones with "const" in them say they won't modify it (even
though they strictly could cuz of the char **), but that is safe.

Adding const is always safe; removing it can cause problems.


Greg Stein, http://www.lyra.org/

View raw message