TL;DR
I have a class that contains an array. Sometimes I want to instantiate an object of that class in which the array has 100 members. Other times, I only want 10. The object is instantiated immediately when the program runs (at compile time, maybe?) and lasts the length the the program. How do to?
I have a RollingAverage class. Clearly, this class includes an array of samples to be averaged. Currently, this is hard coded to 100. An object can be initialized to use whatever portion of the total array is needed. The values in the array can be any type (it's a class template). This works well under extensive testing.
Now, the class is useful enough that I've begun using objects of it all over the place. Problem is, several of these uses need a rolling average of unsigned long, and only really need 10 samples for the rolling average.
So my problem is this: I simply want to be able to set the size of the array upon construction (probably as a parameter passed to the constructor). All of these objects are members of objects that are created on runtime. They will exists for the life of the program. I'm unclear whether that even counts as "dynamic memory allocation".
I am aware of solutions involving creating an array in outer class or setup() loop and passing it into the constructor as a reference or something....I don't really like that because it goes against encapsulation and could result in side-effects if the outer code goes messing with that array.
Please let me know if seeing any source code would be helpful.
I'm not sure I understand. sizeof returns the size of the current array, yes? What I'm trying to do is set the size of the array in the object's constructor.
IOW I'm not trying to determine the size of the array in the sense of finding out what it is, but rather in the sense of creating it with a given size that is determined by a parameter passed to the object's constructor.
Ah, that's brilliant. As a noob I would never have thought of that. I would then use arrayPointer the same way as I would use an array normally? e.g. arraypointer[3] = 5; sizeof(arrayPointer); etc.
Or do I have to dereference it each time?
Also, I presume I can name it something normal like sensorDataArray?
NO!
sizeof(arrayPointer) is 2 or 4 depending on the platform. You already know what the size is since you created the array. Just save that number in a variable.
I don't need the size of the array to be variable once created.
I just sometimes need a rolling average with 1000 samples; sometimes I only need 10. Want to define that at compile time. Solution from @gfvalvo works.
Btw, @gfvalvo, it took me some debugging to figure out that the array that's formed has values in the cells already. I have to do a loop to initialize each cell to zero.
Of course there must be some values there, memory can't be "empty". When allocated dynamically, an array of POD (plain old data) will initially contain whatever values happened to be in those memory locations before they were allocated to the array. Same as for a local variable in a function.
When allocated dynamically, an array of POD (plain old data) will initially contain whatever values happened to be in those memory locations before they were allocated to the array.
That makes sense. The values do get zeroed out when you declare an array in the "usual" way, however? i.e. int anArray[50]
Is there a more elegant way to zero out a dynamically allocated POD than a for next loop?
Ok, I was studying this, and thought I might understand it, and then I saw that the original poster had edited his/her suggestion and I'm lost again lol.
Is this using "Basic exponential smoothing" ? According to the wiki that's basically (sorry don't know how to do subtext here) St = S(t-1) + A(Xt - S(t-1)), where St is the current "average", Xt is the current data point being included in the average, and A is a smoothing factor that is 0 < A < 1.
Are you using 1/1024 as the smoothing factor A? Using the formula from the wiki, I don't understand where the extra multiplications, bitshifts and addidtions are coming from here.
Every cycle, you add a fixed fraction of the latest sample, to a running total minus that same fraction.
The smaller the fraction, the more sluggish the response of the filter.
std::array would work, since I don't need to dynamically resize it.
How does that work with the context I have though, where I need to set the size in the constructor of the object that has the array as a member?
Would it still be like gfvalvo's example, but with a different syntax for declaring the array?: