This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: malloc attributes and realloc

Robert Dewar <> writes:

| Gabriel Dos Reis wrote:
| > Yes, that use is rather marginal in my experience and is rather
| > involved -- when done correctly.  But nobody knows what C programmers
| > do.
| Well we only pay attention to "what C programmers do" to a limited
| extent. Our primary semantic guide for correct translation is indeed
| the standard, and not the expectations of all possible C programmers.
| This of course is not absolute, there are some cases where we know that
| expectations outside the standard are strong enough that it makes sense
| to meet them even though the standard says we don't have to.
| > And I don't know how that impacts on the aliasing algorithm.
| And that of course is indeed the critical focus of the discussion!

Sure.  Marginal or not, it is covered by the standard and it can be

     void *p = malloc(34933);
     p = realloc(7489483454);
     if (same_pointer_value_as_before(p))
        printf("realloc returned same pointer\n");

That way, you can detect the behaviour of realloc. 

Now, I suspect that in practice there are lots of code where the
predicate same_pointer_value_as_before takes the vulgar form "p == q"
and if you bantantly assume that test must be false, then you'll end
up deleting a piece of code that has observable behaviour even when it
is well-formed.  If you say the test is true (just to make sure that
in the well-formed case you get it right) then you end up saying that
the return value of reallo may alias something already existing.

I suggest we don't apply the malloc attribute to realloc -- but we
correct (if possible) the comment in libiberty header.

-- Gaby

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]