473,942 Members | 16,704 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1421
>
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**********@gm ail.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
3145
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 tablespaces. you should see "USER" value in the ALLOCATION_TYPE column for migrated tablespaces'. Oracle documentation says that parameters like INITIAL, NEXT , MINEXTENTS, MAXEXTENTS will not apply to locally managed tablespaces. Suppose I...
7
2931
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 taking things apart and found that the problem stems from the size of the arrays I'm declaring. These are arrays of structures and some of them are quite large (100,000 elements, it's an engineering app). I've been able to get things working by...
3
9276
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 code directly to SQL Server and speed things up. However, I want to be able to "filter" or place conditions on which records to retrieve based on LOCAL tables in Access.
11
24223
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 memchr(), memcmp(), memmove() a lot. Is there an ANSI C maximium size for character arrays which are guaranteed to succeed, assuming the machine has sufficient memory? And will the memxxx() functions work with that size? I'm looking at hopefully...
6
2729
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
2659
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 keep track of the memory which is used for each object (for stats purpose). They are 2 basic approaches, one is to write a Memory manager type of class where new & delete would be overloading the standard new & delete operator. Each time the new...
4
2876
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 and very little code on the forms does anyone have any suggestions as to why the file should be so big - i have other applications none of which exceed 15mb even with locally
6
3451
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 the size of the executable outcome at the end? thanks
9
2114
by: raashid bhatt | last post by:
does having more variables increases the size of program.
0
9969
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11531
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11120
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11298
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10661
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6087
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4909
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4510
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3511
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.