0
\$\begingroup\$

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?

\$\endgroup\$
5
  • 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 M
    Oct 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\$
    – Justme
    Oct 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 M
    Oct 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\$
    – Justme
    Oct 24, 2022 at 10:08

1 Answer 1

9
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.