Correct Way To Dynamically Resize An Array - Using new and delete?

Hi, this is my first post so please let me know if I'm missing something.

I have an application where I need an array of variable size. It's okay if the contents of the array are lost while resizing.
Due to constrained size, I'm not using the std:vector Arduino library.

Currently I'm doing

int *myArr = new int [first_size]();
myArr = {assign values}

//// Use in Code ////

delete [] myArr;

myArr = new int [second_size]();

//// Cycle Continues ////

I'm not familiar with dynamic memory allocation on Arduino, is this method okay?
Could it lead to any problems over long term use?

Thanks!

peaboard:
I'm not familiar with dynamic memory allocation on Arduino, is this method okay?
Could it lead to any problems over long term use?

I don't know how to do it because dynamic memory allocation is a risky business when you have only 2K of SRAM and no supervisory operating system to stop silly things from happening.

Why not just allocate an array that is big enough for your needs and under-use it some of the time?

As there can only be one program running on an Arduino there is nothing that can make use of the memory that might be released.

...R

Yes that does make sense.

I've tested this out by running the program for a few hours with the array reset every ten minutes and it worked fine.

Still to avoid any problems, will be doing it the way you said using a bigger array.

Thanks!

Depending on the datatype you might save some memory by compacting the array.
E.g. if you only have values between 0..1023 10 bit integers should work

Check - Arduino/libraries/BitArray at master · RobTillaart/Arduino · GitHub

e.g. for 1000 10-bit numbers you need around 1250-1300 bytes (fit) instead of 2000 (won't fit)