General Category > General Discussion

Overwriting fields in init struct.

(1/3) > >>

lerno:
In this article:

https://lwn.net/Articles/444910/

The following method is discussed for setting better defaults of vtables than having NULL in C for the Linux kernel:


--- Code: ---#define FOO_DEFAULTS  .bar = default_bar, .baz = default_baz
struct foo_operations my_foo = { FOO_DEFAULTS,
  .bar = my_bar,
};

--- End code ---

The usefulness here is that having a meaningful default method, the test for NULL can be avoided. So instead of:


--- Code: ---if (my_foo.bar != NULL) my_foo.bar();

--- End code ---

We can simply use:


--- Code: ---my_foo.bar();

--- End code ---

Since the default method is assumed to be correct for the situation.

Currently in C2, this would not be possible. While I understand the need to "error proof" things, I think this should not be forbidden in C2, and instead produce a warning that can be supressed using an @(override), like this:


--- Code: ---#define FOO_DEFAULTS  .bar = default_bar, .baz = default_baz
Foo_operations my_foo = { FOO_DEFAULTS,
  .bar = my_bar @(override),
};

--- End code ---

OR by setting a @(weak) on the default:


--- Code: ---#define FOO_DEFAULTS  .bar = default_bar @(weak), .baz = default_baz @(weak)
Foo_operations my_foo = { FOO_DEFAULTS,
  .bar = my_bar,
};

--- End code ---

bas:
This depends completely on the situation. For some file_operations in linux, this might be a good option, but for generic
code, there are no good 'defaults' imho. What C2 did change already is to guarantee that every global is always initialized
by default. This avoids some silly mistakes.

lerno:
Should the error on overwriting fields in init struct stay? Or just a warning that can be overridden?

bas:
Which unit test?

lerno:
/Init/init_field_designator_duplicate.c2

Navigation

[0] Message Index

[#] Next page

Go to full version