GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 17-Dec-2004, 06:03
BobbyMurcerFan BobbyMurcerFan is offline
Member
 
Join Date: May 2004
Posts: 103
BobbyMurcerFan is on a distinguished road

Initializing All Array Elements to Zero vs. Other Numbers?


I would like to initialize all the elements of an array to a particular value. It seems that placing = {0} in the declaration will initialize ALL the array's elements to zero, not just the first one.

However placing a number other than zero in the braces initializes only the first element, and additional elements can be initialized by repeatedly tacking on a comma followed by a number.

Am I understanding this behavior correctly, that placing a single ZERO in braces initializes ALL array elements, but placing a single number other than zero in braces initializes ONLY the first element?

And is behavior part of the ANSI Standards for C & C++ or is it compiler specific?

Thanks very much!


CPP / C++ / C Code:
// Is This Correct & Does it Hold True for All C & C++ Compilers?
int aiMyArray[7] = {0};			// initializes ALL elements to 0.

int aiMyArray2[9] = {4};			// initializes ONLY the first element to 4.

int aiMyArray3[10] = {4, 5, 6};	// initializes the first four elements to 4, 5, 6 repectively.
  #2  
Old 17-Dec-2004, 08:26
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,311
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by BobbyMurcerFan
I would like to initialize all the elements of an array to a particular value. It seems that placing = {0} in the declaration will initialize ALL the array's elements to zero, not just the first one.

However placing a number other than zero in the braces initializes only the first element, and additional elements can be initialized by repeatedly tacking on a comma followed by a number.

Am I understanding this behavior correctly, that placing a single ZERO in braces initializes ALL array elements, but placing a single number other than zero in braces initializes ONLY the first element?

And is behavior part of the ANSI Standards for C & C++ or is it compiler specific?

Thanks very much!


CPP / C++ / C Code:
// Is This Correct & Does it Hold True for All C & C++ Compilers?
int aiMyArray[7] = {0};			// initializes ALL elements to 0.

int aiMyArray2[9] = {4};			// initializes ONLY the first element to 4.

int aiMyArray3[10] = {4, 5, 6};	// initializes the first four elements to 4, 5, 6 repectively.


It works like this (in the C and C++ standard):

If there are fewer initializers for an array than the specified size, the others will be zero.

So if you have this, all of the array elements are set to 0:

CPP / C++ / C Code:
int a[10] = {0};

If you have this, a[0] = 1, and all others are zero:

CPP / C++ / C Code:
int a[10] = {1};

If you have this, a[0] = 99, a[1] = -1234, and all others are zero:

CPP / C++ / C Code:
int a[10] = {99, -1234};

Regards,

Dave
  #3  
Old 17-Dec-2004, 12:55
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
The best way to initialize an array to a particular value is outside of its declaration. To my knowledge, looping is the best way to fill the array.

CPP / C++ / C Code:
for ( int x = 0; x < arraysize; x++ )
  array[x] = value;

or if you're going for something more efficient...

CPP / C++ / C Code:
register int x = arraysize;

while ( x-- )
  array[x] = value; // fills array in reverse order, but MUCH more efficient
__________________
-Aaron
  #4  
Old 17-Dec-2004, 13:22
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,311
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by aaroncohn
The best way to initialize an array to a particular value is outside of its declaration. To my knowledge, looping is the best way to fill the array.

CPP / C++ / C Code:
for ( int x = 0; x < arraysize; x++ )
  array[x] = value;

or if you're going for something more efficient...

CPP / C++ / C Code:
register int x = arraysize;

while ( x-- )
  array[x] = value; // fills array in reverse order, but MUCH more efficient


Like most, if not all, broad generalizations, it is simply incorrect to claim that the second example is "MUCH more efficient." Efficiency of loops depends on compiler implementations. Programmers, in my opinion, should write programs that are intuitively correct and simple from their point of view. If performance is an issue, then profiling the program after it is running with a realistic data set can point to bottlenecks. There is nothing wrong with either example; use whichever one seems best for you.

For your example, with Borland bcc32 (not the best, not the worst of c compilers), here is the code generated for the loop in your case number 1:

Code:
; for (x = 0; x < arraysize; x++ ) ; ?live1@48: ; EDX = arraysize, ECX = value xor eax,eax cmp edx,eax jle short @3 ; ; array[x] = value; ; ?live1@64: ; EAX = x, EDX = arraysize, ECX = value @2: mov dword ptr [ebp+4*eax-40],ecx inc eax cmp edx,eax jg short @2 ; ; return 0;

The loop itself is between the "@2:" label and the "jg short @2" instruction.

Here is the loop for your second example:

Code:
; while ( x-- ) ; array[x] = value; ; ?live1@48: ; EAX = x, EDX = value @2: mov dword ptr [ebp+4*eax-40],edx @3: mov ecx,eax add eax,-1 test ecx,ecx jne short @2 ; ; return 0;

The loop itself is between the "@2:" label and the "jne short @2" instruction.

Note that the loop in example 1 has four instructions while the loop in example 2 has five instructions.

Now, it is virtually impossible to tell which is really faster (due to pipelining and instruction caching), but whether example 1 is really faster or not, it is clearly incorrect to claim that your method number two is "MUCH more efficient."

(Other compilers and other versions of this compiler may have different results. Some compilers have optimization switches that make the compiler work a little harder to try to gain efficiency. Your Mileage May Vary)



Regards,

Dave
  #5  
Old 17-Dec-2004, 16:13
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
If I knew assembly myself, I probably could've told you those same things, but since I am still a novice programmer, I simply regurgitate what my teacher tells me without questioning its validity.
__________________
-Aaron
  #6  
Old 17-Dec-2004, 16:33
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,311
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by aaroncohn
If I knew assembly myself, I probably could've told you those same things, but since I am still a novice programmer, I simply regurgitate what my teacher tells me without questioning its validity.


A lot of things that are "common sense" are just plain wrong. Things that used to be are not necessarily true now. If I coded the equivalent of the "while" loop in z-80 assembly language it might be more efficient than a literal translation of the "for" loop.

Here's something I learned a long time ago in FORTRAN programming:

I had a routine where I wanted something simple: k = 4*j. Fast execution time was of paramount importance.

The most efficient code at that time on that computer was

Code:
k = j + j k = k + k

Why was this more efficient? Well, multiplication operations took about seven times as long as additions. Therefore, two additions were faster than one multiplication.

Nowadays, the expression k = 4*j in C (again using the Borland C compiler) results in a single instruction to shift left two places. That's right! The C compiler recognizes multiplication by a power of 2 to be arithmetically the same as a left shift by that power!!!

That's why I say: don't try to out-optimize the compiler until you have completely finished and you have determined (by profiling or some other means) where the bottlenecks are.

Regards,

Dave

"It's not what you don't know that hurts you, it's what you think is so that isn't!"

--Mark Twain
  #7  
Old 17-Dec-2004, 21:00
BobbyMurcerFan BobbyMurcerFan is offline
Member
 
Join Date: May 2004
Posts: 103
BobbyMurcerFan is on a distinguished road
Thanks a lot guys! It's been VERY educational and hepful .
 
 

Recent GIDBlogVista ?Widgets? on Windows XP by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting elements of arrays C++ the_crazyman C++ Forum 25 30-May-2008 07:27
Function and Array (w/ reference variables) question brookeville C++ Forum 15 07-Dec-2004 01:11
more array help (simulation project..) dilmv C++ Forum 6 17-Oct-2004 07:51
looking for a way to identify a repetitive sequence of digits in an array or string sho C++ Forum 8 14-Jun-2004 23:59

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:17.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.