X-Recipient: archive-cygwin AT delorie DOT com X-SWARE-Spam-Status: No, hits=0.2 required=5.0 tests=AWL,BAYES_00,FSL_RCVD_USER,KHOP_THREADED,RCVD_IN_DNSWL_NONE,RCVD_IN_HOSTKARMA_YE X-Spam-Check-By: sourceware.org Message-ID: In-Reply-To: <1341437446.12368.ezmlm@cygwin.com> References: <1341437446 DOT 12368 DOT ezmlm AT cygwin DOT com> Date: Thu, 5 Jul 2012 15:36:55 +0200 Subject: Re: Differences between C++ 'new' operator and 'malloc()' (NOT a C/C++ question) From: "Claude SIMON" To: cygwin AT cygwin DOT com Reply-To: sc DOT cygwin DOT com AT zeusw DOT org User-Agent: SquirrelMail/1.4.21 MIME-Version: 1.0 Content-Type: text/plain;charset=iso-8859-1 Content-Transfer-Encoding: 8bit Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner AT cygwin DOT com Mail-Followup-To: cygwin AT cygwin DOT com Delivered-To: mailing list cygwin AT cygwin DOT com Ryan Johnson wrote: > On 04/07/2012 5:45 AM, Claude SIMON wrote: >> When I compile the component with Visual C++, it works. When I compile >> the >> component with g++... it crashes. >> >> With 'gdb', I found that the problem happens when calling the 'malloc' >> function (as soon as the function is called, NOT when the returned >> allocated memory is used). When I replace the 'malloc' by a the C++ >> 'new' >> operator, the component compiled with Cygwin g++ doesn't crash anymore. > >> I thought that the C++ 'new' operator calls the 'malloc' function, but >> this seems not to be the case. As I want to use 'malloc'-like functions >> rather than the C++ 'new' operator, I wonder which functions are used in >> the C++ 'new' operator to allocate memory (and naturally which functions >> are used in the C++ 'delete' operator to free the memory). > Operator new() and malloc() are explicitly *not* interchangeable (for > many reasons, not least of which that the Standard says so). If you were > to free new'ed memory, or delete malloc'ed memory, the resulting heap > corruption could easily manifest as a crash the next time you tried to > allocate something... or it might just silently clobber data and lead to > "spooky action at a distance." > Thank you for the answer, but I am aware of this and my problem has nothing to do with it, nor, as stated in the subject, with having some lacuna in C/C++ programming. Let's try to be a little more explicit despite my poor English. Let's consider a Java native component which only calls a 'malloc(1)'. It doesn't even test the returned value (it is usually not a good idea, but it doesn't matter here). This component : - compiled with g++ under Linux : works, - compiled with g++ under Mac OS : works, - compiled with Visual C++ under Windows : works, - compiled with g++ under Cygwin : CRASHES ! It crashes as soon the 'malloc(1)' function is called. You don't even have the opportunity to test the returned value, nor to use it. It's perhaps a Cygwin bug, or perhaps a JVM/JRE/JDK bug ; I don't know and I don't bother (but if someone will make some investigation about that, I'm ready to help him or her if I can). When you replace the 'malloc()' by the 'new' operator, then the component compiled with g++ under Cygwin works too. The 'new' operator, among other things, allocates memory, as 'malloc()' does, but obviously it doesn't use 'malloc()' as it doesn't crash. So, because I can't use 'malloc()' in my Java native components, and because I doesn't want to use the 'new' operator, I wish to know which functions the 'new' operator uses to allocate memory, so I can use them in my Java native component so they would no more crash when compiled with g++ under Cygwin. -- Claude SIMON (sc DOT cygwin DOT com AT zeusw DOT org) -- Problem reports: http://cygwin.com/problems.html FAQ: http://cygwin.com/faq/ Documentation: http://cygwin.com/docs.html Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple