user48730: I'm trying to create a deepSleep class for my ESP32 project, but I get this error : section attribute not allowed for 'clock_beforeSleep' The only place it compiles, is outside the class.
How should I declare it ?
EDIT1: Add Code
#include "EEPROM.h"
#include "time.h"
#include "WiFi.h"

RTC_DATA_ATTR long clock_expectedWake = 0;
RTC_DATA_ATTR int bootCounter = 0;
RTC_DATA_ATTR int driftRTC = 0;
RTC_DATA_ATTR long clock_beforeSleep = 0;

class esp32Sleep
{
#define EEPROM_SIZE 16
#define DEV_NAME "ESP32lite"
#define uS_TO_S_FACTOR 1000000ULL /* Conversion micro seconds to seconds */

private:
  char sleepstr[250];
  // RTC_DATA_ATTR long clock_expectedWake = 0;
.
.
.


user7508: I think some explanation about the different allocation schemes could be
useful:

Global variables, as well as static locals and static class members,
are allocated at build time, by the linker, typically in the .data or
.bss section of the program.
Local variables are allocated at run time in the stack. Typically the
compiler issues some instructions to adjust the stack pointer in order
to make room for the variable. As an optimization, the most used
locals can be stored directly in CPU registers, but this optimization
is disabled if you use the address of the variable.
Variables created with new and malloc() are allocated at run time
in the heap. This allocation is handled by a support library.

Note that objects are variables and are allocated in one piece, just
like a C struct. Static class members are not part of the class
instances.
Now, some build chains have extensions that allow you to store
statically allocated variables elsewhere than the RAM. One example is
the PROGMEM storage on AVR. RTC_DATA_ATTR sounds like a similar
extension. You may be able to store a regular class member this way if:

The object is global and stored as RTC_DATA_ATTR as a whole.
It doesn't need a constructor.


user4143: You can't put an RTC_DATA_ATTR qualified variable inside a class because the class is stored in normal RAM. The RTC_DATA_ATTR qualified variable is, by definition, stored in RTC RAM.
The definition of RTC_DATA_ATTR includes a section attribute (__attribute__((section(".rtc.data")))) that tells the compiler where abouts in memory the variable should be stored (in this case the 8kiB of "slow" RTC memory). That is mutually exclusive with where the compiler decides to store the class.
It's like you have a diamond necklace which the has to be kept in a safe at all times, but you want to wear it around your neck whilst at the same time storing it in the safe. You can't do it.

48730: <p>I'm trying to create a deepSleep class for my ESP32 project, but I get this error : <code>section attribute not allowed for 'clock_beforeSleep'</code> The only place it compiles, is outside the class.</p>

<p>How should I declare it ?</p>

<p><strong>EDIT1</strong>: Add Code</p>

<pre><code>#include "EEPROM.h"
#include "time.h"
#include "WiFi.h"

RTC_DATA_ATTR long clock_expectedWake = 0;
RTC_DATA_ATTR int bootCounter = 0;
RTC_DATA_ATTR int driftRTC = 0;
RTC_DATA_ATTR long clock_beforeSleep = 0;

class esp32Sleep
{
#define EEPROM_SIZE 16
#define DEV_NAME "ESP32lite"
#define uS_TO_S_FACTOR 1000000ULL /* Conversion micro seconds to seconds */

private:
  char sleepstr[250];
  // RTC_DATA_ATTR long clock_expectedWake = 0;
.
.
.
</code></pre>
 7508: <p>I think some explanation about the different allocation schemes could be
useful:</p>

<ul>
<li>Global variables, as well as static locals and static class members,
are allocated at build time, by the linker, typically in the .data or
.bss section of the program.</li>
<li>Local variables are allocated at run time in the stack. Typically the
compiler issues some instructions to adjust the stack pointer in order
to make room for the variable. As an optimization, the most used
locals can be stored directly in CPU registers, but this optimization
is disabled if you use the address of the variable.</li>
<li>Variables created with <code>new</code> and <code>malloc()</code> are allocated at run time
in the heap. This allocation is handled by a support library.</li>
</ul>

<p>Note that objects are variables and are allocated in one piece, just
like a C <code>struct</code>. Static class members are not part of the class
instances.</p>

<p>Now, some build chains have extensions that allow you to store
statically allocated variables elsewhere than the RAM. One example is
the <code>PROGMEM</code> storage on AVR. <code>RTC_DATA_ATTR</code> sounds like a similar
extension. You may be able to store a regular class member this way if:</p>

<ol>
<li>The object is global and stored as <code>RTC_DATA_ATTR</code> as a whole.</li>
<li>It doesn't need a constructor.</li>
</ol>
 4143: <p>You can't put an <code>RTC_DATA_ATTR</code> qualified variable inside a class because the class is stored in normal RAM. The <code>RTC_DATA_ATTR</code> qualified variable is, by definition, stored in RTC RAM.</p>

<p>The definition of <code>RTC_DATA_ATTR</code> includes a <code>section</code> attribute (<code>__attribute__((section(".rtc.data")))</code>) that tells the compiler where abouts in memory the variable should be stored (in this case the 8kiB of "slow" RTC memory). That is mutually exclusive with where the compiler decides to store the class.</p>

<p>It's like you have a diamond necklace which the has to be kept in a safe at all times, but you want to wear it around your neck <em>whilst at the same time storing it in the safe</em>. You can't do it.</p>
