------- 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 via email to