What happens if malloc()
fails in a microcontroller (e.g., due to not enough data memory freed)? Does the watchdog timer triggers a timeout response (e.g., reboot) or does malloc()
return a null pointer and the exception must be handled in software?
-
1\$\begingroup\$ You would have a dangling pointer its unknown what will happen without looking at your code. In large scale systems, that may effect system stability which broadly means programs not functioning correctly or the MCU may reset frequently. \$\endgroup\$– Amit MOct 24, 2022 at 7:59
-
2\$\begingroup\$ @AmitM Why would you have a dangling pointer if malloc returns null when there is no memory available? That's not what a dangling pointer means. \$\endgroup\$– JustmeOct 24, 2022 at 8:03
-
3\$\begingroup\$ @AmitM - Please don't answer questions in comments. That is not an allowed use of comments. (Also, when that answer is wrong, you cannot edit to correct it etc.) So, please stop doing that. Thanks. \$\endgroup\$– SamGibson ♦Oct 24, 2022 at 8:27
-
\$\begingroup\$ Justme, I meant a un assigned pointer. Not a dangling pointer. Sorry for wrong usage of that phrase. \$\endgroup\$– Amit MOct 24, 2022 at 8:31
-
\$\begingroup\$ @AmitM No, you will then have a pointer that is specifically assigned with null, so a null pointer. \$\endgroup\$– JustmeOct 24, 2022 at 10:08
1 Answer
It works identically on all plaforms according to C standard.
If there is no memory available for the amount requested, it will return a NULL pointer.
What happens after that is up to you.