It's a best practice to check if a request for dynamic memory allocation succeeded or failed. So, trying with a T3.2:
Using malloc() I get the expected result if there's insufficient memory:
Result (expected):
But, trying it with "new[]":
Result (unexpected):
Seems rather odd. I'd appreciate if anyone can shed light on what appears to be undefined behavior.
Thanks.
PS: T3.2, Arduino IDE v1.8.15, Teensyduino v1.55
Using malloc() I get the expected result if there's insufficient memory:
Code:
#include "Arduino.h"
void setup() {
Serial.begin(115200);
char *ptr = malloc(10000000);
Serial.printf("0x%08x: ", (uint32_t) ptr);
if (ptr != nullptr) {
Serial.println("Good Pointer");
} else {
Serial.println("Null Pointer");
}
}
void loop() {
}
Code:
0x00000000: Null Pointer
But, trying it with "new[]":
Code:
#include "Arduino.h"
void setup() {
Serial.begin(115200);
char *ptr = new char[10000000];
Serial.printf("0x%08x: ", (uint32_t) ptr);
if (ptr != nullptr) {
Serial.println("Good Pointer");
} else {
Serial.println("Null Pointer");
}
}
void loop() {
}
Code:
0x00000000: Good Pointer
Seems rather odd. I'd appreciate if anyone can shed light on what appears to be undefined behavior.
Thanks.
PS: T3.2, Arduino IDE v1.8.15, Teensyduino v1.55