Reputation: 5204
I apologize in advance if I am using the incorrect terminology, I'm new to the C++ language. I have a class with a constructor that creates an empty buffer using malloc
LPD6803PWM::LPD6803PWM(uint16_t leds, uint8_t dout, uint8_t cout) {
numLEDs = leds;
pixels = (uint16_t *) malloc(numLEDs);
dataPin = dout;
clockPin = cout;
}
My understanding is that this creates an empty buffer with the length of whatever I pass to numLEDs
this is essentially a dynamically created array correct? I'm using malloc
because this code goes on an Arduino that has very limited memory and I want to avoid overflows and from what I have read, this is the best way to declare arrays is you don't know what size the array will be and you want to avoid overflow errors.
My question is, once this array has been created is there a faster way than a traditional for loop to fill the array with a single value. Very often I will want to do this and even microseconds make a difference in this application. I know that from the C++ standard library array classes have a fill
method, but what about an array declared in this way?
Upvotes: 1
Views: 3338
Reputation: 125007
My question is, once this array has been created is there a faster way than a traditional for loop to fill the array with a single value.
The C standard library provides memset()
and related functions for filling a buffer. There's also calloc()
, which allocates a buffer just like malloc()
, but fills the buffer with 0
at the same time.
Very often I will want to do this and even microseconds make a difference in this application.
In that case you might consider ways to avoid repeatedly allocating the array, which could take more time than filling an existing array. As well, the easiest way to make your code go faster is to run it on faster hardware. Arduino is a great platform, but Raspberry Pi Zero costs less ($5, if you can find them), has a LOT more memory, and has a clock speed that's 64x faster than a typical Arduino (1Ghz vs. 16MHz). Computing is often a tradeoff between good, cheap, and fast, but in this case you get all three.
Upvotes: 3
Reputation: 8451
You can still use std::fill
(or std::fill_n
), most standard library implementations will delegate to memset
for RandomAccessIterator (e.g. gcc and Clang). Trust in the standard library writers!
Upvotes: 1
Reputation: 522
Well, you can use memset from stdlib.h:
memset(array, 0, size_of_array_in_bytes);
Note however that memset works byte for byte,e.g it sets the first byte to 0 or whatever value you set as the second parameter, then the second byte and so on, which means that you must be careful.
Just a note: malloc gets its size as the size of arrays in bytes, so you might consider multiplying its parameter by sizeof(uint16_t)
Upvotes: 0
Reputation: 1090
You can use memset. But you have to be careful about the value you want to set. And you won't be much faster than using a for
loop. The computer needs to set all these values somehow! memset
may set larger contiguous memory spans and therefore be faster, but a smart compiler may do the same for a for
loop.
If you're really concerned about microseconds you need to do some profiling.
Upvotes: 0