typ*_*232 2 c c++ function call post-increment
可能重复:
未定义的行为和序列点
我正在使用microsoft visual c ++.请看以下示例:
int n = 5;
char *str = new char[32];
strcpy(str, "hello world");
memcpy(&str[n], &str[n+1], 6+n--);
printf(str);
// output is "hell world"
Run Code Online (Sandbox Code Playgroud)
所以不可思议的是,我的编译器生成的代码首先递减n然后执行memcpy.以下来源将完成我预期的事情:
int n = 5;
char *str = new char[32];
strcpy(str, "hello world");
memcpy(&str[n], &str[n+1], 6+n);
n--;
printf(str);
// output is "helloworld"
Run Code Online (Sandbox Code Playgroud)
首先,我试着向自己解释一下.最后一个参数首先被压入堆栈,因此可以先对其进行求值.但我真的相信后递增/递减保证在下一个分号后进行评估.
所以我运行了以下测试:
void foo(int first, int second) {
printf("first: %i / second: %i", first, second);
}
int n = 10;
foo(n, n--);
Run Code Online (Sandbox Code Playgroud)
这将输出"first:10/second:10".
所以我的问题是:这种情况有没有明确的行为?有人能指出我描述这个的文件吗?我找到了编译器错误~~ OO ~~?
这个例子简直就是不再有意义了,它只是展示了我的问题而且单独运作.