Content #
ulimit -c unlimited
Sometimes it’s useful to tell the debugger to stop at a break point only if some condition is met, like the when a variable has a particularly interesting value. Conditional Breakpoints is similar to watchpoints work, but with an important distinction.
If you have a suspicion about where a variable is getting a bogus value, a conditional breakpoint is preferable to a watchpoint.
The watchpoint will break whenever that variable changes value. The conditional breakpoint will only break at the suspected problem code, and then, only when the variable takes on the bogus value.
...RAII(Resource Acquisition Is Initialization,资源获取就是初始化),是 C++ 中的一个利用了面向对象的技术。这个设计模式叫“代理模式”。我们可以把一些控制资源分配和释放的逻辑交给这些代理类,然后,只需要关注业务逻辑代码了。而且,在我们的业务逻辑代码中,减少了这些和业务逻辑不相关的程序控制的代码。
mutex m;
void foo() {
m.lock();
Func();
if ( ! everythingOk() ) return;
...
...
m.unlock();
}
可以看到,上面这段代码是有问题的,原因是:那个 if 语句返回时没有把锁给 unlock 掉,这会导致锁没有被释放。如果我们要把代码写对,需要在 return 前 unlock 一下。
mutex m;
void foo() {
m.lock();
Func();
if ( ! everythingOk() ) {
m.unlock();
return;
}
...
m.unlock();
}
但是,在所有的函数退出的地方都要加上 m.unlock(); 语句,这会让我们很难维护代码。于是可以使用面向对象的编程模式,我们先设计一个代理类。
class lock_guard {
private:
mutex &_m;
public:
lock_guard(mutex &m):_m(m) { _m.lock(); }
~lock_guard() { _m.unlock(); }
};
然后,我们的代码就可以这样写了:
...用GDB调试下面的两个循环,为什么until命令可以退出while循环,却不能退出 for循环?
int i = 9999;
while (i--) {
...
}
int i;
for (i = 0; i < 10; i++) {
...
}
In fact, what until really does is execute until it reaches a machine instruction that has a higher memory address than the current one, rather than until it reaches a larger line number in the source code.
GCC编译for循环时,会把循环条件放在循环体的底部,因此,for循环体中的下一条语句是循环条件,从源角度来看,在for循环中使用until后又从新回到了 for语句上。