public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: "John (Eljay) Love-Jensen" <eljay@adobe.com>
To: "gcc-help@gcc.gnu.org" <gcc-help@gcc.gnu.org>
Subject: Isn't new[] supposed to throw std::bad_alloc in this situation?
Date: Fri, 26 Feb 2010 19:42:00 -0000	[thread overview]
Message-ID: <4B7A6CC9992C4E4FB188D02872C90A6B134EC8@nambxv01a.corp.adobe.com> (raw)

Hi everyone,

Note:  32-bit application.  ILP32, LL64.

Isn't new[] supposed to throw std::bad_alloc in this situation?

- - - - - - - - - - - - - - -
int main()
{
  int size = 0x4000000A;
  int* p = new int[size];
  for(int i = 0; i < size; ++i)
  {
    p[i] = i; // Crashes here.
  }
}
- - - - - - - - - - - - - - -

The above loop is crashing sometime after p[9].  (On my system, it crashes on p[0x3FFAC].  Which is well after p[9] but long before p[0x40000009].)

My assumption is that the problem for this 32-bit toy app where sizeof(int) is 4, is that 0x4000000A * 4 ==> 0x100000028, which gets (0x100000028 & 0xFFFFFFFF) sliced to 0x28.

But, I presume, the size_t memory allocation request is being sliced to 0x28 before the new routine gets a chance to notice that the size * sizeof(type) is just too darn big.  I also presume that the p[0x3FFAC] crash is just happenstance to do how the heap is allocated from the OS, and the code is trashing vast tracks of the heap before running off the end into SEGV land.

My (naive?) expectations are that the new[] will throw a std::bad_alloc.

Is this code undefined behavior, or working-as-expected, or bug in GCC's C++ compiler (4.0.1, in this case), or a PICNIC error?

PICNIC - Program In Chair, Not In Computer

If it is a bug in the GCC C++ compiler (4.0.1), has it since been fixed in a more recent version?

Thanks,
--Eljay

# g++ crash.cpp

# ./a.out
Segmentation fault

# g++ --version
i686-apple-darwin9-g++-4.0.1 (GCC) 4.0.1 (Apple Inc. build 5490)

             reply	other threads:[~2010-02-26 15:26 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-02-26 19:42 John (Eljay) Love-Jensen [this message]
2010-02-27 11:31 ` Gabriel Dos Reis

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=4B7A6CC9992C4E4FB188D02872C90A6B134EC8@nambxv01a.corp.adobe.com \
    --to=eljay@adobe.com \
    --cc=gcc-help@gcc.gnu.org \
    /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).