Earlier quoted context omitted.
> I don’t think I could ever sleep the same at night after writing something like that In these situations, you likely know your hardware and know your compiler, so you can actually provide an answer for 4 of the questions. The last one is a situation where someone should tell you not to get cute in the code review. I wrote C in telecom and finance and in both places we enforced a rule: when you define a structure, p…
> In some of our code, we would test the size of various types and structures on startup This is one of the things that C++ has actually improved a lot recently: doing this with static_assert is much nicer in terms of catching problems early... And yes, it's great for long-term maintainability.
So you think you know C? (2016)
141–150 of 344 posts
Re: So you think you know C? (2016)
#142Earlier quoted context omitted.
Further: Unless your code is compiled, deployed to a rocket, and fired off the Earth never to return, the question of “what is my platform?” is meaningless in the context of writing good C. So, today, using the compiler installed on your system right now, sizeof(int) = 32. Great. That means nothing, and changes nothing about whether your code is correct. You should not write code relying on it. Just like you should n…
>Unless your code is compiled, deployed to a rocket, and fired off the Earth never to return, the question of “what is my platform?” is meaningless in the context of writing good C. While I feel the tone of your comparison was intended to be a bit hyberbolic, the reality is a bulk of modern C development occurs in a context similar to the one you describe. Further the thought, utterly foreign to the vast majority of…
The author said he never did a full scale rewrite. He slowly migrated code from one platform to the next.
Today, Apple’s code runs on both ARM and x86 and with Marzipan, as will developers code. True most will be in Objective C, but some low level code is still in C.
Re: So you think you know C? (2016)
#143My score is 3/5. One immediate redflag I have noticed is using "int", "char", "short" as if they have a definite size. They don't. C standard only guarantees a minimum size. For example, many PDPs are 36-bit. Assuming the size of a variable is a common practice nowadays, but at least one should use uint8_t, int32_t, etc. from stdint.h. But I was still tricked, it should be obvious in hindsight, 12 years of schooling…
In school, I was taught to choose the "best answer" on a test or quiz, and if you don't know something, choose the answer that looks right to you. This test.. it reverses that entirely.
Re: So you think you know C? (2016)
#144Earlier quoted context omitted.
> compiler writers are basically taking the standard as gospel I would be rather disappointed if they didn't, honestly.
Consider the following statements: 1) The standard says I must do this, so I must do it. 2) The standard doesn't say I must not do this (but does allow me to either do it or not do it), so it's totally OK if I do it. I think you're thinking of cases covered by statement 1, and I think pretty much everyone agrees that compiler writers should behave that way for the standard to mean anything. The issues arise in cases…
Re: So you think you know C? (2016)
#145Earlier quoted context omitted.
> The code is undefined, but "I don't know." is still the correct answer for what happens to the variable. Well, then a better choice would be "I can't know."
Compile it, look at the assembly. You can know. The answer will vary from place to place, but it isn't non-existent.
This pretty much is the definition of "undefined behaviour" in the context of a standardized language specification.
Re: So you think you know C? (2016)
#146Earlier quoted context omitted.
Compile it, look at the assembly. You can know. The answer will vary from place to place, but it isn't non-existent.
> Compile it, look at the assembly. You can know. The answer will vary from place to place, but it isn't non-existent. This pretty much is the definition of "undefined behaviour" in the context of a standardized language specification.
Re: So you think you know C? (2016)
#147Pro tip: avoid `int`s as much as possible in your C code. Use defined macros instead.
There is a time and place for int32_t.
And time_t[0] is not one.
;-)
0 - https://pubs.opengroup.org/onlinepubs/9699919799/functions/t...
Re: So you think you know C? (2016)
#148Re: So you think you know C? (2016)
#149This is a perfect example of what I hate about some tests. Didn't quite know the purpose of the test, there's a difference between code for any machine and any compiler and gcc running on some vanilla x86, which is pretty common, and could have been the content (ex: you say it's undefined, that's obvious, everyone knows that already, but it's still deterministic... Here's a breakdown of what happens in practice, blah…
That's an assumption though. If it's undefined, the result may even depend on the order of some internal hash table which is affected by other code.
Re: So you think you know C? (2016)
#150Earlier quoted context omitted.
> Compile it, look at the assembly. You can know. The answer will vary from place to place, but it isn't non-existent. This pretty much is the definition of "undefined behaviour" in the context of a standardized language specification.
Whoa there! You mean “unspecified behavior”. int i = [unspecified] means that i has some value, but the spec doesn’t determine the value. Undefined behavior means that all your secrets might be sold to the highest bidder, your centrifuges might explode, and your computer is now full of ransomware.
That's only when using Boehm GC[0] in kernel device drivers that self-modify. Or any MSVC binary.
:-D