Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 254 Vote(s) - 3.66 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Order of function call

#1
for the expression

(func1() * func2()) + func3()

will func1() * func2() be evaluated first as it has brackets or can the functions be called in any order like

first func3() and then (func1() * func2())
Reply

#2
The functions can be called in any order.
Reply

#3
Parenthesis in C/C++ force order of operations. `func1() * func2()` will be added to `func3()`, but the compiler can choose to call the functions in whatever order it wishes before passing in the results to the multiplication / addition operation.
Reply

#4
You can't make any assumptions about the order in which these functions will be called. It's perfectly valid for the compiler to call these functions in any order, assign the results to temporaries, and then use these temporary values to calculate the result of the expression.
Reply

#5
It's natural to think that `A+B` is evaluated before `C` in this psudocode:

`(A+b)*C`

But in fact this is not so. The Standard says that the order of evaluation for *all* expressions is "Unspecified", unless otherwise specified by the Standard:

5/4 [expr]:
----------

> Except where noted, the order of
> evaluation of operands of individual
> operators and subexpressions of
> individual expressions, and the order
> in which side effects take place, is
> unspecified

The Standard then goes on to identify a parenthesized expression as a "Primary expression" but does not specify the order of evaluation for Primary expressions. (5.1/5).

In Standardese, "Unspecified" does not mean "Undefined." Rather it means "Implementation Defined, but no documentation is required." So you might not even be able to say what the order of evaluation is for a specific compiler.

Here is a simple program illustrating the behavior:

#include <iostream>
#include <string>
using namespace std;

class Foo
{
public:
Foo(const string& name) : name_(name) {++i_; cout << "'" << name << "'(" << i_ << ")\n"; };
operator unsigned() const { return i_; }
Foo operator+(const Foo& rhs) const { string new_name = name_; new_name += "+"; new_name += rhs.name_; return Foo(new_name); }
private:
string name_;
static unsigned i_;
};

unsigned Foo::i_ = 0;

int main()
{
(Foo("A") + Foo("B")) + Foo("C");
}

On my MSVC10 running in Debug/x64 on Win7, the output happened to be:

'C'(1)
'B'(2)
'A'(3)
'A+B'(4)
'A+B+C'(5)

Reply

#6
Precedence of operators has got nothing to do anything with the order of evaluation of operands.

The C or C++ Standard doesn't determine the order in which the functions would be called. .

The order of evaluation of subexpressions, including

- the arguments of a function call and
- operands of operators (e.g.`, +, -, =, * , /`), with the exception of:
- the binary logical operators (`&&` and `||`),
- the ternary conditional operator (`?:`), and
- the comma operator (`,`)

is **Unspecified**

For example

int Hello()
{
return printf("Hello"); /* printf() returns the number of
characters successfully printed by it
*/
}

int World()
{
return printf("World !");
}

int main()
{

int a = Hello() + World(); //might print Hello World! or World! Hello
/** ^
|
Functions can be called in either order
**/
return 0;
}
Reply

#7
These calls can be made in any order. You want to learn about <del>[C++ sequence points][1]</del> [C++ sequence points][2].


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through