But it isn't really a "good catch" because it would break C_ASSERT.
C_ASSERT is intended for use outside of function scope like so:
**********************
Struct foo {
...
};
Struct bar {
...
};
C_ASSERT(sizeof(foo) >= sizeof(bar);
***********************
As is C_ASSERT is a global type definition which is fine in C.
With {}'s added, C_ASSERT becomes a global block of undefined type. C
won't allow you to have
{
Anything
}
At global scope without a keyword to indicate what the following block
contains. So it would
But it's not really a good catch. This change would break C_ASSERT for
its intended usage.
Take the following C file:
#define C_ASSERT(e) typedef char C_ASSERT[(e)?1:-1]
#define C_ASSERT_WITH_BRACES(e) {C_ASSERT(e)}
struct foo {
int i;
};
struct bar {
int i;
int j;
};
C_ASSERT(sizeof(struct bar) > sizeof(struct foo));
C_ASSERT_WITH_BRACES(sizeof(struct bar) > sizeof(struct foo));
Compiling this with cl.exe gives the following error:
C:\WinDDK\6000\src\umdf\usb\fx2_driver>cl foo.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.220
for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
foo.c
foo.c(14) : error C2449: found '{' at file scope (missing function
header?)
foo.c(14) : error C2059: syntax error : '}'
foo.c(14) : fatal error C1004: unexpected end-of-file found
you can't have a naked { } block at global scope.
-p
-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Pavel A.
Sent: Friday, January 05, 2007 8:27 AM
To: Windows System Software Devs Interest List
Subject: Re:[ntdev] C_ASSERT doesn't work in C files
A good catch! thanks!
-- Pavel
"Alexandru Pojoga" wrote in message
news:xxxxx@ntdev...
> Not sure if this is the right place to post...
>
> C_ASSERT works in .cpp but not always in .c files. The reason is its
> definition (from ntdef.h):
>
> #define C_ASSERT(e) typedef char C_ASSERT [(e)?1:-1]
>
> From what I understand, because C requires all declarations to be at
the
> beginning of scope, you can't have the following in a .c file (though
it
> works in .cpp):
>
> void f()
> {
> int x = 0;
> C_ASSERT(x==0); /* this works /
>
> x = 14;
> C_ASSERT(x==14); / this doesn't */
> }
>
> ... and the compile fails with a truly obscure error: C2143: syntax
error :
> missing ';' before 'type'
>
> The problem seems to be solved if we wrap the definition in braces:
>
> #define C_ASSERT(e) { typedef char C_ASSERT [(e)?1:-1]; }
>
> Just thought I'd mention it, since it's a useful feature but not
usable in
> its present state.
>
---
Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256
To unsubscribe, visit the List Server section of OSR Online at
ListServer/Forum