[Toybox] Any language lawyers here?

Frank Bergmann toybox at tuxad.com
Wed Feb 22 08:37:16 PST 2012


Hi Rob,

On Wed, Feb 22, 2012 at 07:35:22AM -0600, Rob Landley wrote:
[...]
> temp.c:3: warning: ‘blah’ initialized and declared ‘extern’
> 
> But if I just have extern into blah[] without the initialization, I get:
> 
> temp.c: In function ‘main’:
> temp.c:7: error: invalid application of ‘sizeof’ to incomplete type ‘int[]’

I stumpled over the same problem with my software ngtx. :-)

> The second of which at least makes sense.  The first is just stupid. I

No. I guess the compiler will use the "local" declared variable (a short
comparison of the object file should show it). IMHO that's the cause why
sizeof does work here.

In case you KNOW the size you must specify it here (making the usage of
sizeof senseless).
  extern char buf0space[READBUFSIZE+1];

Actually you can't use sizeof here.
C99 allows sizeof on variable length arrays (at runtime) but sizeof "shall
not be applied" to an incomplete type. Here's an example of variable
length usage:

$ ./sizeof 
2: 3
9: 10
$ cat sizeof.c 
#include <stdio.h>

size_t asize(int a) {
  char buf[a+1];
  return sizeof(buf);
}

int main(int argc, char *argv[])
{
  printf("2: %d\n", asize(2));
  printf("9: %d\n", asize(9));
  return 0;
}

> Is there some kind of "yes I know what I'm doing, shut up gcc"

Use a special function which use C99 sizeof at runtime like shown above?

Frank


 1329928636.0


More information about the Toybox mailing list