Disadvantage of using namespaces

Using namespace local variables instead of global ones make code cleaner with virtually zero overhead in storage and memory usage but has the side effect that, for the verifier, those variables look like local variables (even if their scope is the entire duration of the program). Because of that, using these variables results in one always seeing more or less the same value for the Global variables use when the program is verified even when adding new variables.

Did anyone found a middle ground for this? Ideally I wanted to use the namespace-scoped variables but have them accounted as globals but any alternative that is not simply “just use globals” would be great.

Would local static scoping fit your bill? The variable then has a lifetime equal to the lifetime of the program, but is only in scope inside the block where it is declared.

This si what I have been doing (namespace-static at least). The problem is that it is not accounted for when the statistics are generated during sketch verification. Right now it says I have ~320 bytes free for local variables but this is a few bytes less than what I get with a very simple program.

But, maybe, I am just very good at optimizing memory usage. :wink:

The way the Arduino IDE reports code and memory usage is awful. It literally uses a regex to extract the (hoped) size info from text output. So ugly.

Can you give an example ino that reports the wrong data size and we’ll try to find a way to get a more reliable figure?

Well, at least it is not a regex over the source code. :slight_smile:

Well, this was me being stupid. The memory usage was increasing but as it was 1 or 2 bytes (and I am used to programming for computers that have hundreds of gibabytes of memory) and that and the fact that I always thought of the memory available as “around 300 bytes” ended up tripping me. Sorry for the noise.

1 Like