![]() |
|
|||||||
|
|
Thread Tools | Search this Thread | Rate Thread |
|
#1
|
|||
|
|||
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:
|
|||
|
#2
|
|||
|
|||
|
Quote:
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:
If you have this, a[0] = 1, and all others are zero: CPP / C++ / C Code:
If you have this, a[0] = 99, a[1] = -1234, and all others are zero: CPP / C++ / C Code:
Regards, Dave |
|
#3
|
||||
|
||||
|
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:
or if you're going for something more efficient... CPP / C++ / C Code:
__________________
-Aaron |
|
#4
|
|||
|
|||
|
Quote:
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:
The loop itself is between the "@2:" label and the "jg short @2" instruction. Here is the loop for your second example: Code:
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
|
||||
|
||||
|
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
|
|||
|
|||
|
Quote:
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:
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
|
|||
|
|||
|
Thanks a lot guys! It's been VERY educational and hepful
|
Recent GIDBlog
Vista ?Widgets? on Windows XP by LocalTech
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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