3 ms·
How does this _Alias feature differ from simply defining the local "aliased" name as a static const pointer-to-function (at file-wide scope)? Why wouldn't that
by zozbot234 5y ago
How does this _Alias feature differ from simply defining the local "aliased" name as a static const pointer-to-function (at file-wide scope)? Why wouldn't that work already?
- JonChesterfield 5y agoSyntactically distinct. &foo has different type for function vs pointer to function. Also makes life difficult for the optimiser; I think solvably as long as the new pointer is indeed static.
- sirwhinesalot 5y agoNot guaranteed to be zero-cost I guess?
- zozbot234 5y agoWhy wouldn't it be zero cost, if _Alias can be? (Note, the fact that the function pointer would be declared as "static const" at file scope is relevant in replicating the _Alias featureset.)
- sirwhinesalot 5y agoThere is an extra degree of indirection the optimizer has to get rid of. There is no extra pointer in the _Alias version. Whether this minor difference is relevant I'll leave to better informed people.
- zozbot234 5y agoIs there, though? AIUI there's no symbol created and no pointer when you declare a "static const int" at file scope. It's simple substitution, like a #define. So the question is why wouldn't a function label be the same.
- monocasa 5y agoThat's only an common optimization, not a guarantee, and it breaks down when you let that object or a pointer to that object leave the translation unit like you're suggesting.
- mananaysiempre 5y agoAmong other things, because the ways to bind to a dynamic symbol are multitudinous and frequently painful. For example, IIRC, ELF dynamic linking occasionally forces the compiler and/or linker through spectacular contortions in order to ensure two pointers to a single function compare equal (as required by the C standard and as happens with static libraries) even if the values being compared originate from two different shared objects, the function itself resides in a third one, and the compiler has no idea which functions are external to the shared object its output will be linked into. (Windows tells the standard to go take a hike, or more charitably doesn’t attempt to badly emulate the semantics of static linking in dynamic linking. I’m actually with Windows here, even if their executable format is a horrible underdocumented mess.) Those contortions are not required when the function is only ever called and never has its address taken, except compilers can be pretty naive about that part. Not that they have to be, but at least it’s not an obvious sure thing.
- LegionMammal978 5y agoThe main issue comes from code like this (https://godbolt.org/z/GY5baP18q https://godbolt.org/z/GY5baP18q): #include <stdio.h> typedef int int_func(int x); void print_result(int_func *func, int x) { printf("%d\n", func(x)); } int f(int x) { return x; } _Alias f_alias = f; static int_func * const f_ptr = f; void doesnt_work(int x) { print_result(&f, x); print_result(&f_alias, x); //print_result(&f_ptr, x); //error: incompatible pointer types } Since &f is of type int_func *, an alias to f must behave such that taking its address still results in an int_func *. Otherwise, existing code is broken, as shown above, since f cannot be replaced with f_ptr in the expression. This is what _Alias is meant to solve: it's perfectly transparent, in that the end-user has no way to distinguish whether the name comes from the original declaration or an _Alias. This allows libraries to modify the external name of a function, without breaking code that depends on the syntactic name of the function.
- mananaysiempre 5y agoNot saying it’s pretty or perfect, but you can make it work (https://godbolt.org/z/asW3zvxa9 https://godbolt.org/z/asW3zvxa9): #define f_deref_ptr (*f_ptr) /* ... */ print_result(&f_deref_ptr, x); Function types (as opposed to function pointer types), along with array types, are kind of shy in C, but it is possible to get hold of and use them.
- noobermin 5y agoI mean the usage sort of is similar but this is specifically about the labels of the functions in the binary (asm or instruction really) level which is in a way a bit of a lower level. Also, the caller which in this case are library users either need to know what the different implementations are which kinda could be harry and difficult for users, or the library would have a bunch of function pointers in memory that it would assign, meaning you'd have to init libraries and can't just call functions from the library which makes almost all libraries a little more heavy (even if it's a few instructions) on top of allowing a nice new vector for hackers to exploit (I'll just make the pointer point to "evil_strlen:").