|how to avoid a memset() optimization email@example.com (Francis Wai) (2002-11-07)|
|Re: how to avoid a memset() optimization firstname.lastname@example.org (Lars Duening) (2002-11-08)|
|Re: how to avoid a memset() optimization email@example.com (Alex Colvin) (2002-11-08)|
|Re: how to avoid a memset() optimization firstname.lastname@example.org.OZ.AU (Fergus Henderson) (2002-11-12)|
|Re: how to avoid a memset() optimization email@example.com (Christian Bau) (2002-11-12)|
|Re: how to avoid a memset() optimization firstname.lastname@example.org (Lars Duening) (2002-11-12)|
|Re: how to avoid a memset() optimization email@example.com (Clayton Weaver) (2002-11-12)|
|Re: how to avoid a memset() optimization firstname.lastname@example.org (Charles Bryant) (2002-11-13)|
|Re: how to avoid a memset() optimization email@example.com (Dobes Vandermeer) (2002-11-13)|
|[9 later articles]|
|From:||"Alex Colvin" <firstname.lastname@example.org>|
|Date:||8 Nov 2002 11:02:08 -0500|
|Organization:||The World Public Access UNIX, Brookline, MA|
|Posted-Date:||08 Nov 2002 11:02:07 EST|
"Francis Wai" <email@example.com> writes:
> char key;
> strcpy(key, "whatever");
> memset(key, 0, 16);
>Various suggestions have been made, such as declaring the variable
>volatile and having a scrub memory function in a file of its own. I'm
>wondering if there are better ways such as telling the compiler not to
>optimize away a function call.
>[Declaring the array volatile is the right way to do it. The reason
>volatile exists is to tell the compiler not to do otherwise valid
I hesitate to contradict the master, but I vote against 'volatile" for
key. If you declare key volatile, then you have to cast away the
volatility when passing it to strcpy(), encrpts(), and memset(), which
do not deal with volatile strings. In this example, there's no reason
why they should.
You want the compiler to assume a reference to key after memset(),
which is what you're assuming when you worry about someone seeing
it. Try declaring key static or external instead. That warns the
compiler that you're assuming a lifetime beyond main().
If you absolutely need key to be auto, then you've got a problem.
Consider writing your own memset() that accepts a volatile.
mac the naïf
[I don't see any reason that casting away the volatile wouldn't work. -John]
Return to the
Search the comp.compilers archives again.