public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "burnus at gcc dot gnu dot org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug fortran/42958] Weird temporary array allocation
Date: Thu, 04 Feb 2010 19:33:00 -0000	[thread overview]
Message-ID: <20100204193256.20990.qmail@sourceware.org> (raw)
In-Reply-To: <bug-42958-10053@http.gcc.gnu.org/bugzilla/>



------- Comment #1 from burnus at gcc dot gnu dot org  2010-02-04 19:32 -------
> so, we check if the array-size is empty, ubound - lbound < 0.  In that
> case we set size to zero.  Otherwise we compute it
> as (ubound - lbound + 1) *8.
> Then we check whether size is negative and error out.
> Then we actually allocate max(1,size).

The reason for the setting it to zero is that Fortran allows one to allocate a
zero-sized array:  ALLOCATE ( Array(0:-2) )

The reason for  MAX(1, size)  is to make  ALLOCATE(A(1:0)); if(ALLOCATED(A))
work. (This was added later than "size < 0 ? 0 : size".)

Why there is a negative check? Well, I do not know. I also can speculate about
a poor man's overflow check, which sometimes indeed works, but often fails.

 * * *

> Why not simply do
>   size = (ubound - lbound + 1) * 8;
>   malloc (size);

I think that would be the future: With the array descriptor (dope vector)
reform, we will have an "allocated" field and thus one can do:

   descriptor.allocated = true
   descriptor.data = malloc (max (0,size))

Where "if(allocated(A))" translates into "if(A.allocated)".

Actually, maybe one should better use:
   size = (ubound - lbound + 1)
   if (size > 0)
     {
       descriptor.data = malloc(size)
       if (descriptor.data == NULL)
         error ("allocate failed");
     }
   descriptor.allocated = true

The "allocated" member of the descriptor is also needed in order to make the
following work properly:
   integer, target :: int
   integer, pointer :: ptr
   ptr => int
   deallocate(ptr, stat=i) ! shall return "i != 0" but not crash

Thus, from my side: The negative check should be removed and the simplified
version should be applied after the descriptor update. Before the descriptor
update (planned for 4.6, breaks ABI) one can use:
   size = (ubound - lbound + 1) * 8;
   malloc (max(1,size));


Paul, what do you think?


(PS: POSIX Allows "ptr = malloc(0); free(ptr)", where "malloc(0)" returns
either NULL or a unique pointer.)


-- 

burnus at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |pault at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=42958


  reply	other threads:[~2010-02-04 19:33 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-02-04 16:56 [Bug fortran/42958] New: " rguenth at gcc dot gnu dot org
2010-02-04 19:33 ` burnus at gcc dot gnu dot org [this message]
2010-02-05  5:36 ` [Bug fortran/42958] " pault at gcc dot gnu dot org
2010-02-05  9:32 ` rguenther at suse dot de
2010-02-05 14:24 ` rguenth at gcc dot gnu dot org
2010-02-17 20:17 ` jvdelisle at gcc dot gnu dot org
2010-02-20  8:31 ` burnus at gcc dot gnu dot org
2010-03-27 18:55 ` pault at gcc dot gnu dot org
2010-03-27 19:06 ` rguenth at gcc dot gnu dot org
2010-03-27 19:06 ` rguenth at gcc dot gnu dot org
2010-03-27 19:08 ` rguenth at gcc dot gnu dot org
2010-03-27 19:12 ` rguenth at gcc dot gnu dot org
2010-03-28 12:57 ` burnus at gcc dot gnu dot org
2010-03-28 13:05 ` burnus at gcc dot gnu dot org
2010-03-28 13:57 ` burnus at gcc dot gnu dot org
2010-03-28 14:11 ` burnus at gcc dot gnu dot org
2010-03-28 14:45 ` rguenther at suse dot de
2010-04-16  8:18 ` burnus at gcc dot gnu dot org
2010-04-28 14:52 ` rguenth at gcc dot gnu dot org
2010-04-28 15:16 ` amonakov at gcc dot gnu dot org
2010-04-28 15:20 ` jv244 at cam dot ac dot uk
2010-04-28 15:43 ` jb at gcc dot gnu dot org
2010-04-29 10:02 ` paul dot richard dot thomas at gmail dot com
2010-04-29 13:19 ` jakub at gcc dot gnu dot org
2010-04-29 19:09 ` tkoenig at gcc dot gnu dot org

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=20100204193256.20990.qmail@sourceware.org \
    --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).