While it was 21/1/04 3:21 pm throughout the UK, Thomas Matthews
sprinkled little black dots on a white screen, and they fell thus:
J. Campbell wrote:
From reading this forum, it is my understanding that C++ doesn't
require the compiler to keep code that does not manifest itself in any
way to the user.
There are two cases about unused code: code not used within a function
and entire functions that are not used.
That's not what the OP is talking about. In the given example, the code
_is_ used, the only variable declared within it _is_ used, it merely has
no overall effect.
Surely almost any compiler is going to optimise away unreachable code,
such as that immediately following a break or return in its block with
no label leading to it.
A half-decent compiler would probably do the same with conditions that
are obviously either always true or always false.
A null-effect loop like the OP's example just might be cut out.
However, it cannot know for sure that the programmer wasn't intending to
count up to 0x7fffffff as a processor speed benchmark, or to make a game
run at the right speed on an 8MHz 8086 while having it unplayable on
anything made in the last 15 years.
<snip>
The question is this: Can an optimizing compiler ignore a memset()
command if the memory pointed to is never again accessed after the
memset() command?
No. There are instances where the area of memory needs to be cleared,
or erased before the function is exited. One scenario is cryptography,
where secret keys must be erased before the function is terminated.
Obviously the decrypted data (or data not yet encrypted) would need to
be erased just as well.
<snip>
The real question is can memset be used to assure that a program's
data is purged from memory before exiting?
The memset function has no relation to the allocation or deallocation
of memory (purging).
<snip>
Exactly. But it's purging of data that's the question here.
Stewart.
--
My e-mail is valid but not my primary mailbox, aside from its being the
unfortunate victim of intensive mail-bombing at the moment. Please keep
replies on the 'group where everyone may benefit.