public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: Ryan Johnson <ryan.johnson@cs.utoronto.ca>
To: cygwin@cygwin.com
Subject: Re: Differences between C++ 'new' operator and 'malloc()' (NOT a C/C++ question)
Date: Wed, 04 Jul 2012 13:48:00 -0000	[thread overview]
Message-ID: <4FF449A6.5060102@cs.utoronto.ca> (raw)
In-Reply-To: <8ef10a0d4a6912c0aa23bc33eebbe95d.squirrel@zeusw.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.

> 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

  reply	other threads:[~2012-07-04 13:48 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-07-04  9:45 Claude SIMON
2012-07-04 13:48 ` Ryan Johnson [this message]
     [not found] <1341437446.12368.ezmlm@cygwin.com>
2012-07-05 13:37 ` Claude SIMON
2012-07-05 20:05   ` Ryan Johnson
2012-07-06 11:48   ` NightStrike
     [not found] <1341539419.17571.ezmlm@cygwin.com>
2012-07-10 16:46 ` Claude SIMON
2012-07-11 19:47   ` Ryan Johnson
     [not found] <1341614850.23157.ezmlm@cygwin.com>
2012-07-10 16:51 ` Claude SIMON
     [not found] <1342037244.32732.ezmlm@cygwin.com>
2012-07-12 12:12 ` Claude SIMON
2012-07-12 14:17   ` Ryan Johnson
     [not found] <1342107001.13284.ezmlm@cygwin.com>
2012-07-12 16:00 ` Claude SIMON
2012-07-13  8:12   ` Csaba Raduly
2012-07-13  8:26   ` Al Slater
2012-07-13 13:03     ` Ryan Johnson
     [not found] <1342195424.30517.ezmlm@cygwin.com>
2012-07-18  8:03 ` Claude SIMON
2012-07-18  8:21 ` Claude SIMON
2012-07-18  8:42 ` Claude SIMON
2012-07-18  9:27   ` Eliot Moss
2012-07-18 10:23     ` Yaakov (Cygwin/X)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4FF449A6.5060102@cs.utoronto.ca \
    --to=ryan.johnson@cs.utoronto.ca \
    --cc=cygwin@cygwin.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).