I'll wager a bet that he means "The most efficient" means by which to add a value to all items in an array.
Comatose
Taboo Programmer
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
If you want to set all elements to 0 then you can use memset(). Microsoft compilers implement that function in assembly because the chip has an instruction that will do it, and that is about as efficient as you can get. I don't know how other compilers implement it.
Also use the = {0} when declaring the array. int myarray[255] = {0};
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Comatose
Taboo Programmer
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
[] operator is the best to add values to an array, I think. Applicable only if you know the index in advance.
If you have to search in runtime for the position to which we can add a value, it will have a complexity of O(n). In that situation, if possible, use stl map.
kbshibukumar
Junior Poster in Training
65 posts since Jan 2009
Reputation Points: 12
Solved Threads: 8
> I know how to add values to an array. I am developing an application
> with RTAI, comedi and Qwt (plotting in realtime).
Oh please tell me you've used a profiler to work out where the real hot spots are, and not just picked random lines of code out based on guesswork.
Dickering about with an array of a few 1000 entries is nothing to the cost of plotting all those millions of pixels in your nice glossy user interface.
You just can't say "draw it" and hope that it takes zero time, because it won't. How you draw it will have a much larger impact on performance than what you're suggesting.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953