Re: how to avoid a memset() optimization

"Fergus Henderson" <fjh@cs.mu.OZ.AU>
13 Nov 2002 12:18:41 -0500

          From comp.compilers

Related articles
[3 earlier articles]
Re: how to avoid a memset() optimization fjh@cs.mu.OZ.AU (Fergus Henderson) (2002-11-12)
Re: how to avoid a memset() optimization christian.bau@freeserve.co.uk (Christian Bau) (2002-11-12)
Re: how to avoid a memset() optimization lars@bearnip.com (Lars Duening) (2002-11-12)
Re: how to avoid a memset() optimization cgweav@aol.com (Clayton Weaver) (2002-11-12)
Re: how to avoid a memset() optimization n2102139816.ch@chch.demon.co.uk (Charles Bryant) (2002-11-13)
Re: how to avoid a memset() optimization dobes@dobesland.com (Dobes Vandermeer) (2002-11-13)
Re: how to avoid a memset() optimization fjh@cs.mu.OZ.AU (Fergus Henderson) (2002-11-13)
Re: how to avoid a memset() optimization jvorbrueggen@mediasec.de (Jan C. =?iso-8859-1?Q?Vorbr=FCggen?=) (2002-11-13)
Re: how to avoid a memset() optimization usenet-1ugeabe@qeng-ho.org (Arthur Chance) (2002-11-13)
Re: how to avoid a memset() optimization cfc@shell01.TheWorld.com (Chris F Clark) (2002-11-15)
Re: how to avoid a memset() optimization usenet-1ugeabe@qeng-ho.org (Arthur Chance) (2002-11-15)
Re: how to avoid a memset() optimization joachim_d@gmx.de (Joachim Durchholz) (2002-11-17)
Re: how to avoid a memset() optimization cfc@shell01.TheWorld.com (Chris F Clark) (2002-11-20)
[2 later articles]
| List of all articles for this month |
From: "Fergus Henderson" <fjh@cs.mu.OZ.AU>
Newsgroups: comp.compilers
Date: 13 Nov 2002 12:18:41 -0500
Organization: Computer Science, University of Melbourne
References: 02-11-030 02-11-053
Keywords: optimize
Posted-Date: 13 Nov 2002 12:18:41 EST

"Clayton Weaver" <cgweav@aol.com> writes:


>>int main()
>>{
>> char key[16];
>> strcpy(key, "whatever");
>> encrpts(key);
>> memset(key, 0, 16);
>>}
>
> if (!memset(key, 0, 16)) {
> return error;
> }
>
>(ie waste a jump; might even be more efficient if it still allows
>other optimizations that volatile would prevent).


This is not guaranteed to work. A compiler might easily know that
`memset(key, ...)' returns `key', and that the address of local variables
is never null, and might thus be able to optimize away both the if and
the memset.


Using volatile is better, because it provides more guarantees.


Unfortunately using volatile is not guaranteed to work either,
Using volatile guarantees that the contents of `key' will be
overwritten. But it doesn't prevent the compiler from having
alos stored the contents into other memory areas (e.g. the stack
frame used by encrpts()) which may not get cleared. However,
this failure mode is IMHO a lot less likely.
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
The University of Melbourne | of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh> | -- the last words of T. S. Garp.


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.