public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "kariya_mitsuru at hotmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/65350] New: [C++14] operator new[] should not be called if # of initializer elements exceeds # of elements
Date: Sun, 08 Mar 2015 16:49:00 -0000	[thread overview]
Message-ID: <bug-65350-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65350

            Bug ID: 65350
           Summary: [C++14] operator new[] should not be called if # of
                    initializer elements exceeds # of elements
           Product: gcc
           Version: 5.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: kariya_mitsuru at hotmail dot com

Please see the sample code below.

========================== sample code ==========================
#include <iostream>
#include <new>

void* operator new[](std::size_t size)
{
    std::cout << "my operator new[](" << size << ")" << std::endl;
    return ::operator new(size);
}

int main()
{
    int i = 1;
    try {
        int* p = new int[i]{ 1, 2 };
        delete[] p;
    } catch (const std::bad_array_new_length& e) {
        std::cout << e.what() << std::endl;
    }
}
========================== sample code ==========================
========================== output ==========================
my operator new[](4)
std::bad_array_new_length
========================== output ==========================

cf. http://melpon.org/wandbox/permlink/tQFp5fpPXT5mZu34

The C++14 standard 5.3.4[expr.new]/p.7 says,

  The expression in a noptr-new-declarator is erroneous if:

    ...

    --- the new-initializer is a braced-init-list and the number of array
elements
        for which initializers are provided (including the terminating '\0' in
a
        string literal (2.13.5)) exceeds the number of elements to initialize.

  ... Otherwise, a new-expression with an erroneous expression **does not call
  an allocation function** and terminates by throwing an exception of a type
  that would match a handler (15.3) of type std::bad_array_new_length
(18.6.2.2).
  ...

(emphasis mine)

So, I think that the sample code above should output only
========================== output ==========================
std::bad_array_new_length
========================== output ==========================


             reply	other threads:[~2015-03-08 16:49 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-03-08 16:49 kariya_mitsuru at hotmail dot com [this message]
2015-03-08 16:50 ` [Bug c/65350] " kariya_mitsuru at hotmail dot com
2015-03-13  3:51 ` [Bug c++/65350] " hubicka at gcc dot gnu.org
2015-03-13 10:15 ` redi at gcc dot gnu.org
2021-08-09 21:09 ` pinskia at gcc dot gnu.org
2023-09-07  5:17 ` gayathri.gottumukkala.27 at gmail dot com

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=bug-65350-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@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).