Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: took out definition of static_assert

...

Code Block
bgColor#CCCCFF
borderStylesolid
#define JOIN(x, y) JOIN_AGAIN(x, y)
#define JOIN_AGAIN(x, y) x ## y
#define static_assert(e) \
typedef char JOIN(assertion_failed_at_line_, __LINE__) [(e) ? 1 : -1]
struct test{
 int a;
 char b;
 char padding_1, padding_2, padding_3;
 int c;
};
static_assert(offsetof(struct test, c) ==  (offsetof(struct test, padding_3) + 1) );
struct test arg = {.a=1,.b=2,.c=3};
arg.padding_1 = 0;
arg.padding_2 = 0;
arg.padding_3 = 0;
// perform operation on arg
.
.
// Copy arg to user space
copy_to_user(ptr, &arg, sizeof(arg));

The static_assert() macro accepts a constant expression e, which is evaluated as the first operand to the conditional operator. If e evaluates to nonzero, an array type with a size of 1 is defined; otherwise, an array type with a size of -1 is defined. Because it is invalid to declare an array with a negative size, the resulting type definition will be flagged by the compiler. The name of the array is used to indicate the location of the failed assertion, a feature of the C1X standard, accepts a constant expression and an error message. The expression is evaluated at compile time and, if false, the compilation is terminated and the error message is output.

See recommendation DCL03-C. Use a static assertion to test the value of a constant expression

...