473,491 Members | 2,008 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1132
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
1130
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
2286
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
2655
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
3099
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
1651
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
7211
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
2337
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
2030
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
2401
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...
0
6978
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
7154
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,...
1
6858
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
7360
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5451
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,...
0
4578
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3086
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3076
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
280
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.