Bug 14951 - fail to assign function return value to struct member
Summary: fail to assign function return value to struct member
Status: RESOLVED DUPLICATE of bug 11751
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.2.2
: P1 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-04-14 07:52 UTC by Zheng Shao
Modified: 2005-07-23 22:49 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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
./a.out

You will see on the screen:
8:-1
8:-1
3:-1

But actually it should be:
8:-1
8:2
3:-1

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();
  buf.push_back(node());
  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(){
  construct(8,-1);
  construct(3,0);
  for(int i=0;i<buf.size();i++) {
    cout<<buf[i].value<<":"<<buf[i].son<<endl;
  }
  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 ***