c++ - Using a local short to pass a value into a function expecting an int; does this save memory? -
i'm on embedded system (for first time) , have whopping 512 bytes of memory. i'm bumping against barrier, , i'm looking save on each , every byte possible. such, following question:
in sdk, there's function, prototyped:
void foo(int val);
as such, main looked like:
void main() { int myval = 0; // stuff compute myval foo(myval); }
myval, however, never have value more ~100. saving memory @ doing instead?
void main() { short int myval = 0; // stuff compute myval foo(myval); }
edit: on architecture, ints 4-bytes, shorts 2-bytes. i'm unsure of whether using local short (or char, or whatever) save space since has up-cast meet foo(int) prototype.
i have no experience such small systems; following guess (the smallest ones worked had 384 k).
changing int
short
or makes compiler's optimizer work, , optimizer's output cannot predicted 100% accuracy.
your platform has sort of convention passing parameters functions (abi, documented compiler). guess stack aligned 4 bytes (the size of int
, should "natural" type platform). in case, if code uses stack passing parameter, there no difference in memory consumption.
however:
- if functions have 1 or few parameters, placed in registers , not on stack (arm first 4 parameters), there no memory consumption reduce
- if
main
function has 2short
local variables , not one, take 4 bytes of stack space,short
betterint
(andchar
, if has 8 bits, better) - if want send 2 parameters , not one, can stuff them
struct
; 2short
parameters take 4 bytes
ultimately, it's easy check kind of stuff. @ compiler's output (machine instructions) or tell compiler measure maximal depth of stack (gcc
can it; not sure compiler use).
Comments
Post a Comment