Thread: POSIX Threads and dynamic memory allocation

  1. #1
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515

    POSIX Threads and dynamic memory allocation

    I am using POSIX threads and in each thread, I allocated some data dynamically using the 'new' operator in C++. Also, I am using the standard template library. I am facing problems with the dynamic memory allocation part. My code keeps crashing randomly at places where I am allocating memory, deleting memory or manipulating string or other objects provided by the C++ STL. I tried finding out on the internet, but I can't find out for sure if functions like malloc() are thread-safe. I tried to backtrace using gdb and always, the trace ends with free() or malloc(). Sometimes I get a SIGSEGV and sometimes, I get a memory corruption error. What could be the problem?

    If there is a function which I pass as an argument to pthread_create, then does the function need to be reentrant? Is it fine to allocate memory dynamically in the function?

    Thanks.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. serial programming, threads and dynamic buffer
    By lectrolux in forum C Programming
    Replies: 1
    Last Post: 05-06-2003, 09:59 AM

Tags for this Thread