User account creation filtered due to spam.

Bug 14951

Summary: fail to assign function return value to struct member
Product: gcc Reporter: Zheng Shao <zshao>
Component: c++Assignee: Not yet assigned to anyone <unassigned>
Severity: normal CC: gcc-bugs
Priority: P1    
Version: 3.2.2   
Target Milestone: ---   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed:

Description Zheng Shao 2004-04-14 07:52:33 UTC
**Note: The short code in this post reveals the bug, which fails to assign 
function return value to struct member when executing:
a.member = func(b,c);

**Note: I tried my best to simplify the source code. I think the source is 
short and obvious.

**Instructions to reveal the bug:
Please save the source file to test.cpp and execute the following command lines:
g++ test.cpp

You will see on the screen:

But actually it should be:

The reason is that the noted line in the source code:
  buf[r].son = construct(value, buf[r].son);
calls construct, but does not assign the value to buf[r].son.

//test.cpp source begins
#include <iostream>
#include <vector>
using namespace std;

struct node{
  int value;
  int son;
  node() {value=0;son=-1;}

vector<node> buf;

int construct(int value, int tree) {
  int r = buf.size();
  if(tree==-1) {
    buf[r].value = value;
    return r;
  buf[r] = buf[tree];
  // ************* the following line is not executed correctly *******
  buf[r].son = construct(value, buf[r].son);
  return r;

int main(){
  for(int i=0;i<buf.size();i++) {
  return 0;
//source ends
Comment 1 Andrew Pinski 2004-04-14 11:45:33 UTC
This is invalid because the next statement is equivent to
buf[r].son = construct(value, buf[r].son);

buf.operator[](r).son = construct(value, buf.operator[](r).son);

So the order of the function call to the operator[] is not defined as you do not have a sequence point 
inbetween the calls (a comma is not a sequence point).
Comment 2 Wolfgang Bangerth 2004-08-13 15:45:21 UTC
Reopen to mark as a duplicate of... 
Comment 3 Wolfgang Bangerth 2004-08-13 15:45:56 UTC
...PR 11751. 

*** This bug has been marked as a duplicate of 11751 ***