delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2012/07/04/09:49:03

X-Recipient: archive-cygwin AT delorie DOT com
X-SWARE-Spam-Status: No, hits=-2.0 required=5.0 tests=AWL,BAYES_00,KHOP_RCVD_UNTRUST,KHOP_THREADED,MAY_BE_FORGED,RCVD_IN_DNSWL_LOW,SPF_NEUTRAL,T_RP_MATCHES_RCVD
X-Spam-Check-By: sourceware.org
Message-ID: <4FF449A6.5060102@cs.utoronto.ca>
Date: Wed, 04 Jul 2012 09:48:22 -0400
From: Ryan Johnson <ryan DOT johnson AT cs DOT utoronto DOT ca>
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20120614 Thunderbird/13.0.1
MIME-Version: 1.0
To: cygwin AT cygwin DOT com
Subject: Re: Differences between C++ 'new' operator and 'malloc()' (NOT a C/C++ question)
References: <8ef10a0d4a6912c0aa23bc33eebbe95d DOT squirrel AT zeusw DOT org>
In-Reply-To: <8ef10a0d4a6912c0aa23bc33eebbe95d.squirrel@zeusw.org>
X-IsSubscribed: yes
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com

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."

Ryan


--
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

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019