4 ms·
> The auto feature is only enabled in C2X mode. In older modes, auto is a redundant storage class specifier which can only be used at block scope. Is it redund
by tpoacher 3y ago
> The auto feature is only enabled in C2X mode. In older modes, auto is a redundant storage class specifier which can only be used at block scope.
Is it redundant? I'm no c expert, but I remember several very specific cases where auto is necessary, and not using it leads to problems. Nested functions being the big one I remember.
The 'new' meaning for the 'auto' keyword doesn't seem compatible with this change. Won't these affect lots and lots of code that relies on nested function semantics?
- peterfirefly 3y agoNested functions are not C. It's a gcc extension but it is not used much. Many Debian/Red Hat packages that used it had it removed more than a decade ago because the implementation did things like stuffing code into the stack and executing it. When we began hardening our machines and disallowing that kind of behaviour, those packages either had to upgraded to not use that feature or be removed.
- uecker 3y agoMy software and Debian package uses nested functions. I use it because code quality improves a lot with nested functions. But yes, the executable stack is a problem. I had a patch for GCC to use function designators which would have removed this problem, but it was not accepted due to some backwards compatibility issues. I think a generic solution could be a new function pointer type.
- AdamH12113 3y agoNested functions are a GCC extension, not standard C. As far as I know, there's never been a reason to use the auto keyword in standard C.
- tpoacher 3y agoInteresting, I hadn't realised... So in standard c, all function prototypes and definitions must always appear at toplevel?
- kps 3y agoDefinitions, yes. Declarations can appear inside functions, but it's not common to see. int f(int x) { extern int g(int); return g(x + 1) - 1; }
- tpoacher 3y agoThis is doubly interesting, since the whole point of auto in the nested scenario is precisely to prevent the default "externness" of the declaration. But, if the definition itself cannot appear in the enclosing block scope in the first place without the gcc extension, then I suppose an auto keyword here would still be meaningless (if not misleading / dangerous possibly UB?).
- uecker 3y agoauto is not allowed for a function declaration with block scope without the GCC extension.
- AdamH12113 3y agoThat is correct, yes. You can limit the scope of a function to a single file using the static keyword; otherwise all functions are in the same global namespace for static linking. In practice, this is not a big limitation by itself. Although C has function pointers, it's not a functional programming language -- there are no closures, and you can't create new functions at run-time under any circumstances. (Not within the language itself, anyway. You'd need to embed a compiler, or at least a minimal code generator.) On Harvard-architecture systems like microcontrollers, code and data may be in different memories (ROM vs. RAM) with different access permissions, and some platforms even put them in entirely separate address spaces.