Go Down

Topic: Source code of standard fonction (Read 680 times) previous topic - next topic

tuxduino


Is it possible, in my programs, to use variables I see in these source files?


Short answer: don't do it.

There's one thing that is called encapsulation. When you write a library, you design a "public" interface, i.e. a set of functions that a user of the library is supposed to call to perform whatever service your library provides. To achieve this, you might need some module-scoped(*) variables that can be accessed by the library code at your will without worrying about someone else tampering with them. If someone _does_ step in and modify those variable's value when he shouldn't, the library code might crash or produce wrong results.
Whatever you try to achieve by accessing a library-scoped variable via "extern" in your sketch is either doable by "properly" using the library or needs to be rethought.


(*) that is, variables that might be used by all functions in the library but that are not supposed to be visible outside it.

dammien

Thank you for all your answers,

I understand that I should not try to access directly these variables.

Why would I try to do that ?
For example  If I want to write my own "pseudo micros() function", in order to fulfill my specific requirements. It's just an example.

Concerning this specific time - question, I finished with using directly TCNT0, because I am on a very time-critical program and I realised that I can probably do it with 8bit variables, which saves me a lot of time on these little Arduinos :).

But I also ask the question in a more general way, and I thank you for the answers.  ;)


Go Up