|  | does having more variables increases the size of program. |  | |
| | | raashid bhatt |  |
| Posted: Sun Aug 17, 2008 4:50 pm Post subject: does having more variables increases the size of program. |  |
| does having more variables increases the size of program. |
| |
| | | Anand Hariharan |  |
| Posted: Sun Aug 17, 2008 4:50 pm Post subject: Re: does having more variables increases the size of program |  |
On Sun, 17 Aug 2008 09:50:43 -0700, raashid bhatt <raashidbhatt@gmail.com> wrote:
| Quote: | does having more variables increases the size of program.
|
If they have linkage, yes. |
| |
| | | santosh |  |
| Posted: Sun Aug 17, 2008 4:50 pm Post subject: Re: does having more variables increases the size of program |  |
Antoninus Twink wrote:
| Quote: | 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> |
| |
| | | raashid bhatt |  |
| Posted: Sun Aug 17, 2008 4:59 pm Post subject: Re: does having more variables increases the size of program |  |
On Aug 17, 9:50 am, Anand Hariharan <znvygb.nanaq.unevun...@tznvy.pbz> wrote:
| Quote: | 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 |
| |
| | | William Pursell |  |
| Posted: Sun Aug 17, 2008 5:03 pm Post subject: Re: does having more variables increases the size of program |  |
On 17 Aug, 17:50, raashid bhatt <raashidbh...@gmail.com> wrote:
| Quote: | 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. |
| |
| | | Gordon Burditt |  |
| Posted: Sun Aug 17, 2008 5:33 pm Post subject: Re: does having more variables increases the size of program |  |
| |  | |
| Quote: | 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. |
| |
| | | Antoninus Twink |  |
| Posted: Sun Aug 17, 2008 5:41 pm Post subject: Re: does having more variables increases the size of program |  |
On 17 Aug 2008 at 16:59, raashid bhatt wrote:
| Quote: | 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. |
| |
| | | Antoninus Twink |  |
| Posted: Sun Aug 17, 2008 6:56 pm Post subject: Re: does having more variables increases the size of program |  |
On 17 Aug 2008 at 18:25, santosh wrote:
| Quote: | 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. |
| |
| | | Richard Bos |  |
| Posted: Mon Aug 18, 2008 6:49 am Post subject: Re: does having more variables increases the size of program |  |
| |  | |
santosh <santosh.k83@gmail.com> wrote:
| Quote: | 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 |
| |
| | | Nick Keighley |  |
| Posted: Mon Aug 18, 2008 9:03 am Post subject: Re: does having more variables increases the size of program |  |
| |  | |
On 17 Aug, 18:41, Antoninus Twink <nos...@nospam.invalid> wrote:
| Quote: | 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.
| Quote: | 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 |
| |
|
|