0

I'm on an embedded system (for the first time) and have a whopping 512 bytes of memory. I'm constantly bumping up against that barrier, and I'm looking to save on each and every byte possible. As such, the following question:

In the SDK, there's a function, prototyped:

void foo(int val);

As such, my main looked like:

void main() {
    int myVal = 0;
    // do stuff to compute myVal
    foo(myVal);
}

myVal, however, will never have a value more than ~100. Will I be saving any memory at all by doing this instead?

void main() {
    short int myVal = 0;
    // do stuff to compute myVal
    foo(myVal);
}

edit: On this architecture, ints are 4-bytes, shorts are 2-bytes. I'm mostly unsure of whether using a local short (or char, or whatever) will save space since it has to up-cast to meet the foo(int) prototype.

S. J.
  • 1,106
  • 1
  • 10
  • 23
  • http://stackoverflow.com/questions/589575/size-of-int-long-etc this may help you. You can also call `sizeof` method to check if there are any differences. – nervosol Jan 31 '14 at 18:46
  • 2
    I'd look at the generated assembly in both cases and compare. On some systems, the compiler may pass function arguments via registers. If that was the case, you could potentially be saving 2 bytes of stack space in `main()` (assuming a 4-byte `int`). – David LaPorte Jan 31 '14 at 18:47
  • For values that small, you could possibly even use a `char`. – Fred Larson Jan 31 '14 at 18:55
  • 1
    You could build your project both ways and then compare the section sizes in the .map files generated by the linker. – kkrambo Jan 31 '14 at 18:58
  • @nervosol: I know the structures have different sizes. :) Thanks though. I'm mostly unsure about whether the second implementation will save 2-bytes when it will have to cast to an int for the foo parameter anyway. – S. J. Jan 31 '14 at 20:48
  • In code this simple, the compiler should just clear the register where the first argument is passed and call `foo`; `myVal` should never exist in memory at all. – Eric Postpischil Jan 31 '14 at 20:53
  • So you are doing C++ on a machine with 512 bytes of RAM?? – starblue Feb 03 '14 at 20:36

1 Answers1

0

I have no experience with such small systems; so the following is just a guess (the smallest ones I worked with had 384 K).

Changing int to short or back makes the compiler's optimizer work, and optimizer's output cannot be predicted with 100% accuracy.

Your platform has some sort of a convention for passing parameters to functions (ABI, probably documented together with the compiler). I guess the stack is aligned to 4 bytes (the size of int, which should be the most "natural" type for your platform). In this case, if your code uses the stack for passing your parameter, there will be no difference in memory consumption.

However:

  • If your functions have 1 or a few parameters, they will be placed in registers and not on the stack (ARM does this for the first 4 parameters), so there is no memory consumption to reduce
  • If your main function has two short local variables and not one, they will take 4 bytes of stack space, so short is better than int (and char, if it has 8 bits, is even better)
  • If you want to send two parameters and not one, you can stuff them into a struct; then 2 short parameters will take 4 bytes

Ultimately, it's easy to check this kind of stuff. Just look at your compiler's output (machine instructions) or tell the compiler to measure the maximal depth of stack (gcc can do it; not sure which compiler you use).

anatolyg
  • 26,506
  • 9
  • 60
  • 134