Skip to content

Warning about clearing of uninitialized global variables on reset #11682

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
brycecherry75 opened this issue Oct 12, 2021 · 2 comments
Closed
Labels
Component: Documentation Related to Arduino's documentation content feature request A request to make an enhancement (not a bug fix) Type: Invalid Off topic for this repository, or a bug report determined to not actually represent a bug

Comments

@brycecherry75
Copy link

When I define global variables with no initial values, they are cleared after initiating a reset with power applied even though a value other than zero has been written to them by the sketch beforehand.
From what I see, this is caused by the Arduino bootloader which in my belief tests the RAM and/or clears its contents on reset.

@per1234
Copy link
Collaborator

per1234 commented Oct 12, 2021

When I define global variables with no initial values, they are cleared after initiating a reset

This is how C++ (and thus the Arduino programming language) is designed and required to work, so it's the correct and expected behavior with or without a bootloader:
https://en.cppreference.com/w/cpp/language/zero_initialization

Zero initialization is performed in the following situations:

  1. For every named variable with static or thread-local (since C++11) storage duration that is not subject to constant initialization, before any other initialization.

@per1234 per1234 closed this as completed Oct 12, 2021
@per1234 per1234 added Type: Invalid Off topic for this repository, or a bug report determined to not actually represent a bug Component: Documentation Related to Arduino's documentation content feature request A request to make an enhancement (not a bug fix) labels Oct 12, 2021
@matthijskooijman
Copy link
Collaborator

matthijskooijman commented Oct 13, 2021

FYI, it is libc (or maybe the gcc startup libraries), not the bootloader that overwrites these values. You can access the pre-initilization values by putting a function in a special section (e.g. init1) to let it run before various initialization steps (see e.g. https://www.nongnu.org/avr-libc/user-manual/mem_sections.html)

Alternatively (and easier), you can put your variable in the .noinit section, which prevents the variable from being initialized at all, e.g.:

uint8_t some_variable __attribute__((section(".noinit")));

Note that this is feature must be supported by the architecture (or really, the linker script). I know AVR-libc has it and Arduino_Core_STM32 also defines it in its linker scripts since a while, but I'm not sure about others.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Documentation Related to Arduino's documentation content feature request A request to make an enhancement (not a bug fix) Type: Invalid Off topic for this repository, or a bug report determined to not actually represent a bug
Projects
None yet
Development

No branches or pull requests

3 participants