Holmes: My dear, do not tell me where we are?
Shepherd: You are on a balloon !!!
Holmes: You must be a programmer.
Shepherd: Yes, but how did you guess?
Holmes: Only a programmer could give so accurate and
with such a useless response.
... excerpt from a famous joke
Sketch uses 1,090 bytes (3%) of program storage space. Maximum is 30,720 bytes.
Global variables use 21 bytes (1%) of dynamic memory, leaving 2,027 bytes for local variables. Maximum is 2,048 bytes.
text data bss dec hex filename
52136 1148 12076 65360 ff50 MyProject
Global variables use 21 bytes (1%) of dynamic memory, leaving 2,027 bytes for local variables. Maximum is 2,048 bytes.
int * buf;
void setup()
{
buf = new int[3000];
}
void loop() {}
int buf[300];
void setup()
{
buf[0] = 1; //Avoid optimizing this array
}
void loop()
{
}
cd C:\Users\GrafAlex\AppData\Local\Temp\arduino_build_55567
"C:\Program Files (x86)\Arduino\hardware\tools\avr\bin\avr-objdump.exe" -x -D -S -s StaticMem.ino.elf
…
00800100 l O .bss 00000258 buf
…
Sketch uses 456 bytes (1%) of program storage space. Maximum is 30,720 bytes.
Global variables use 6,009 bytes (293%) of dynamic memory, leaving -3,961 bytes for local variables. Maximum is 2,048 bytes.
...
Not enough memory; see http://www.arduino.cc/en/Guide/Troubleshooting#size for tips on reducing your footprint.
static int buf[300];
void setup()
{
buf[0] = 1; //Avoid optimizing this array
}
extern int buf[300];
void foo()
{
buf[0]++;
}
C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/main.cpp:47: undefined reference to `buf'
static int buf[300];
void foo()
{
buf[0]++;
}
0080036a l O .bss 00000258 _ZL3buf.lto_priv.12
00800112 l O .bss 00000258 _ZL3buf.lto_priv.13
namespace
{
int buf[300];
}
class A
{
static int buf[300];
public:
int * getBuf()
{
return buf;
}
};
int A::buf[300];
void setup() {}
void loop()
{
A a;
a.getBuf()[0] += 1;
}
00800100 l O .bss 00000258 A::buf
int getCounter()
{
static int counter = 0;
return ++counter;
}
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.println(getCounter());
}
00800116 l O .bss 00000002 getCounter()::counter
int * getBuf()
{
static int buf[300];
return buf;
}
int * getBuf()
{
static int buf[300] = {1, 2, 3};
return buf;
}
class Buf
{
public:
int buf[300];
Buf(int v)
{
buf[1] = v;
}
};
int * getBuf()
{
static Buf buf(1234);
return buf.buf;
}
class Singleton
{
int buf[300];
public:
static Singleton & getInstance()
{
static Singleton instance;
return instance;
}
int * getBuf()
{
return buf;
}
};
void setup()
{
Serial.begin(9600);
Singleton::getInstance().getBuf()[42] = 10;
}
void loop()
{
Serial.println(Singleton::getInstance().getBuf()[42]);
}
00800116 l O .bss 00000258 Singleton::getInstance()::instance
Source: https://habr.com/ru/post/459028/
All Articles