473,395 Members | 1,556 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Size of an executable for locally created arrays

Hi,

The following code snippet when compiled using gcc 3.3.5 creates an
executable which is 6.9 KB in size.

int main(){
int arr[1000000]={0};
long arr2[100000]={0};
double arr3[100000]={0};
}

However

int main(){
int arr[1000000]={1};
long arr2[100000]={1};
double arr3[100000]={1};
}

creates an executable which is 5 MB in size. Why is there such a huge
difference when we change the initialization value? In the former all
the elements are zero while in the latter case all except the first one
are zero. In another forum someone mentioned that in the latter case
the space for the arrays is allocated in the data segment while in the
former case it is not. Is that the case? If so can anyone brief me
about how the compiler decides where memory is allocated in such cases?
Do let me know if this a just compiler issue and not related to C++ per
se.

Thanks
Priya

Jul 28 '06 #1
3 1393
>
int main(){
int arr[1000000]={1};
long arr2[100000]={1};
double arr3[100000]={1};
}

creates an executable which is 5 MB in size.
I tried on VC 6 compiler. The exe is around 32KB for both 0 and 1
initializations.

Jul 28 '06 #2
Hello,

ag**********@gmail.com wrote:
cases? Do let me know if this a just compiler issue and not related to
C++ per se.
This is just a compiler issue, i.e. a quality of implementation issue.
E.g. gcc-4.1.1 compiles the code to around 7200 bytes in both cases.
The problem must have been known.

There are many ways for a compiler to establish the initialization. The
most important rule for the compiler is the as-if rule. The compiler
may do what it wants as long the code behaves like as if nothing
special had been done.

The most trivial one is doing it at compile time by putting all the data
as required into the executable into the data segment. This uses lots
of space in the executable but no extra code and no extra execution
time.

In the case of initialization to zero, it is possible to use the BSS
segment, which means that the memory is not used up in the executable,
but at execution time. The memory is taken from the OS initialized to
all bits zero, at least for most UNIX systems. This won't work when
initializing to something different from zero.

The last alternative for non zero-initialized data is producing code to
initialize the arrays at runtime using space allocated in the BSS. This
is what gcc-4.1.1 does when looking at the resulting assembler code.

There are even more opportunities if you think about combinations.

For arrays of that size, it is clear that allocating memory in the
executable would increase loading time a lot more then including a
little more code to do the initialization.

It is obvious, that a compiler has to decide by the size of the array,
the types involved, and the optimization objective (space or time) to
find the best among the possibilities. Most of the work done for
gcc-4.1.1 involves improvements in optimization. This is one field
where improvements were possible.

Bernd Strieder

Jul 28 '06 #3
Thanks a lot Bernd Strieder. That was exactly what I was looking for
:).

Regards
Priya

Jul 29 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Shailesh | last post by:
Metalink says that, whenever we migrate from dictionary managed to locally managed tablespaces, 'Migrated tablespaces are not subject to the UNIFORM/SYSTEM policy of newly created locally managed...
7
by: James Kimble | last post by:
I'm porting a Unix app that I developed to windows and I'm having a terrible time getting the bloody thing to work. I can compile just fine but when I run I get stack overflow errors. I started...
3
by: dk | last post by:
Hi all, Would appreciate some advice on the following: I am trying to speed up an Access database connected to a SQL Server back-end. I know I can use a pass-through query to pass the sql...
11
by: Walter Dnes (delete the 'z' to get my real address | last post by:
I've noticed a few threads (full of sound and fury, signifying nothing) here recently about allocation of large memory blocks. I'm about to start on a personal pet project where I'll be using...
6
by: AB | last post by:
Hello all, I heard once that there is a limitation on the number of lines of code that a program can have when using C, but not for C++. Is this true? Any ideas?
11
by: mast2as | last post by:
This question has been posted to this forum before and I read the thread but found that the answers were perhaps imcomplete, so I am trying again. Whenever I am creating objects I would like to...
4
by: ian.tiesdell | last post by:
Hi I have a database which has linked tables, none stored locally - it has a size when compacted of nearly 200Mb it comprises 21 queries, 26 forms and 2 reports - there are no embedded images...
6
by: bobby | last post by:
hi group, Does the header file size or number in include(s) effect the size of executable file? In other world if i chose a large header file and include it with my source file does it increase...
9
by: raashid bhatt | last post by:
does having more variables increases the size of program.
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.