468,765 Members | 1,710 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,765 developers. It's quick & easy.

does having more variables increases the size of program.

does having more variables increases the size of program.
Aug 17 '08 #1
9 1826
On Sun, 17 Aug 2008 09:50:43 -0700, raashid bhatt <ra**********@gmail.com>
wrote:
does having more variables increases the size of program.
If they have linkage, yes.

Aug 17 '08 #2
On Aug 17, 9:50*am, Anand Hariharan <znvygb.nanaq.unevun...@tznvy.pbz>
wrote:
On Sun, 17 Aug 2008 09:50:43 -0700, raashid bhatt <raashidbh...@gmail.com>
wrote:
does having more variables increases the size of program.

If they have linkage, yes.
sorry i didnt understand
Aug 17 '08 #3
On 17 Aug, 17:50, raashid bhatt <raashidbh...@gmail.comwrote:
does having more variables increases the size of program.
bash-3.2$ for i in a b; do echo $i.c; cat $i.c; gcc -o $i $i.c; done
a.c
int main( void ) { return 0;}
b.c
int x;
int main( void ) { return 0;}
bash-3.2$ wc -c a b
12564 a
12580 b

From which one can conclude that *on this particular
implementation* the size of the executable is in
fact larger with the additional variable. This will
probably hold for many implementations.
Aug 17 '08 #4
On 17 Aug 2008 at 16:59, raashid bhatt wrote:
On Aug 17, 9:50*am, Anand Hariharan <znvygb.nanaq.unevun...@tznvy.pbz>
>On Sun, 17 Aug 2008 09:50:43 -0700, raashid bhatt <raashidbh...@gmail.com>
does having more variables increases the size of program.

If they have linkage, yes.

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.

Aug 17 '08 #5
Antoninus Twink wrote:
On 17 Aug 2008 at 16:59, raashid bhatt wrote:
>On Aug 17, 9:50*am, Anand Hariharan
<znvygb.nanaq.unevun...@tznvy.pbz>
>>On Sun, 17 Aug 2008 09:50:43 -0700, raashid bhatt
<raashidbh...@gmail.com>
does having more variables increases the size of program.

If they have linkage, yes.

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.
Typically, the .bss section occupies no space in the executable file and
the specified space is created and zeroed out at load-time.

<snip>

Aug 17 '08 #6
On 17 Aug 2008 at 18:25, santosh wrote:
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.

Aug 17 '08 #7
>does having more variables increases the size of program.

Probably, but it depends on what you mean by "size of program".
There are at least 3 interpretations of that:

(1) The length of the file containing the executable. (On UNIX, obtained
with ls -l, or on Windows, obtained with DIR).
(2) The value returned by the UNIX size(1) command, which includes
code, initialized data, and uninitialized data. Uninitialized data
is typically represented as a count of how large the section is,
but without needing to include a large block of zeroes in the file.
Having the OS clear the memory is usually more efficient than reading
blocks of zeroes from disk.
(3) The amount of memory taken when the program is running. This can
depend on user input, and includes automatic variables and
dynamically allocated memory (malloc() & friends).

Non-automatic initialized variables increase sizes (1), (2), and (3).
Non-automatic uninitialized variables increase sizes (2) and (3), but
typically not (1).

Automatic variables, initialized or not, require space depending
on the worst-case depth of what doesn't necessarily have to be a
stack.

Automatic initialized variables require code to initialize them,
which increases sizes (1), (2), and (3). They may also increase
size (3) for the variable itself.

Automatic uninitialized variables may increase size (3) for the
variable itself. Adding extra uninitialized variables to a function
which isn't on the worst-case path may not increase the total size
required for automatic variables unless it becomes part of the
worst-case path. Calculating the worst-case path is complicated
by recursive functions, which vary in their requirements.
Aug 17 '08 #8
santosh <sa*********@gmail.comwrote:
Antoninus Twink wrote:
On 17 Aug 2008 at 16:59, raashid bhatt wrote:
On Aug 17, 9:50*am, Anand Hariharan
<znvygb.nanaq.unevun...@tznvy.pbz>
On Sun, 17 Aug 2008 09:50:43 -0700, raashid bhatt
<raashidbh...@gmail.com>
does having more variables increases the size of program.

If they have linkage, yes.

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.

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
Aug 18 '08 #9
On 17 Aug, 18:41, Antoninus Twink <nos...@nospam.invalidwrote:
On 17 Aug 2008 at 16:59, raashid bhatt wrote:
On Aug 17, 9:50*am, Anand Hariharan <znvygb.nanaq.unevun...@tznvy.pbz>
On Sun, 17 Aug 2008 09:50:43 -0700, raashid bhatt <raashidbh...@gmail.com>
does having more variables increases the size of program.
If they have linkage, yes.
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.
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.
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.

--
Nick Keighley

Aug 18 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

31 posts views Thread by bilbothebagginsbab5 AT freenet DOT de | last post: by
8 posts views Thread by Will Chamberlain | last post: by
58 posts views Thread by Jorge Peixoto de Morais Neto | last post: by
4 posts views Thread by Hong Chen | last post: by
3 posts views Thread by raashid bhatt | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.