I have an array pointed inside a class which changes after some random times. And after it changes I need to show its values to Serial monitor. But it only shows first value of array correctly, else are random.
class SomeClass
{
public:
byte* ArrayData;
uint16_t ArrayDataLength;
void UpdateArray();
};
SomeClass::UpdateArray()
{
int length = 15;//random value
byte dataBytes[length];
for (byte a = 0; a < length; a++)
{
dataBytes[a] = a;
}
ArrayData = dataBytes;
ArrayDataLength = dataLength;
}
SomeClass someclass;
void setup()
{
Serial1.begin(9600);
}
void loop()
{
someclass.UpdateArray();
delay(1000);
Serial1.println("- Data -");
Serial1.println("");
for(int a = 0; a < someclass.ArrayDataLength;a++)
Serial1.println(String(someclass.ArrayData[a]));
Serial1.println("");
Serial1.println("- Data End -");
}
Also, as a new byte array is being created each time inside UpdateArray(), should the old array set to pointer needs to be deleted to avoid memory leakage ? or it will be gone itself ?
Delta_G:
No it wouldn't. If you're going to allow some user to create an array in that function that is 15 elements large then you'd better have enough memory back to hold that array. If you can't use that memory for anything else then you haven't lost anything by actually reserving it for the thing you have to hold it for.
Well, what I want is to use that array and then clear it from the memory. If I use any pre-sized array, it will always be in memory and also limits the array size to certain value. If I choose very high value, it will be taking memory for no good reason. So it doesn't feel like right choice.
Having a pointer seemed good, set the array when need, and remove it after use. But it doesn't work.
As Delta_G pointed out, your method of assigning a pointer to an array that will essentially be immediately destroyed won’t work.
Aside from the academic exercise, why do you need to create this random-length array... to what end?
You could consider reserving a static array in the class sized to the maximum allowable. In that case, only one array gets created regardless of the number of class instances.
Delta_G:
Taking memory for no good reason? Think this all the way through. What were you going to do with this memory you save? If you don't have enough reserved for that array then when you try to create it in that function your Arduino will crash and reset. Is that what you're going for?
If you have to reserve that space anyway to keep the Arduino from crashing when you create the array, then you might as well have the space already allocated to the array. It's not like you could use it for anything else.
Agreed.
The only exception I can think of is if the code had two or more tasks that each required a large chunk of memory, but not at the same time. Then, you could use dynamic allocation to supply those chunks as needed. But the method that OP is trying to use is way wrong. I'd malloc() the space and free() it when the task is done.
Shouldn't get into fragmentation trouble as long as you only request one chunk at a time and return it before requesting the next one. Also very important to check the pointer returned by malloc() for NULL to ensure that you really got the space you requested.