POSIX Threads and dynamic memory allocation

This is a discussion on POSIX Threads and dynamic memory allocation within the Linux Programming forums, part of the Platform Specific Boards category; I am using POSIX threads and in each thread, I allocated some data dynamically using the 'new' operator in C++. ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21