Skip to main content
20 events
when toggle format what by license comment
Jan 10, 2020 at 20:02 history edited jonk CC BY-SA 4.0
added 1609 characters in body
Jul 11, 2019 at 8:10 comment added jonk @JimmyB I'm not saying 'extern' is a problem. In fact, you and I agree on exactly how it isn't a problem. So long as you only define once, of course. I'm speaking there about why the author may have chosen a different path, using 'static' instead. That's all.
Jul 11, 2019 at 8:07 comment added JimmyB Re-read it now, the point I disagree is the paragraph starting with "Had they used 'extern' it might pose a problem." - extern would not have posed a problem, because it makes sure there is ever only one definition of the variable.
Jul 11, 2019 at 8:05 comment added JimmyB static instance of the variable would be allocated to its own, seperate memory location, which is avoided by extern.
Jul 11, 2019 at 8:05 comment added jonk @JimmyB If you read me closely, you should see where I say just what I think you are saying. Look closer to the end. Perhaps the third paragraph from the bottom?
Jul 11, 2019 at 8:01 comment added JimmyB Didn't really mean to criticize :) And after I re-read your answer I noticed that you really were the one to point out the different meanings of static. My point is just that what the static declaration (and definition) in the header file is used for in this case, i.e. referencing the same variable/memory location from multiple .c files, only works because of the (non-portable, non-standard) @ 0x06; address specifier, while the "proper" C way would be to use extern at the declaration(s) and a separate, single definition of the variable. Without the @ 0x06 address specifier, every
Jul 10, 2019 at 18:13 comment added jonk @JimmyB And yes, I am WELL aware of the difference between declarations and definitions -- especially as regards global variables and extern. Again, I don't understand your problem with my writing. But I'd love to improve what I wrote, which means I'd love to understand your criticisms (if that's what they are.)
Jul 10, 2019 at 18:12 comment added jonk @JimmyB I'll leave your comments for others to interpret. I've written a C compiler before. I know slightly more than many from that experience. But I am not sure what you are saying. It may be the time I have to spend trying to parse it, though. When I get a moment and the inclination, I may try and understand it. Regardless, I believe what I wrote remains accurate to my experiences and I wish I followed your motivations and comments.
Jul 10, 2019 at 11:31 comment added JimmyB "Technically, with 'extern' the rule of thumb would be that only ONE compilation unit would specify the value and the others should not." - This should make clear that the @ 0x06 is part of the definition of the variable, not its declaration. There can only be one definition of a (non-static) variable in a program, anything else yields an error at link time (or before). You can declare the (non-static) variable multiple times via extern, but there can only be one definition.
Jul 10, 2019 at 9:14 comment added JimmyB @jonk Indeed I believe you misunderstood me. Notice that we're not talking about variable declarations/definitions inside functions but global ones. Notice the different meaning of static for global variables as opposed to function-local variables.
Jul 10, 2019 at 9:10 comment added JimmyB @CortAmmon I think it's the "more proper" way because, in this case, the static way only works because it relies on the (non-portable) extension (@ 0x06;) which allows to allocate multiple (static) instances of a variable to a single memory location. Using extern that would not be a concern.
Jul 10, 2019 at 7:06 comment added Jeroen3 To add: Remember that the #include directive is the pre-processor, and this might indeed create multiple of PORTB in the entire compilation when not caught by the include guards. This is what counts as multiple occurances.
Jul 9, 2019 at 21:24 comment added Cort Ammon @JimmyB Extern linkage would definitely be possible, though I don't know if it's "more proper." One consideration is that the compiler may be able to emit more optimized code if the information is found in the translation unit. For embedded scenarios, saving cycles on every IO statement can be a big deal.
Jul 9, 2019 at 18:56 comment added jonk @JimmyB No, they could not have used 'extern', instead, for function variable declarations that behave as 'static' does. 'extern' is already an option for variable declarations (not definitions) within function bodies and serves a different purpose -- providing link-time access to variables defined outside of any function. But it's possible I'm misunderstanding your point, too.
Jul 9, 2019 at 18:54 history edited jonk CC BY-SA 4.0
added 718 characters in body
Jul 9, 2019 at 13:27 comment added jonk @Peter I think I said that. But perhaps not as well as you'd have liked?
Jul 9, 2019 at 12:47 comment added Peter Smith I would also note that a static variable declared within a function is not changed across function calls which can be useful for functions that need to retain some sort of state information (I have used it specifically for this purpose in the past).
Jul 9, 2019 at 9:21 comment added JimmyB Great answer, +1! Only one minor point: They could have used extern, and it would be the more proper C way of doing it: Declaring the variable extern in a header file to be inlcuded multiple times in the program and defining it in some non-header file to be compiled and linked in exactly once. After all, PORTB is supposed to be exactly one instance of the variable to which different c.u.'s can refer. So the use of static here is kind-of a shortcut they took to avoid needing another .c file in addition to the header file.
Jul 8, 2019 at 19:09 history edited jonk CC BY-SA 4.0
added 1253 characters in body
Jul 8, 2019 at 18:56 history answered jonk CC BY-SA 4.0