This is the mail archive of the
mailing list for the Cygwin project.
Re: Differences between C++ 'new' operator and 'malloc()' (NOT a C/C++ question)
- From: Ryan Johnson <ryan dot johnson at cs dot utoronto dot ca>
- To: cygwin at cygwin dot com
- Date: Wed, 04 Jul 2012 09:48:22 -0400
- Subject: Re: Differences between C++ 'new' operator and 'malloc()' (NOT a C/C++ question)
- References: <firstname.lastname@example.org>
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.
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."
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).
Problem reports: http://cygwin.com/problems.html
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple