- What gives smaller code size ?
- Posted by Nadleira on April 29th, 2004
Hi
What will give smaller code size, when we have something like
char buf[32] appearing in many functions ?
1. Declare it in each function ("auto")
2. Declare it as global.
3. Declare it as global and static.
Thanks
Dan
- Posted by 42Bastian Schick on April 29th, 2004
Very vague question.
Missing: Compiler, CPU.
I'd say: Give it a try :-)
---
42Bastian
Do not email to bastian42@yahoo.com, it's a spam-only account :-)
Use <same-name>@epost.de instead !
- Posted by Hans-Bernhard Broeker on April 29th, 2004
Nadleira <dan_elbert@yahoo.com> wrote:
Wrong question. The given alternatives differ in semantics, rendering
whatever differences there may be in code size quite irrelevant.
The main effect they'll have is on data size, anyway, because they differ
in how many independent buffers you'll end up with.
Oh, and you missed one:
4. Declare it static in each function.
--
Hans-Bernhard Broeker (broeker@physik.rwth-aachen.de)
Even if all the snow were burnt, ashes would remain.
- Posted by CodeSprite on April 29th, 2004
dan_elbert@yahoo.com (Nadleira) wrote in
news:ef1d6d6d.0404290625.2ae154ba@posting.google.c om:
Well, an auto array will probably allow stack-pointer relative addressing,
which may save loading up a pointer to the global array.
*in general* - and this is entirely processor & compiler dependent - using
pointers into any array rather than indexes will cause more compact code to
be generated, which may outweigh any differences due to local/static/global
array instantiations.
Peter.
- Posted by Dave Moore on April 29th, 2004
"Nadleira" <dan_elbert@yahoo.com> wrote in message
news:ef1d6d6d.0404290625.2ae154ba@posting.google.c om...
Is there a way you can print out a memory map after you've tried it each
way? It'll be compiler dependent.
- Posted by Mark A. Odell on April 29th, 2004
In article <ef1d6d6d.0404290625.2ae154ba@posting.google.com>, "Nadleira"
<dan_elbert@yahoo.com> wrote:
4. static within block scope
Typically, 2, 3, & 4 are identical. I'm not sure how they could ever not be,
actually. So the question becomes does a stack based array access result in
smaller code than a statically allocated array (e.g. in .data or .bss)? The
answer to that can only be answered by you with your compiler and your CPU.
- Posted by Will on April 29th, 2004
dan_elbert@yahoo.com (Nadleira) wrote in message news:<ef1d6d6d.0404290625.2ae154ba@posting.google. com>...
Locals don't take up code space. I would avoid globals if I can, not because
CS101 says so, but because accessing globals adversely affects your
performance.
- Posted by Tauno Voipio on April 30th, 2004
Will wrote:
That's not so simple - take any AVR or 8051 compiler and try.
All decent compilers offer an opportunity to see the generated
assembly language code.
Accessing automatic variables (locals in stack) will be
more complicated because the processor instruction set
is not prepared well for register-based addressing.
With larger RISC processors (e.g. MIPS, SPARC, ARM) the
situation is different, as all memory addresses are register-
based anyway, and the stack/frame pointer is in a register
so it does not need to be loaded with the static variable's
address.
Tauno Voipio
tauno voipio @ iki fi
- Posted by Bill Davy on April 30th, 2004
Is your code multi-threaded? In which case sharing the buffer(s) may not be
safe. On the other hand, if the buffer "belongs to" something (like the
RS232 output port), then you might want to share it (carefully).
"Nadleira" <dan_elbert@yahoo.com> wrote in message
news:ef1d6d6d.0404290625.2ae154ba@posting.google.c om...
- Posted by moocowmoo on April 30th, 2004
"Nadleira" <dan_elbert@yahoo.com> wrote in message
news:ef1d6d6d.0404290625.2ae154ba@posting.google.c om...
Looks to me like a homework question.
Peter
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.659 / Virus Database: 423 - Release Date: 15/04/04