Consider the following program:
include <iostream>
class Bar
{ public:
int getData9() { m_data = 9; return m_data;}
int getData11() { m_data = 11; return m_data;}
int m_data;
};
int main()
{
Bar f;
int a = f.getData11() – f.getData9();
std::cout<<"Answer: "<<a<<std::endl;
}
This program on this (not so great in general) compiler produces an
answer of "0".
My assumption is that each of the function calls should be a sequence
point, and the behavior according to the standard is well defined.
The fact that these function calls happen to be inlined by the
compiler (something a caller has no control over) does not change the
fact that they are indeed "function calls" with regards to what is and
what is not a sequence point.
Any thoughts? References?
Thanks 3 1664
On 22 Maj, 23:21, joe <joeyc...@mail.comwrote:
Consider the following program:
include <iostream>
class Bar
{ public:
* int getData9() * { *m_data = 9; *return m_data;}
* int getData11() { *m_data = 11; return m_data;}
* int m_data;
};
int main()
{
* Bar f;
* int a *= f.getData11() – f.getData9();
* std::cout<<"Answer: "<<a<<std::endl;
}
This program on this (not so great in general) compiler produces an
answer of "0".
My assumption is that each of the function calls should be a sequence
point, and the behavior according to the standard is well defined.
The fact that these function calls happen to be inlined by the
compiler (something a caller has no control over) does not change the
fact that they are indeed "function calls" with regards to what is and
what is not a sequence point.
Any thoughts? *References?
Thanks
Your compiler is wrong. There is a sequence point after each function
call:
[program.execution]
"When calling a function (whether or not the function is inline),
there is a sequence point after the evaluation of all function
arguments (if any) which takes place before execution of any
expressions or statements in the function body."
/Peter
On May 22, 4:21*pm, joe <joeyc...@mail.comwrote:
Consider the following program:
include <iostream>
class Bar
{ public:
* int getData9() * { *m_data = 9; *return m_data;}
* int getData11() { *m_data = 11; return m_data;}
* int m_data;
};
int main()
{
* Bar f;
* int a *= f.getData11() – f.getData9();
* std::cout<<"Answer: "<<a<<std::endl;
}
This program on this (not so great in general) compiler produces an
answer of "0".
My assumption is that each of the function calls should be a sequence
point, and the behavior according to the standard is well defined.
The fact that these function calls happen to be inlined by the
compiler (something a caller has no control over) does not change the
fact that they are indeed "function calls" with regards to what is and
what is not a sequence point.
Several issues:
- The result should be 2, not 0.
- The function being inlined has no impact on sequence points
- As written, there's nothing that would be impacted by sequence
points anyway.
Thanks for all the responses. That's what I believed.
The assembler output produced is ... amusing
bl _main
li r12, 9 // Load value "9" into register 12
li r12, 11 // Load value "11" into register 12 ??
subf r12, r12, r12 // Subtract r12 by r12 and store in r12
-
Joe This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Jens.Toerring |
last post by:
Hi,
I have a possibly rather stupid question about the order of evaluation
in a statement like this:
result = foo( x ) - bar( y );
How can I make 100% sure that foo(x) is evaluated before...
|
by: subnet |
last post by:
What does the standard say about this:
#include <stdio.h>
void somefunc(int a, int b, int c)
{
printf("%d %d %d\n", a, b, c);
}
int main(void) {
int i = 5;
|
by: Steve McLellan |
last post by:
Hi,
We've found some pretty serious performance hits that we didn't expect in a
mixed mode C++ application. The number crunching bits of our algorithms are
compiled with #pragma unmanaged. They...
|
by: dragoncoder |
last post by:
Consider the following code.
#include <stdio.h>
int main()
{
int i =1;
printf("%d ,%d ,%d\n",i,++i,i++);
return 0;
}
|
by: Bilgehan.Balban |
last post by:
Hi,
If I define an inline function in one .c file, and use it from another,
after compiling and linking the two, it seems the function is not
inlined but rather called as a regular function. I...
|
by: mdh |
last post by:
May I ask the group the following: (Again, alas , from K&R)
This is part of a function:
while ( ( array1 = array2 ) != '\0' ); /* etc etc */
Is this the order that this is evaluated?
...
|
by: I.M. !Knuth |
last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until
I started goofing around with this:
...
|
by: sam_cit |
last post by:
Hi Everyone,
I have the following code,
int main()
{
int p = {10,20,30,40,50};
int *i = &p;
printf("before : %p\n",(void*)i);
printf("1 %d %d\n",*++i,*i++);
|
by: subramanian100in |
last post by:
In the following link,
http://www.c-faq.com/malloc/retaggr.html
The following ANSWER is given to a question(comp.lang.c FAQ list ·
Question 7.5a) :
Whenever a function returns a pointer, make...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
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...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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...
|
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...
|
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...
|
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...
| |