Re: Reentrant functions and memory allocation



Eric Sosman <esosman@xxxxxxxxxxxxxxxxxxxx> writes:

CBFalconer wrote:
fmassei@xxxxxxxxx wrote:
I'm trying to put some old code in a portable, cross-platform
library and I'm facing a big problem: is there a standard,
platform-independent way to write a reentrant function that
allocate dynamic memory? In other words: is it safe to use
malloc() or free() in a thread-safe function?

No. Because the malloc system will be operating on it's own memory
areas, and is not re-entrant. You have to ensure that all accesses
to the malloc package are done through appropriate guards.

<off-topic>

For POSIX threads, malloc() et al. already contain whatever
guards are required, and you need provide no further protection
when calling them.

It's because of answers of this kind -- sort of right but
mostly not -- that it's been suggested you seek advice not here,
but on a forum where threading experts hang out.

</off-topic>

There are still people like Chuck there spouting nonsense. If you dont
know, dont answer.

.



Relevant Pages

  • Re: Reentrant functions and memory allocation
    ... mallocor freein a thread-safe function? ... to the malloc package are done through appropriate guards. ...
    (comp.lang.c)
  • Re: Reentrant functions and memory allocation
    ... Because the malloc system will be operating on it's own memory ... Posted via a free Usenet account from http://www.teranews.com ...
    (comp.lang.c)
  • Re: Reentrant functions and memory allocation
    ... independent way to write a reentrant function that allocate dynamic ... is it safe to use malloc() or freein a ... thread-safe function? ... know about threads but does know a little about reentrancy, and malloc/realloc/free are not required to be reentrant. ...
    (comp.lang.c)
  • Re: Reentrant functions and memory allocation
    ... If a function is thread-safe, then it can be executed more than ... once simultaneously without malfunctioning. ... malloc() must use locking, ... Think about malloccalled from a signal handler. ...
    (comp.lang.c)
  • Re: Is malloc() function provided in thread-safe?
    ... needed to make a malloc system thread-safe. ... restores that ability. ... Concurrency is part of my day to day work and has been for many, ...
    (comp.lang.c)