On May 9, 3:06 pm, some troll wrote:
> On 9 May 2008 at 7:36, Dan wrote:
>
> > What is the correct way to allocate memory that is defined as volatile
> > so that all standard compilers will not optomise out writes to the
> > memory that are never read again?
>
> What compiler are you using? How are you writing to the memory?
>
> Here's a simple program:
>
> #include <stdlib.h>
> #include <string.h>
>
> int main(void)
> {
> char *x = malloc(128);
> memset(x, 0, 128);
> free(x);
> return 0;
>
> }
>
> Here's the start of the code produced by gcc with -O3 optimization
> level:
<possible gcc output>
> 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.


|