Hello , I am developing an application in realtime and I need to update some values in an array. What is the best method to add a value to every position of an array?

Thank you for your help.

Recommended Answers

All 8 Replies

You're developing a realtime application, and don't know how to add values to elements of an array?????

Pull my other leg: it'll play a tune for you.

What is the criterion by which you would describe a method as "best"? If you are truly designing a realtime application, you will realise the significance of that question.

commented: Jingle bells, jingle bells, jingle all the way? +26

I'll wager a bet that he means "The most efficient" means by which to add a value to all items in an array.

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};

I'll wager a bet that he means "The most efficient" means by which to add a value to all items in an array.

I'd be betting on your side. But that just means he then needs to define the criterion for "The most efficient". For example, does it mean "fastest", "smallest number of machine instructions", "least memory consumption", "least heat output from the CPU" :)

He probably also needs to specify the type of array elements. Adding a value to elements of an array of strings might conceivably require a technique that differs from adding a value to elements of an array of int.

Incidentally, I despise irrelevant questions that put carts before horses. ;)

LOL

You're developing a realtime application, and don't know how to add values to elements of an array?????

Pull my other leg: it'll play a tune for you.

What is the criterion by which you would describe a method as "best"? If you are truly designing a realtime application, you will realise the significance of that question.

I know how to add values to an array. I am developing an application with RTAI, comedi and Qwt (plotting in realtime). Right know the application works ok but I need to improve efficiency. I need to add values (not setting to 0, add value to shift the coordinates) to an array of double (the x values). I need to do it in an efficient manner.

Thank you for your help.

[] 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.

> 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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.