User Tools

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revisionBoth sides next revision
user:embedded_c_code_doesn_t_have_to_be_ugly [2020/05/11 18:23] – [5. Globals] Igor Yefmovuser:embedded_c_code_doesn_t_have_to_be_ugly [2020/05/11 18:27] – [5. Globals] Igor Yefmov
Line 242: Line 242:
 A relic of K&R style ''C'' language, that required all the function variables be declared up-front, still finds its proponents even in the modern world of not-dumb-anymore-compilers that are able to deduce a lot more information from the code's text than some 50 years ago. A relic of K&R style ''C'' language, that required all the function variables be declared up-front, still finds its proponents even in the modern world of not-dumb-anymore-compilers that are able to deduce a lot more information from the code's text than some 50 years ago.
  
-These same compilers are capable of making quite intelligent choices on how to translate your code into zeroes and ones, so that you should truly be not afraid of expressing your functionality in the language of abstraction rather than writing in ''C'' like it is a macro-Assembler((I'm absolutely not advocating for total ignorance on how your ''C'' code translates into machine code, of course not. Just saying that one must **not** try to "help" compilers too much at expense of code's clarity)).+These same compilers are capable of making quite intelligent choices on how to translate your code into zeroes and ones, so that you should truly be not afraid of expressing your functionality in the language of abstractionrather than writing in ''C'' like it is a macro-Assembler((I'm absolutely not advocating for total ignorance on how your ''C'' code translates into machine code, of course not. Just saying that one must **not** try to "help" compilers too much at expense of code's clarity)).
  
 Stack allocation is extremely cheap, even on the most low-end down-to-earth hardware, so concerning yourself with the cost of that operation by grouping all-the-possible-function-variables together at the top should be reserved only for the most critical code((in which case it is very likely that such code should be written directly in Assembler by a highly-skilled engineer who is also extremely familiar with the target platform)). Stack allocation is extremely cheap, even on the most low-end down-to-earth hardware, so concerning yourself with the cost of that operation by grouping all-the-possible-function-variables together at the top should be reserved only for the most critical code((in which case it is very likely that such code should be written directly in Assembler by a highly-skilled engineer who is also extremely familiar with the target platform)).
  
 === Cross-function globals === === Cross-function globals ===
-Oh, these are absolutely the worst as they have a tendency on sneaking up on you at the least expected time and place! They get declared as globals and then are only set in one place while being accessed in plenty of other places. Especially dangerous ones cross thread boundaries, making for extra-nasty race condition type bugs.+Oh, these are absolutely the worstas they tend to sneak up on you at the least expected time and place! They get declared as globals and then are only set in one place while being accessed in plenty of other places. Especially dangerous ones cross thread boundaries, making for extra-nasty race condition type bugs.
  
 Even if one doesn't do anything about the first two kinds, one must always strive to eliminate these ones. In the long run it **never** makes sense to have that kind of (questionable) optimization benefit at the expense of destabilizing your code so badly. Even if one doesn't do anything about the first two kinds, one must always strive to eliminate these ones. In the long run it **never** makes sense to have that kind of (questionable) optimization benefit at the expense of destabilizing your code so badly.

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also, you acknowledge that you have read and understand our Privacy Policy. If you do not agree, please leave the website.

More information