From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 14229 invoked by alias); 5 Jul 2012 13:37:17 -0000 Received: (qmail 14054 invoked by uid 22791); 5 Jul 2012 13:37:16 -0000 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 Received: from smtp2-g21.free.fr (HELO smtp2-g21.free.fr) (212.27.42.2) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 05 Jul 2012 13:37:01 +0000 Received: from www.zeusw.org (unknown [82.231.68.224]) by smtp2-g21.free.fr (Postfix) with ESMTP id 420184B0083 for ; Thu, 5 Jul 2012 15:36:54 +0200 (CEST) Received: from localhost ([::1] helo=zeusw.org) by www.zeusw.org with esmtp (Exim 4.72) (envelope-from ) id 1SmmEp-0000fK-H7 for cygwin@cygwin.com; Thu, 05 Jul 2012 15:36:55 +0200 Received: from 192.168.5.10 (SquirrelMail authenticated user esc) by zeusw.org with HTTP; Thu, 5 Jul 2012 15:36:55 +0200 Message-ID: In-Reply-To: <1341437446.12368.ezmlm@cygwin.com> References: <1341437446.12368.ezmlm@cygwin.com> Date: Thu, 05 Jul 2012 13:37:00 -0000 Subject: Re: Differences between C++ 'new' operator and 'malloc()' (NOT a C/C++ question) From: "Claude SIMON" To: cygwin@cygwin.com Reply-To: sc.cygwin.com@zeusw.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@cygwin.com; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner@cygwin.com Mail-Followup-To: cygwin@cygwin.com X-SW-Source: 2012-07/txt/msg00061.txt.bz2 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.cygwin.com@zeusw.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