Where the compiler puts on stack the default value, whenever the function call doesn't include it.
Guess that won't compromise with anything.
Where the compiler puts on stack the default value, whenever the function call doesn't include it.
Guess that won't compromise with anything.
So you could do foo({.bar=1}) and the rest would be default initialized.
struct bla_t {
int a = 23;
const char* hello = "Hello World!";
};
...and only allow comptime known constants for the default value declarations. struct bla_t {
int a;
const char* hello;
} blub = {
.a = 23,
.hello = "Hello World!",
}; struct bla_t {
int a = 23;
const char* hello = "Hello World!";
} blub = {
.a = 46,
};
The missing designated init item blub.hello would now be initialized to "Hello World!" by the compiler by looking up the default value in the struct declaration. Currently, missing designated init items are set to zero. And it would work in any other place where a bla_t is created: struct bla_t blob = {};
This would initialize blob to its default state of blob.a = 23 and blob.hello = "Hello World!".If I look at a piece of code that does
struct bla_t blub = {
.a = 46,
};
I know that everything except .a will be zero, without knowing anything about bla_t.With your proposal I would now get a 'helpful' default that might not be what I want at all.
Wait - no. What's wrong with
#define BLA_INIT_DEFAULT { .a = 23, .hello = "Hello World!", }
struct bla_t blub = BLA_INIT_DEFAULT;
blub.a = 42;
That's what your proposal would do under the hood anyway, just more explicit. #define BLA_DEFAULTS .a = 23, .hello = "Hello World!"
struct bla_t blub = {
BLA_DEFAULTS,
.a = 42,
};
...because C99 allows designated initializers to show up multiple times, but that's all a bit too much macro magic for my taste, I'd really prefer the defaults in the struct declaration. struct baz tmp = {.bar=1};
foo(&tmp);It would also free up zero as being an actual value instead of standing for 'default value'.
#define FOO_INIT_DEFAULT {.bar=1}
I don’t want some 'magic' struct that behaves different than all other structs on init.A static (default zero) struct lives in .bss, that entire section is zeroed on init.
If you want to have default values, it goes to .data where it will consume some ROM.
Also, once you initialize structs with values somewhere in the code (for instance with designated init), there's a high chance that the compiler will put a copy into the data section anyway, which is then memcpy'ed into the runtime struct (it depends on the compiler and compile options).
#include <stdio.h>
#include <stdlib.h>
struct params {
int a;
void *p;
};
void
f (struct params p)
{
printf ("p.a = %d, p.p = %p\n", p.a, p.p);
}
int
main (void)
{
f ((struct params){.a = 42});
f ((struct params){.p = f});
f ((struct params){});
exit (EXIT_SUCCESS);
}
Even though these are stack allocated, the missing fields are initialized to 0/NULL. The last case (no parameters) is new in C23. In C99 you had to use (struct params){0} to initialize it. https://en.cppreference.com/w/c/language/compound_literal int foo(int a, int b)
{
return a + b;
}
int main(void)
{
printf("Direct: %d\n", foo(1, 2));
int (*ptr)(int, int) = foo;
printf("Indirect: %d\n", ptr(1, 2));
return 0;
}
of course default arguments, if added, would have to be part of the function pointer type as well, making the above: int foo(int a = 1, int b = 2) // NOT REAL CODE, FANTASY SYNTAX
{
return a + b;
}
int main(void)
{
printf("Direct default: %d\n", foo());
int (*ptr)(int a = 1, int b = 2) = foo; // NOT REAL CODE, FANTASY SYNTAX
printf("Indirect default: %d\n", ptr());
return 0;
}
Unnamed function arguments would look silly (`int (ptr)(int = 1, int = 2)`?), but I would be radical then and only support default argument values for named arguments, probably.Edit: fixed a typo in the code, changed in-code comment.
So if a dynamically linked library uses default values, and you make use of them, and the dynamically linked library decides to change its default values (e.g. a crypto library switches to more secure defaults), you don’t get the update until you recompile your own code.
That doesn’t sound great.
Isn’t it C# which uses this strategy, of embedding the defaults in the caller?
PS: I don’t think C is defined in terms of stack, and modern calling conventions use registers for at least the first few arguments.