Teensy hangs when realloc first argument is null

Status
Not open for further replies.

chow

Member
int* p = NULL;
p = (int*)realloc(p, sizeof(int));

The controller will hang when it hits this code. According to C90/C++98/C99/C++11 standards:

"In case that ptr is a null pointer, the function behaves like malloc, assigning a new block of size bytes and returning a pointer to its beginning."

This workaround functions just fine:
int* p = NULL;
if (p != NULL) {
p = (int*)realloc(p, sizeof(int));
} else {
p = (int*)malloc(sizeof(int));
}
 
Status
Not open for further replies.
Back
Top