Re: automatic arrays with negative size in g95 and gfortran




glen herrmannsfeldt wrote:

For comparison purposes, the argument to malloc() in most C libraries
is unsigned, so a negative value will be treated as a very large
positive value. Even worse, many will return zero (NULL) when the
argument is zero, a request to allocate a zero size array. Since NULL
is returned when the allocation failed, the program will normally assume
it failed, and treat it appropriately. Some, it seems, treat a request
of zero as a request for 2**32 bytes (on 32 bit systems) which usually
fails.

No, the C standard allows that behavior. Some (misguided) people
think it is a good idea. The specific language of the C standard is

If the size of the space requested is zero, the behavior is
implementation-defined: either a null pointer is returned,
or the behavior is as if the size were some nonzero value,
except that the returned pointer shall not be used to access
an object.

Bob Corbett

.



Relevant Pages

  • Re: Return value of malloc(0)
    ... choices in the standard. ... or 2) Behave as though it returned a 'minimum allocation' (which ... the returned pointer shall not be used to access an object. ... "minimum" is zero, then using a sentinel value works. ...
    (freebsd-hackers)
  • Re: automatic arrays with negative size in g95 and gfortran
    ... requests for zero or negative values, ... there is no way to even express such a request. ... You do not directly specify allocation ... C will, in most cases, attempt to allocate a very large amount of memory, and most likely fail. ...
    (comp.lang.fortran)
  • Re: [PHP] correctly reading binary data from http post
    ... it yields 1 (it truncates the string after the first zero byte). ... Instead set your request Content-Type to octet-stream and grab the whole post body at once. ... 'http' => array( ...
    (php.general)
  • Re: When to check the return value of malloc
    ... void *xmalloc{ ... allocate a zero amount of memory (and *why*, precisely, would anyone want ... returned shall be either a null pointer or a unique pointer." ... My preference would be to make the action taken on a zero byte request a ...
    (comp.lang.c)
  • Re: [QUICKLIST 0/4] Arch independent quicklists V2
    ... Well if they're zero then perhaps they should be released to the page allocator ... to satisfy the next __GFP_ZERO request. ... Send instant messages to your online friends http://au.messenger.yahoo.com - ...
    (Linux-Kernel)