Patch: malloc function attribute

Mark Mitchell mark@codesourcery.com
Mon Nov 29 07:49:00 GMT 1999


>>>>> "Jeffrey" == Jeffrey A Law <law@cygnus.com> writes:

    Jeffrey> I believe xrealloc is safe.  Consider the conditions
    Jeffrey> under which is can return the same pointer, I believe all
    Jeffrey> are safe.

I believe that if you know something about your realloc
implementation, then you can know that realloc will return the same
pointer.  So, I can imagine something like:

  int *ip = (int *) malloc (8);
  int *ip2 = (int* ) realloc (ip, 16);

and that the programmer knows that `ip' and `ip2' *are* aliases.  I've
just looked at the C standard and I don't see any reason to believe
that using `ip' in this code yields undefined behavior, if you know
that `ip' and `ip2' are equal.  In particular, I'm sure this is legal:

  if (ip == ip2) {
    ip[2] = 3;
    ip2[2] = 7;
  }

and ought to leave the 2nd element with a 7 in it, so we'd better
think the return value from realloc aliases ip.

Maybe I'm off base, but I thought that the malloc attribute said the
new pointer doesn't alias anything.

--
Mark Mitchell                   mark@codesourcery.com
CodeSourcery, LLC               http://www.codesourcery.com


More information about the Gcc-patches mailing list