473,226 Members | 1,331 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,226 software developers and data experts.

OK for compiler to not call function with constant return?

Hi all.

Suppose I have

bool CFoo::AMethod()
{
// do lots of side effects...
...
return false; // the only return
}

and later I have:

bool result = AMethod();

Is it OK for the compiler to skip the call to AMethod and just assign
the value false to result? Even if all optimisations are off?

Thanks

Aaron
Feb 9 '06 #1
4 1125
Hi,
It wont assign false directly. It will only happen if the function
is inline.

Best regards,
Amal P.

Feb 9 '06 #2
Aaron Lawrence wrote:
Hi all.

Suppose I have

bool CFoo::AMethod()
{
// do lots of side effects...
...
return false; // the only return
}

and later I have:

bool result = AMethod();

Is it OK for the compiler to skip the call to AMethod and just assign
the value false to result? Even if all optimisations are off?


Not in general, it depends what you mean by side-effects.

Can you post complete, compilable code that demonstrates the problem?

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Feb 9 '06 #3
In message <lD**********************@news.xtra.co.nz>, Aaron Lawrence
<aa*******@HEREintegration.co.nz> writes
Hi all.

Suppose I have

bool CFoo::AMethod()
{
// do lots of side effects...
...
return false; // the only return
}

and later I have:

bool result = AMethod();

Is it OK for the compiler to skip the call to AMethod and just assign
the value false to result? Even if all optimisations are off?

Not if the side-effects affect program state. The compiler is allowed to
eliminate some calls to copy constructors, but otherwise the as-if rule
means that if the function has detectable side-effects, they must take
place.

Can you post a short complete program that demonstrates your actual
problem?

--
Richard Herring
Feb 9 '06 #4
Richard Herring wrote:
Not if the side-effects affect program state. The compiler is allowed to
eliminate some calls to copy constructors, but otherwise the as-if rule
means that if the function has detectable side-effects, they must take
place.
That's what I thought. It seemed like a clear case of the optimisation
breaking my reasonable code because I had accidentally returned false in
all cases. Suddenly my function was no longer called.
Can you post a short complete program that demonstrates your actual
problem?


Should be possible. Tomorrow ...

Cheers

Aaron
Feb 9 '06 #5

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

Similar topics

0
by: r5 | last post by:
I'm using the MIPSpro Compiler and having trouble defining a function template (involving array size specifiers as template arguments) inside a class. The same definition compiles fine outside...
20
by: Markus Sandheide | last post by:
Hello! Execute these lines: int x = 1; x = x > 2345678901; You will get: x == 1 with Borland C++ Builder
43
by: Anitha | last post by:
Hi I observed something while coding the other day: if I declare a character array as char s, and try to use it as any other character array..it works perfectly fine most of the times. It...
14
by: joshc | last post by:
I'm writing some C to be used in an embedded environment and the code needs to be optimized. I have a question about optimizing compilers in general. I'm using GCC for the workstation and Diab...
9
by: Brian Tyler | last post by:
I have seen a very strange piece of code being generated by the C# compiler and was hoping someone might be able to shed some light on it. I've reduced the example down to a bare minimum and I am...
8
by: amjain.gzb | last post by:
Greetings, I am trying to compile some code on Solaris. I need to compile that using CC instead of g++. Following small program is representation of my problem ...
29
by: Ark | last post by:
A function int foo(struct T *x) { return (x+1)-x; } should always return a 1, no matter how T is defined. (And int could be replaced with ptrdiff_t for you pedants.) For one thing, it was...
0
by: erik.erikson | last post by:
I am getting a compiler error that I can't well explain or even understand the origin of (though I boiled it down close...). Below is a bare-bones example. What I am doing is defining the...
5
by: marcsirois | last post by:
I have an SQL Query that looks like this. I simplified it for the purpose of this example, but it's still a runnable query SELECT TOP 1 FundedPositions.PositionNumber AS , (select top 1...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.