Differences between C++ 'new' operator and 'malloc()' (NOT a C/C++ question)
Ryan Johnson
ryan.johnson@cs.utoronto.ca
Wed Jul 4 13:48:00 GMT 2012
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
More information about the Cygwin
mailing list