Why are malloc() and printf() said as non-reentrant?

后端 未结 6 1514
深忆病人
深忆病人 2020-11-29 19:25

In UNIX systems we know malloc() is a non-reentrant function (system call). Why is that?

Similarly, printf() also is said to be non-reent

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-29 19:52

    If you try calling malloc from two separate threads (unless you have a thread-safe version, not guaranteed by C standard), bad things happen, because there's only one heap for two threads. Same for printf- the behaviour is undefined. That's what makes them in reality non-reentrant.

提交回复
热议问题