7

Does anyone know if free and malloc are threadsafe on visual C++ 2010?

I have having bizarre problems where memory is getting corrupted and im pretty much down to this being the only possiblity.

Does anyone know if the safety can be turned on and off and how?

myforwik
  • 73
  • 1
  • 3
  • 1
    I fairly confident it is - I'm assuming you're linking to the multi-threaded runtime. – Nick Jan 28 '11 at 09:07
  • Why are you using malloc and free in a C++ program ? – Paul R Jan 28 '11 at 09:46
  • Because its called visual C++ but it can also compile C. – myforwik Jan 28 '11 at 10:45
  • @Nick D: the answer you refer to is for Linux/POSIX systems. Visual C++ does not target POSIX systems at all. – MSalters Jan 28 '11 at 13:53
  • apparently in visual C++ all stdlib and stdio are thread safe, so long as you use the /MD compiler directive to use the multithread libraries. I tried to write colliding code but it would never collide and behaved the same when I added semaphores. – myforwik Jan 29 '11 at 23:41

1 Answers1

4

Provided you're linking with thread-safe libraries and using the correct flags, yes, malloc should be thread-safe.

If you think otherwise, you should post some code for us to examine.

I should mention that, in the vast majority of cases, it's never the libraries (which have been tested by uncounted millions of people). It's almost always your own code which, if you're thorough, has been tested by at most several dozen :-)

Not to say Microsoft (or any other large software house) doesn't ship bugs, just that they'd be discovered and fixed pretty damn quick.

paxdiablo
  • 772,407
  • 210
  • 1,477
  • 1,841