4 ms·
From an educational standpoint, C89 is a bit further from modern languages, so might be a better call on that basis. If you know C89 and some recent languages,
by dllthomas 7y ago
From an educational standpoint, C89 is a bit further from modern languages, so might be a better call on that basis. If you know C89 and some recent languages, you can probably roll forward to C99 or C11 without much trouble. If you started with C11, and haven't every worked in something from the 80s... well, that's an education.
> C99 block scoped variables free up stack space
I'm not sure that's actually true. Or at least if it is, I think it's the choice of a particular implementation. The compiler doesn't actually need to allocate anything until first use, and nothing prevents it from hoisting allocations earlier.
- Koshkin 7y agoIt's actually true - in the sense that at the end of the block the part of the stack frame used by the variables defined in it becomes available for reuse (which is normally done at compile time).
- dllthomas 7y agoHmm, I think I might've misread, but I interpreted what was being asked for the ability to mix declarations and statements, as C99 allows. If what's being asked for is simply the ability to declare variables at the top of blocks, I think that's allowed in C89? Certainly GCC and clang both allow it without warning with -std=c89 and -pedantic. I agree that there are cases where enclosing a variable in a block will allow earlier reuse of the memory that backs it.
- Koshkin 7y agoI don't think that the mixing of definitions and statement has any effect on how the stack frame space is (re)used: even if all variable definitions are collected at the beginning of the block, it may still be possible for the compiler to find the points inside the block after which some of the variables are no longer used while some others are brought into action; such variables could, then, share the same space despite having the same lexical scope.
- strbean 7y agoI misremembered my C89 gripe! The issue was that the following is not allowed: for (int i = 0; // ... Forcing you to write int i; for (i = 0; // ... The real annoyance here is that you can't restrict the scope of a variable you need initialized at the start of a for loop to the loop block. It's all fine and dandy when you are just using `i` (and never miss resetting it, or use it somewhere expecting it to be one thing and have it end up being another). When you have several complex loops in a function, it can be a bit aggravating. (Declaring variables willy nilly anywhere in a block is nice too)
- dllthomas 7y agoAh, yeah. There shouldn't be any runtime difference if you don't leak the address of `i` somewhere the compiler can't see, which should be uncommon for an iterator. But I agree it's awkward. You can wrap the whole thing in an extra block, so at least you don't have to hoist `i` to the top of the function. > (Declaring variables willy nilly anywhere in a block is nice too) You can always introduce another block, although it's not uncommon that that's harder on legibility than just hoisting.