我已经查看了一系列有关序列点的问题,并且无法x*f(x)确定f修改后的评估顺序是否有保证x,这是不同的f(x)*x.
考虑以下代码:
#include <iostream>
int fx(int &x) {
x = x + 1;
return x;
}
int f1(int &x) {
return fx(x)*x; // Line A
}
int f2(int &x) {
return x*fx(x); // Line B
}
int main(void) {
int a = 6, b = 6;
std::cout << f1(a) << " " << f2(b) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
这打印49 42在g ++ 4.8.4(Ubuntu 14.04)上.
我想知道这是保证行为还是未指明.
具体来说,在这个程序中,fx两次x=6都被调用两次,并且两次都返回7次.所不同的是线A计算7×7(取的值x之后 …
出于好奇,我正在尝试生成堆栈溢出.此代码根据OP生成堆栈溢出,但是当我在我的机器上运行它时,它会生成分段错误:
#include <iostream>
using namespace std;
int num = 11;
unsigned long long int number = 22;
int Divisor()
{
int result;
result = number%num;
if (result == 0 && num < 21)
{
num+1;
Divisor();
if (num == 20 && result == 0)
{
return number;
}
}
else if (result != 0)
{
number++;
Divisor();
}
}
int main ()
{
Divisor();
cout << endl << endl;
system ("PAUSE");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
此外,根据这篇文章,一些例子也应该做同样的事情.为什么我会得到分段错误呢?
我写了一个简单的,工作的俄罗斯方块游戏,每个块作为类单块的一个实例.
class SingleBlock
{
public:
SingleBlock(int, int);
~SingleBlock();
int x;
int y;
SingleBlock *next;
};
class MultiBlock
{
public:
MultiBlock(int, int);
SingleBlock *c, *d, *e, *f;
};
SingleBlock::SingleBlock(int a, int b)
{
x = a;
y = b;
}
SingleBlock::~SingleBlock()
{
x = 222;
}
MultiBlock::MultiBlock(int a, int b)
{
c = new SingleBlock (a,b);
d = c->next = new SingleBlock (a+10,b);
e = d->next = new SingleBlock (a+20,b);
f = e->next = new SingleBlock (a+30,b);
}
Run Code Online (Sandbox Code Playgroud)
我有一个扫描完整行的函数,并运行删除相关的块的链接列表并重新分配 - >下一个指针. …
我使用valgrindwith 检查了C++中的以下代码--leak-check=full,它说没有内存泄漏.这是为什么?
char *p = new char[256];
delete p;
Run Code Online (Sandbox Code Playgroud)
new[]delete[]据我所知应该匹配.
std::list<Node *> lst;
//....
Node * node = /* get from somewhere pointer on my node */;
lst.remove(node);
Run Code Online (Sandbox Code Playgroud)
std :: list :: remove方法是否调用每个被删除元素的析构函数(和空闲内存)?如果是,我怎么能避免呢?
我遇到了关于C编程语言的这个客观问题.以下代码的输出应该是0 2,但我不明白为什么.
请解释初始化过程.这是代码:
#include <stdio.h>
int main()
{
union a
{
int x;
char y[2];
};
union a z = {512};
printf("\n%d %d", z.y[0], z.y[1]);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 有人能告诉我前缀/后缀运算符是如何工作的吗?我一直在网上看很多但没找到任何东西.
从我可以告诉prefex第一个增量,然后执行操作,然后分配.
Postfix将首先执行操作,然后分配然后递增.
但是我的代码遇到了一些麻烦:
int x, y;
x = 1;
y = x + x++; // (After operation y = 2)(x=2)
Run Code Online (Sandbox Code Playgroud)
但是,当我这样做时:
y = x++ + x; // (After operation y = 3)(x=2)
Run Code Online (Sandbox Code Playgroud)
我不确定为什么这些操作会有所不同.我有两个问题:
你能解释一下这个区别吗?
这如何适用于其他运营商Prefix?
那我们会得到UB吗?我试过这个:
#include <iostream>
struct B
{
B(){ std::cout << "B()" << std::endl; }
~B(){ std::cout << "~B()" << std::endl; }
};
struct A
{
B b;
A(){ std::cout << "A()" << std::endl; throw std::exception(); }
~A(){ std::cout << "~A()" << std::endl; }
};
int main()
{
A a;
}
Run Code Online (Sandbox Code Playgroud)
desctructor没有被要求netither A也没有B.实际输出:
B()
A()
terminate called after throwing an instance of 'std::exception'
what(): std::exception
bash: line 7: 21835 Aborted (core dumped) ./a.out
Run Code Online (Sandbox Code Playgroud)
http://coliru.stacked-crooked.com/a/9658b14c73253700
因此,在块范围变量初始化期间构造函数抛出的任何时候,我们都得到UB吗?
我偶然发现了以下代码片段:
#include <iostream>
#include <string>
using namespace std;
class First
{
string *s;
public:
First() { s = new string("Text");}
~First() { delete s;}
void Print(){ cout<<*s;}
};
int main()
{
First FirstObject;
FirstObject.Print();
FirstObject.~First();
}
Run Code Online (Sandbox Code Playgroud)
该文本表示此代码段应该导致运行时错误.现在,我对此并不十分肯定,所以我尝试编译并运行它.有效.奇怪的是,尽管所涉及的数据非常简单,但在打印"文本"之后程序结结巴巴,并且仅在一秒钟之后完成.
我添加了一个要打印到析构函数的字符串,因为我不确定显式调用这样的析构函数是否合法.程序打印两次字符串.所以我的猜测是析构函数被调用两次,因为正常的程序终止不知道显式调用并试图再次销毁对象.
一个简单的搜索确认显式调用自动化对象上的析构函数是危险的,因为第二次调用(当对象超出范围时)具有未定义的行为.所以我很幸运,我的编译器(VS 2017)或这个特定的程序.
关于运行时错误,文本是否完全错误?或者运行时错误真的很常见吗?或者也许我的编译器实现了某种针对这类事情的warding机制?
c++ destructor lifetime undefined-behavior explicit-destructor-call