473,396 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Q about "optimizing away" "non-used" code

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. For example, in the following:
{
for(int i = 0; i < 10; ++i){
std::cout << i << std::endl;
for(int j = 0; j < 0x7fffffff; ++j){}
}
}

since j is not "used" the compiler can simply get rid of the j-loop.
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?

For example, in the following code:
int main(){
int* a = new int[100];

// some code that uses a

memset(a, 0, 100 * sizeof(*a));
delete [] a;
return 0;
}

is the compiler *obligated* to overwrite the memory used by a before
the program returns, or can the memset line be "optimized away" since
that memory isn't used to produce output later in the program?

The real question is can memset be used to assure that a program's
data is purged from memory before exiting?
Jul 22 '05 #1
4 2493
"J. Campbell" wrote:

since j is not "used" the compiler can simply get rid of the j-loop.
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?
memset is not a command (in general there are no commands in C++)
memset is a function.

For example, in the following code:
int main(){
int* a = new int[100];

// some code that uses a

memset(a, 0, 100 * sizeof(*a));
delete [] a;
return 0;
}

is the compiler *obligated* to overwrite the memory used by a before
the program returns, or can the memset line be "optimized away" since
that memory isn't used to produce output later in the program?


Hmm. Optimization in C++ is under the influence of one rule:
as - if.

That means that the compiler is allowed to rearange everything, as long
as the observable behaviour is as-if the rearrangment hasn't taken
place (with one exception, but this is unimportant right now). So
in principle, if it is a very smart compiler, it could do the
above optimization, if the compiler takes the behaviour of
memset into account. As far as the compiler is concerned,
memset is just a call to a function. On the other hand it is a
standard function, and of course the compiler could inline it
thus eliminating the function call at all which leaves us with
a sort of 'assignment to memory'.
A smart compiler doing data flow anaylsis could also figure
out the point of memory destruction and working backwards from
that point it could throw away all assignments to that memory
until it hits the last read from that memory.

So in principle it is possible. If any compiler does this:
I don't know. Test your compiler.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2

"J. Campbell" <ma**********@yahoo.com> wrote in message
news:b9**************************@posting.google.c om...
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. For example, in the following:
{
for(int i = 0; i < 10; ++i){
std::cout << i << std::endl;
for(int j = 0; j < 0x7fffffff; ++j){}
}
}

since j is not "used" the compiler can simply get rid of the j-loop.
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?
Getting rid of an empty loop is a trivial optimization but optimizing away
whole function calls is a different issue.

For example, in the following code:
int main(){
int* a = new int[100];

// some code that uses a

memset(a, 0, 100 * sizeof(*a));
delete [] a;
return 0;
}

is the compiler *obligated* to overwrite the memory used by a before
the program returns, or can the memset line be "optimized away" since
that memory isn't used to produce output later in the program?
IMHO theoretically the compiler might be allowed to optimize away this
function call, though I doubt that it will do it as the analysis for this is
more complicated than finding an empty loop. However, if you want to be sure
you should take a look at the machine code that your specific compiler
produces. In case you still find such unwanted optimization you can still
turn it off anyway.
The real question is can memset be used to assure that a program's
data is purged from memory before exiting?


Why is that so important for you?

HTH
Chris
Jul 22 '05 #3
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. As far as the first case goes,
it is up to the compiler and how it feels about optimizing. The latter
case is up to the linking portion. Some lazy linkers link in everything
{including libraries) to speed up the process. More optimal linkers
will omit functions that are not executed.

For example, in the following:
{
for(int i = 0; i < 10; ++i){
std::cout << i << std::endl;
for(int j = 0; j < 0x7fffffff; ++j){}
}
}

since j is not "used" the compiler can simply get rid of the j-loop.
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.

For example, in the following code:
int main(){
int* a = new int[100];

// some code that uses a

memset(a, 0, 100 * sizeof(*a));
delete [] a;
return 0;
}

is the compiler *obligated* to overwrite the memory used by a before
the program returns, or can the memset line be "optimized away" since
that memory isn't used to produce output later in the program?
Yes, because in the above scenario, the allocated data is erased to
"leave no trace".

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). The function's only responsibility is to set
an area of memory to a given value. The memset function can be
applied to fixed (static) areas also.

The C++ language does not require an implementation to purge or
erase any memory regions. This _could_ be an operating system
issue; after all, the operating system is in charge of loading
a program into memory and executing it, then recovering the
memory.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #4
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.
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: DaKoadMunky | last post by:
I recently came across some code in a template that default constructed an object of type T to pass to another function... SomeFunction(T()); The code that instantiates that template specifies...
145
by: Sidney Cadot | last post by:
Hi all, In a discussion with Tak-Shing Chan the question came up whether the as-if rule can cover I/O functions. Basically, he maintains it can, and I think it doesn't. Consider two...
40
by: Mark P | last post by:
I'm implementing an algorithm and the computational flow is a somewhat deep. That is, fcn A makes many calls to fcn B which makes many calls to fcn C, and so on. The return value of the outermost...
7
by: VK | last post by:
Let's say I have a rather big HTML template like <?xml version="1.0" encoding="ISO-8859-1"?> .... <snip> .... <xsl:template match="/"> <html> <!-- ... A lot of HTML but no XSL so far
0
by: U S Contractors Offering Service A Non-profit | last post by:
" Visionary Dreams " " Leaving New york City leaving to go " GOD noes were i Don't "
2
by: dave | last post by:
Hi, I have searched for the answer for this error message without success. I have seen the question many times though:) I create an ASP.NET project (VS 2005, C#), and use a very simple .mdf...
206
by: WaterWalk | last post by:
I've just read an article "Building Robust System" by Gerald Jay Sussman. The article is here: http://swiss.csail.mit.edu/classes/symbolic/spring07/readings/robust-systems.pdf In it there is a...
84
by: aarklon | last post by:
Hi all, I found an interesting article here:- http://en.wikipedia.org/wiki/Criticism_of_the_C_programming_language well what do you guys think of this article....??? Is it constructive...
2
by: Ken Tilton | last post by:
Total JS noob here, but porting my hairy Common Lisp Cells (dataflow) package -- long intro on my blog: http://smuglispweeny.blogspot.com/2008/02/cells-manifesto.html ....so I am getting into...
17
by: David C. Ullrich | last post by:
Having a hard time phrasing this in the form of a question... The other day I saw a thread where someone asked about overrideable properties and nobody offered the advice that properties are...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.