Flash Gordon writes:
>Hallvard B Furuseth wrote, On 20/04/08 17:49:
>>Flash Gordon writes:
>>> wrote, On 18/04/08 16:33:
>>>
>>>> According to my (poor) understanding of
>>>> the standard, if realloc() returns the original pointer, then oldbase
>>>> and newbase are both valid pointers which compare equal, so there is
>>>> no problem.
>>> Correct.
>>
>> No. The realloc definition does not acknowledge any original pointer.
>> It just says realloc deallocates the old & returns a new object.
>> Comparing oldbase with anything yields undefined behavior, so how do you
>> know realloc returned the original pointer anyway?
>
> You can use memcmp. Of course, it might give false negatives.
And as I said below, it can give false positives too.
> However, if the bit pattern is the same I don't see any way for it not
> to still be valid.
What's "valid"? What does "the pointers are the same" mean? The
standard doesn't define any of this, so you have to define it first.
>> The compiler can
>> catch you at it.
>> E.g. if it knows that some variable contains the now-indeterminate
>> oldbase value, it may overwrite that variable. (Well, unless the
>> program can detect that the bit pattern in the variable changed, I guess
>> - by reading the variable as character data.)
>
> Which the program is allowed to do
Sure, but most programs don't. And while you can use memcmp and thus
freeze the bit pattern or something, it's still hard to decide just what
that means in theory. In practice it may well make sense to decide you
don't support too esoteric architectures.
After a memcmp the old pointer value and pointers computed from it
remain invalid when used as pointers, but you could use them anyway if
you are sure you've successfully protected them from the compiler (and
compilers are getting smarter all the time). Preferably you'd instead
use the new pointer and just make a note that it matches the old one,
but that doesn't help old pointers computed from the original old one.
>> A more esoteric variant I seem to remember seeing in comp.<lang/std>.c:
>> If malloc/realloc can affect page maps rather than moving memory around,
>> the new pointer could even have the same bit representation as the old
>> pointer - and yet be a different pointer. And realloc need then not
>> ensure that the value (old pointer + pagesize) still refers to the
>> corresponding (new pointer + pagesize).
>
> If the bit patterns of old pointer and new pointer are the same it is
> difficult to see how old pointer can fail to point at the correct place.
>
> However, the OP was explicitly talking about if the original pointer was
> returned not a pointer that happened to have the same bit pattern!
Yes. That's why I said it can adjust page maps. And an address range
viewed as integeres need not be a sequence of contiguous numbers.
Realloc can grow a malloced area by affecting the OS's mapping of
virtual memory to physical memory. If it needs a new page to be put at
the end of an address range, it just asks the OS to put insert a page
there and tell it the page number. Pointer arithmetic which crosses
page boundaries will need help from the virtual memory page maps.
And - since realloc invalidates all pointers into the malloced address
range, realloc might be a good time to normalize the page maps of that
address range, or something like that. Then even if the bit pattern of
a pointer to the start of the address range remains the same, the bit
pattern of the value (start of alloced area + some offset) can change.
I don't remember if that was the actual example (probably it wasn't),
but it is possible and stuff like that has been done.
> I agree that there is no sensible way to check of the old pointer and
> new pointer are the same and it is pointless to try.
--
Hallvard