does having more variables increases the size of program.

A

Antoninus Twink

sorry i didnt understand

Static variables will be stored in your executable, either in the .bss
section (if they're uninitialized) or in the .data section (if they're
initialized), so this will add to the size of your executable.

Automatic variables are just bits of the stack at runtime, while space
that you allocated dynamically with malloc() is produced on the heap at
runtime. So these variables don't take up any storage in your
executable.

On the other hand, obviously the code to move the stack pointer to
create space for an auto variable, and any code to initialize it, will
show up in your compiled program.
 
S

santosh

Antoninus said:
Static variables will be stored in your executable, either in the .bss
section (if they're uninitialized) or in the .data section (if they're
initialized), so this will add to the size of your executable.

Typically, the .bss section occupies no space in the executable file and
the specified space is created and zeroed out at load-time.

<snip>
 
A

Antoninus Twink

Typically, the .bss section occupies no space in the executable file and
the specified space is created and zeroed out at load-time.

Of course. Nonetheless, the existence of an extra variable needs to be
recorded in the executable.
 
R

Richard Bos

santosh said:
Typically, the .bss section occupies no space in the executable file and
the specified space is created and zeroed out at load-time.

Provided you have something called a ".bss section" at all; and anyway,
implementations are allowed to optimise in whatever way they see fit as
long as it doesn't break the semantics of correct programs, so it's
quite possible that, for example, two variables share the same space if
they're only ever used in separate parts of the program, or that, even
more likely, some variables are optimised out completely.
For example, for the two programs

#include <stdio.h>

int i;

int main(void)
{
printf("%1.1d\n", i);
return 0;
}

and

#include <stdio.h>

int main(void)
{
printf("0\n");
return 0;
}

I would not be surprised to find that the first compiles to a larger
executable with no optimisations, but exactly the same one as the second
with maximal space optimisation.

Richard
 
N

Nick Keighley

Static variables will be stored in your executable, either in the .bss
section (if they're uninitialized) or in the .data section (if they're
initialized), so this will add to the size of your executable.

this is platform specific (specificially Unix). Note the OP didn't
ask about size of executable but size of "the program". He may be
interested in runtime size. In which case automatic variables *may*
make a difference.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top