Christos, I think you may have overlooked the subtlety that pathv is *not* a const pointer - it just looks like one.
cat cast.c && echo --- && gcc -Wall -Wcast-qual cast.c #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { const char **p = (const char **) malloc(5); char *q = (char *)p + 3; printf("%p %p\n", p, q); return 0; } On Wed, Apr 10, 2013 at 1:30 PM, Christos Zoulas <chris...@zoulas.com>wrote: > On Apr 10, 11:54am, marti...@google.com (Martin Buchholz) wrote: > -- Subject: Re: RFR-8008118 > > | > 1. We did we switch from NEW() to xmalloc()? Why is the xmalloc cast > | > needed? > | > | NEW is for allocating homogeneous arrays, but here the memory block is > | being used for both chars and pointers. > > I did not know that. Why the cast though? xmalloc() returns void *, no? > Extraneous casts are bad because they hide conversion errors. For example, > if you don't have the xmalloc prototype in scope, without the cast you > get a warning of casting integer to pointer of different size. With > the cast you get the wrong data assigned to the pointer. > > | > 2. I would not declare pathv "const char **", but "char **", and then > | > cast the return if needed. This will make life easier in the future > | > if we decide to turn on warnings about const-castaways. > | > > | > > | I believe the current code doesn't cast away const and doesn't write to > | const. The only cast is to the return from xmalloc, which is expected. > | What might a compiler warn about? > > It is casting away const before the memcpy: > > + p = (char *) pathv + pathvsize; > > Try to compile with -Wcast-qual. > > christos >