Dynamic sized global array

Status
Not open for further replies.

Epyon

Well-known member
Hi all

I would like to dynamically size a global array. The size is known at boot time and never changes during the program (so no heap fragmentation) and can also never be larger than a defined maximum (so no risk of running out of memory). However when I try to run the following code, it just gives me '1' for size.

Code:
int* myArray = 0;
int myArraySize = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("start");
  myArraySize = 20; //some routine will calculate this size in the real program
  if (myArray != 0) {
    delete [] myArray;
  }
  myArray = new int [myArraySize];
  Serial.println(sizeof(myArray)/sizeof(int));
}

void loop() {
  // Here we'll do something with the array

}
I'm using new in stead of malloc() (now supported in Arduino), but C-style malloc gives the same result. I'm using Arduino 1.6.10 with TD1.28b.
 
The sizeof function isn't actually a function -- It's a more like a processor directive. It only knows what the compiler knows at compile-time. Since this isn't an array at compile time, and is instead a pointer, you'll find that sizeof(myArray) == sizeof(int*).

You can only rely on sizeof() in this way for hard-coded array types; use your myArraySize variable in your code instead to determine the array length.
 
If the array is fixed you want to define it statically. Then keep track of how much it actually contains seperately

Code:
int myArray[MAXSIZE]
int numOfDataPoints = 0;

// Add data point to array
myArray[numOfDataPoints++] = newVal;

// Remove end data point from array
numOfDataPoints -= 1;
 
Hm, didn't know that. So there's not really a way to check the size of a dynamically allocated array? That makes it even riskier than I thought. Seems you can easily run into buffer overflows or nullpointers then.

@Xenoamor yes that's how I do it now (not a big fan of dynamic allocations actually), but I thought about some experimenting with dynamic allocation to simplify some coding.
 
Last edited:
new / delete does exactly the same as malloc / free on Teensy.

Hm, didn't know that. So there's not really a way to check the size of a dynamically allocated array?
If there is not enough memory, malloc / new will return NULL. Otherwise you know the allocation was successful. (There is no way to check the size of dynamically allocated memory blocks unless you hack the memory manager.)
 
Last edited:
Hm, didn't know that. So there's not really a way to check the size of a dynamically allocated array? That makes it even riskier than I thought. Seems you can easily run into buffer overflows or nullpointers then.

It's very safe as long as you do some basic checks -- new and malloc will always return either the full amount you requested OR a NULL pointer in the event of a failure. After that, you need to keep track of the array size (myArraySize in your example) yourself instead of relying on sizeof() to get it when needed -- This should present no more real effort.

Keep in mind, it's just as easy to write off the end of a static array as a dynamic one if you don't check before you write.
 
This got me thinking about how delete[] knows the size of the array.
Turns out there's a good discussion on it over here

The two methods commonly done by compilers are:
over-allocation
associative array

I try to use new[] as little as possible personally. I like to know exactly how much memory I'm using at compile time where possible
 
Last edited:
Status
Not open for further replies.
Back
Top