Use of "static" with "RTClib" I've got a question concerning the Arduino library "RTClib" by Adafruit and the use of the word "static".

Here you can see an excerpt from the example provided for the pcf8523 real-time clock:

#include "RTClib.h"

RTC_PCF8523 rtc;

void setup () {

  while (!Serial) {
    delay(1);  // for Leonardo/Micro/Zero
  }

  Serial.begin(57600);
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (! rtc.initialized()) {
    Serial.println("RTC is NOT running!");
  }
}

void loop () {
    DateTime now = rtc.now();

    Serial.print(now.year(), DEC);
    /*
    do some more stuff
    */
}


In the beginning of loop() they wrote:

DateTime now = rtc.now();


Thus, with every iteration of loop() "new" gets defined anew (on the heap?!), right?
This appears inefficient to me.

I thought about rewriting it this way:

static DateTime now;    // static declaration, executed only once
now = rtc.now();    // assignment to "now" every time loop() starts over


In this case "new" should be allocated on the stack like a global variable, right?

I want to use "static" to increase overall performance by reducing work for the processor and the heap (thereby avoiding heap fragmentation). Does this make sense or will it cause more problems than it solves? Keep in mind that I want to learn both proper programming for Arduino and proper coding in general.

Link to the library:
https://github.com/adafruit/RTClib

Thank you! :-)

EDIT: For some reason I CAN NOT declare DateTime now; in global scope or else the program won't run. Don't know why though. This is why I want to use static in the first place.
You wrote:


  DateTime now = rtc.now();

  
  Thus, with every iteration of loop() "now" gets defined anew (on the
  heap?!), right?


No. Local variables like this are usually allocated on the stack,
although the compiler may keep them in CPU registers if that helps
optimization.


  static DateTime now;    // static declaration, executed only once

  
  In this case "new" should be allocated on the stack like a global
  variable, right?


No. It is allocated statically, in the .bss section, like uninitialized
global variables.


  I want to use "static" to increase overall performance by reducing
  work for the processor


Not sure it will make much difference. Static allocation may save you a
few CPU instructions compared to stack allocation (the instructions that
move the stack pointer). On the other hand, it will cost you a few
instructions compared to register allocation (the instructions to access
the RAM).


  avoiding heap fragmentation


Stack allocation does not use the heap, and the stack never fragments.


  Does this make sense or will it cause more problems than it solves?


If you really care about sub-microsecond differences in the execution
time, you should either look and the disassembly of the program, or do
some benchmarking. Otherwise just write whatever makes more sense from
the point of view of the program's logic: does the variable need to keep
its value across calls to loop()? If the answer is “no”, do not use
static.