Skip to content

Instantly share code, notes, and snippets.

@chrisportela
Last active December 18, 2015 17:39
Show Gist options
  • Save chrisportela/5819714 to your computer and use it in GitHub Desktop.
Save chrisportela/5819714 to your computer and use it in GitHub Desktop.
Tests to see how big of array the program can allocate.
int connectionEstablished;
int counter;
int *array;
void setup()
{
connectionEstablished = 0;
counter = 0;
Serial.begin(115200);
Serial.println("Initialize Channel");
do
{
if(Serial.available())
{
connectionEstablished = 1;
Serial.println("Serial Connection Established");
}
}while(connectionEstablished == 0);
pinMode(13, OUTPUT);
}
void loop()
{
digitalWrite(13, HIGH);
//delay(100);
makeArrayBigger();
digitalWrite(13, LOW);
//delay(100);
}
void makeArrayBigger()
{
array = (int *)malloc(sizeof(int)*counter);
if(array == NULL)
{
Serial.println("Malloc failed to allocate memory");
Serial.print("Malloc size: ");
Serial.println(sizeof(int)*counter);
delay(5000);
}
Serial.print("Counter: ");
Serial.println(counter);
counter++;
free(array);
}

#Results of test on Arudino Mega 2560:#

Initialize Channel
Serial Connection Established
[snip]
Counter: 3610
Counter: 3611
Counter: 3612
Counter: 3613
Counter: 3614
Malloc failed to allocate memory
Malloc size: 7230
Counter: 3615
Malloc failed to allocate memory
Malloc size: 7232
Counter: 3616
Malloc failed to allocate memory
Malloc size: 7234
Port closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment