Earlier quoted context omitted.
>Did he make sure the behavior he mentioned is uniform across all ISO standards? I don't know. >What is his source for coming up with a certain answer to a certain piece of code? His conclusions are consistent with my understanding of the C standard. His justifications for his conclusions refer to specific rules regarding program behaviour, which suggests he's using the standard(s). >First of all, this gives a strong…
> ... MAY assume ... and therefore his claim: > ... Turns out, bar() is invoked even when x is the null pointer ... is incorrect (bar may or may not be invoked) and misleading for C newbies, without any mention of the subtleties of standards and implementations. Hence my original comment.
C Questions and Answers
91–100 of 135 posts
Re: C Questions and Answers
#92Although most comments use these examples to argue that C is a bad language, I would argue the opposite, that these examples show how C is an extremely useful language. C has occupied a niche position as the lowest commonly-used language that is both human-readable (at the level of expressing algorithms, data structures, functionality, and control flow) but not specific to any one instruction set architecture (and th…
You mean Algol, Mesa, and few others from the same vintage or even older?
Or rather Macro Assemblers like MASM, TASM that provided higher level macros for structured programming?
Re: C Questions and Answers
#93This reminds of me of the Quiz books that were popular years ago. They'd show some code that inadvertently tripped some obscure corner of the language. Rarely did the quizzes provide great insight. Rather, they confirmed the benefits of keeping your code idiomatic.
It's pretty much what pops into my head every time I see things like these. I got most of those correct, but my universal reaction was why the hell would I write something like that in the first place ?
Re: C Questions and Answers
#94Regarding the first answer: What is called a "tentative definition" is of course a "declaration".
Re: C Questions and Answers
#95Earlier quoted context omitted.
Meh, the problem is the preprocessor. A lot of really silly things are produced by macro expansion, so the "obviously silly" optimizations really do end up mattering.
That sounds interesting, but I'm suspicious. Do you have any examples of such macros and maybe some statistics or ideas how often they are actually seen in the wild?
[0] http://libcello.org/ [1] https://github.com/eatonphil/viola
Re: C Questions and Answers
#96Re: C Questions and Answers
#97Although most comments use these examples to argue that C is a bad language, I would argue the opposite, that these examples show how C is an extremely useful language. C has occupied a niche position as the lowest commonly-used language that is both human-readable (at the level of expressing algorithms, data structures, functionality, and control flow) but not specific to any one instruction set architecture (and th…
#2. Treating dereferencing a NULL pointer as undefined behavior means that the compiler is not required to generate additional instructions such as asserts or crashes to guard against potentially dangerous side effects. C compiler assumes that the programmer is in control of his/her code. In this example, it can be assumed that a careful C programmer has already guaranteed that the pointer will not be null when it is dereferenced. This C feature is an optimization to avoid generating redundant or unnecessary asserts or handling code.
3. C allows the programmer to handing pointers, allowing for such low-level optimizations that may not be possible in higher-level languages. A careful C programmer may have taken steps outside the function to handle the situation where yp==zp, or may otherwise be unconcerned about a particular case, for performance reasons.
4. A correct implementation of IEEE 754.
5. Since C is designed to efficiently compile to any computer architecture, it needs to be aware of the distinction between the arithmetic width of an instruction set vs the width of addresses. Ints are optimized to default to the natural arithmetic width of an instruction set (so that compilation doesn't produce unnecessary packing/unpacking instructions whenever they are accessed) but is guaranteed to be atleast 16 bits wide. However, since data structure can be as large as addressable memory, it is necessary for size_t to be the width of addresses.
6. Allowing size_t to be unsigned allows all bits of a size_t variable to be utilized for expressing size.
7. Undefined behavior is, again, an optimization feature, allowing each compiler to implement as it sees fit.
8. Comma operator is useful when first operand has desirable side effects, such as compactly representing parallel assignment or side effects in for loops.
9. C allows unsigned integers to wrap around 0 and UINT_MAX. This feature can be utilized as an optimization, for example as a free (no additional instruction) deliberate modulus operation. This is usually how unsigned integers behave in assembly.
10 & 11 & 12. Some ISA's, like MIPS, treat overflow of signed numbers as an exception. Others simply treat the result as a valid two's-compliment value. Since C is machine independent, C's official specification for overlow of signed numbers must be compatible for all ISA's. Simply treating the result as undefined does the trick, and means the compiler doesn't have to make guarantees or version for each ISA.
Re: C Questions and Answers
#98Although most comments use these examples to argue that C is a bad language, I would argue the opposite, that these examples show how C is an extremely useful language. C has occupied a niche position as the lowest commonly-used language that is both human-readable (at the level of expressing algorithms, data structures, functionality, and control flow) but not specific to any one instruction set architecture (and th…
> I have trouble imagining how to make something like C any lower without becoming architecture-specific or cumbered with details that are more economically-suited for a compiler. You mean Algol, Mesa, and few others from the same vintage or even older? Or rather Macro Assemblers like MASM, TASM that provided higher level macros for structured programming?
Algol is higher level than C.
Re: C Questions and Answers
#99Although most comments use these examples to argue that C is a bad language, I would argue the opposite, that these examples show how C is an extremely useful language. C has occupied a niche position as the lowest commonly-used language that is both human-readable (at the level of expressing algorithms, data structures, functionality, and control flow) but not specific to any one instruction set architecture (and th…
#1. Tentative definitions are historical baggage from Fortan Common blocks ( https://blogs.oracle.com/ali/entry/what_are_tentative_symbol... ), which may have helped adoption of C by Fortran users. Although it remains in the C language specification, this feature can be ignored. #2. Treating dereferencing a NULL pointer as undefined behavior means that the compiler is not required to generate additional instructions…
Re: C Questions and Answers
#100Earlier quoted context omitted.
#1. Tentative definitions are historical baggage from Fortan Common blocks ( https://blogs.oracle.com/ali/entry/what_are_tentative_symbol... ), which may have helped adoption of C by Fortran users. Although it remains in the C language specification, this feature can be ignored. #2. Treating dereferencing a NULL pointer as undefined behavior means that the compiler is not required to generate additional instructions…
Good comment, but I'd like to add that int is not really the "natural arithmetic width of an instruction set" any more. We will never see 64-bit ints. The sizes of int and long seem to be "whatever works, and is compatible with what it used to be".