On 9 May 2008 at 14:01, vippstar@[EMAIL PROTECTED]
wrote:
> On May 9, 3:06 pm, Antoninus Twink wrote:
>> #include <stdlib.h>
>> #include <string.h>
>>
>> int main(void)
>> {
>> char *x = malloc(128);
>> memset(x, 0, 128);
>> free(x);
>> return 0;
>>
>> }
[snip]
>> Notice that even on the highest optimization level, gcc doesn't remove
>> the call to memset, even with no volatile qualifiers around, and even
>> though the memory is immediately free()d afterwards.
>
> It shouldn't; That would make your program succeed even if malloc()
> returned NULL, which with your current code is not the case.
Define succeed.
Surely if malloc() returns NULL, then passing a null pointer to memset
will invoke undefined behavior, and "success" is one possible result of
undefined behavior. I know that if you believe the regulars here, then
formatting your hard disk and generating nasal demons are the most
likely consequences of undefined behavior, but out here in the real
world, there are other possibilities...


|