Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If I allocate memory in one thread in C++ can I de-allocate it in another

If I allocate memory in one thread in C++ (either new or malloc) can I de-allocate it in another, or must both occur in the same thread? Ideally, I'd like to avoid this in the first place, but I'm curious to know is it legal, illegal or implementation dependent.

Edit: The compilers I'm currently using include VS2003, VS2008 and Embedded C++ 4.0, targetting XP, Vista, Windows 7 and various flavours of Windows CE / PocketPC & Mobile. So basically all Microsoft but across an array of esoteric platforms.

like image 625
SmacL Avatar asked Mar 02 '10 14:03

SmacL


People also ask

Can two threads share the same memory?

In a multi-threaded process, all of the process' threads share the same memory and open files. Within the shared memory, each thread gets its own stack. Each thread has its own instruction pointer and registers.

How do you deallocate memory allocated by new?

New and Delete Operator In C++ when we want to allocate memory from the free-store (or we may call it heap) we use the new operator. int *ptr = new int; and to deallocate we use the delete operator.

Do multiple threads share the same heap?

The answer is simple: all threads in C share the same address space. This means all memory, the heap included, is shared between all threads.

What happens if you don't allocate memory in C?

Sometimes there just isn't enough memory, meaning that malloc isn't guaranteed to return a pointer to usable memory. If it isn't able to allocate memory, it will return a null pointer, NULL .


2 Answers

Generally, malloc/new/free/delete on multi threaded systems are thread safe, so this should be no problem - and allocating in one thread , deallocating in another is a quite common thing to do.

As threads are an implementation feature, it certainly is implementation dependant though - e.g. some systems require you to link with a multi threaded runtime library.

like image 104
nos Avatar answered Oct 12 '22 11:10

nos


There's nothing about new/delete themselves that prevent you from allocating and deallocating in separate threads. As many have said, the Standard is silent on multi-threading -- there is neither support for multi-threading, nor is there anything preventing you from doing it using any of the standard facilities. This is both good and bad in that you can do anything you want, but the language provides no direct mechanism to help you do it safely.

There are many potential technical issues you may need to contend with however. Many compilers have multi-threaded- and single-threaded flavors of the runtime libraries that implement new & delete, so you must be sure you use the right one. (VS 2008 has done away with the single-threaded CRT, so this is not an issue there.) More importantly, your software must be designed from the ground up to be multi-thread aware, and this is the biggest challenge for us. Resources need to be protected, ownership must be clear, and you need to avoid deadlocks & race conditions. But while this is probably the most important and difficult challenge you face when allocating and deallocating in separate threads, it is not directly related to your question, so I'll leave that for another discussion.

like image 42
John Dibling Avatar answered Oct 12 '22 11:10

John Dibling