c++ - Pthreads and dynamic memory -
my thread routine looks this
void * dowork(void * args) { char* ptr = new char[25]; memset(ptr, 0, sizeof(ptr)); // operations ptr // if call delete[] ptr }
i've initialized 5 threads. questions,
- is thread safe?
- which thread owns memory?
- will
ptr
re-initialize every time new thread processesdowork
? if yes, happen memory allocated earlier? - what if
delete[] ptr
used @ end ofdowork
?
the ptr local pointer no other thread interfere long not communicate pointer thread.
two threads running function allocate 1 char[25] array each. thread not owner rather process owns it.
ptr
re initialize , old memory not deleted on thread join. if no delete used leak memory.delete[]
use yes.
to explain ptr
is allocated operative system , every call of new allocate new pointer operative system. value of ptr
ie points, local stack variable hence local thread , no other threads can value long not communicated.
Comments
Post a Comment