Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 7 of 7

Thread: Dynamic sized global array

  1. #1
    Senior Member Epyon's Avatar
    Join Date
    Apr 2013
    Location
    Belgium
    Posts
    443

    Dynamic sized global array

    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.

  2. #2
    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.

  3. #3
    Senior Member
    Join Date
    Nov 2015
    Location
    Wales
    Posts
    579
    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;

  4. #4
    Senior Member Epyon's Avatar
    Join Date
    Apr 2013
    Location
    Belgium
    Posts
    443
    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 by Epyon; 08-17-2016 at 04:37 PM.

  5. #5
    Senior Member
    Join Date
    Jan 2013
    Posts
    843
    new / delete does exactly the same as malloc / free on Teensy.

    Quote Originally Posted by Epyon View Post
    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 by tni; 08-17-2016 at 04:44 PM.

  6. #6
    Quote Originally Posted by Epyon View Post
    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.

  7. #7
    Senior Member
    Join Date
    Nov 2015
    Location
    Wales
    Posts
    579
    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 by Xenoamor; 08-18-2016 at 08:22 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •