0

I would like to know how Arduino allocate memory for pointers in function arguments.

For instance. I have this code that use 192 bytes (Arduino IDE compiler: Global variables use 192 bytes (9%) of dynamic memory).

char alph [] = {'a', 'b', 'c','\0'}; // 4 bytes
int numb [] = {1, 2, 3, 4}; // 8 bytes
float flo [] = {1.1, 2.2, 3.3}; // 12 bytes

void fn(){
  Serial.println(alph);
  Serial.println(numb[1]);
  Serial.println(flo[1]);
}

void setup()
{
  Serial.begin(9600);
  fn();
}

void loop(){}

The following code is the same of above, but I'm using pointer to variables in arguments, that use 234 bytes (Arduino IDE compiller: Global variables use 234 bytes (11%) of dynamic memory).

char alph [] = {'a', 'b', 'c','\0'}; // 4 bytes
int numb [] = {1, 2, 3, 4}; // 8 bytes
float flo [] = {1.1, 2.2, 3.3}; // 12 bytes

void fn(char * _alph, int * _numb, float * _flo){
  Serial.println(_alph);
  Serial.println(_numb[1]);
  Serial.println(_flo[1]);
}

void setup()
  Serial.begin(9600);
  fn(alph, numb, flo);
}

void loop(){}

There is a difference of 42 bytes to use three pointers. Why? A char pointer is 1 byte, a int pointer is 2 bytes and a float pointer is 4 byte length. Do the difference must be 7 bytes?

  • 1
    I make it 224 not 234. However, you're forgetting one simple thing: The compiler is clever. – Majenko Aug 14 '18 at 15:29
  • 1
    By the way: all pointers are 2 bytes, regardless of what they point to. – Majenko Aug 14 '18 at 15:32
  • They use 192 and 224 bytes ram. I can reduce the 224 to 192 for the second sketch by adding the keyword 'const' in front of all three array declarations. The 'const' keyword give the compiler more possibilities to do clever things. – Jot Aug 14 '18 at 16:06
  • But if add 'const' to arrays I can't change it dynamically – Mario J. Aug 14 '18 at 16:25
  • 1
    If you want to find out what happens you’d have to look at the assembly code the compiler produces. – Gerben Aug 14 '18 at 16:58
1

When you ask: "why?". The cheapest answer is: "why not?".

The compiler can do clever optimizations as @Majenko wrote. That is what this is about.

Even for such a small sketch, the compiler has many optimization methods to choose from. Changing a single line or adding a variable can make the compiler to choose other optimizations. It is therefor impossible to link the increased number of bytes to those extra pointers.

Sometimes the compiler can decide to keep a variable in a register instead of storing it in a memory location. Sometimes the compiler understands that a variable has a constant value and is used only once, then the compiler might not even use dynamic ram for it.

The used dynamic ram by the sketch is even more trivial, since Arduino sets the compiler to use the minimal size for the code.

Optimizing a sketch is best done with a large sketch. Your small sketch uses the Serial library, and that is the main part of the compiled code. Your own functions and pointers is just some code on the side.

For beginners, the compiler optimizations can be magical Abracadabra. However, using arrays will prevent the compiler to do extreme optimizations. There might even be a logical explanation for the number of increased bytes if the assembly code is studied as @Gerben wrote.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.