我想知道下一个代码.
#include <iostream>
using std::cout;
template<int N> struct Fib {
enum { v = Fib<N - 1>::v + Fib<N - 2>::v };
};
template<> struct Fib<0> {
enum { v = 0 };
};
template<> struct Fib<1> {
enum { v = 1 };
};
int fib(int n) {
return n < 2 ? n : fib(n - 1) + fib(n - 2);
}
int main() {
cout << Fib<46>::v << '\n';
// cout << fib(46) << '\n';
return 0; …Run Code Online (Sandbox Code Playgroud) 我在玩代码
struct A {
char a[20000];
A() { a[0] = 'A'; }
~A() {}
};
struct B : A {
char a[20000];
B() { a[0] = 'B'; }
~B() {}
};
int main() {
A *pA = new A;
A *pB = new B;
delete pA;
delete pB;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
有人写道(为什么我们需要一个带有动态内存的虚拟析构函数?)它应该引起内存泄漏,但事实并非如此。我使用g ++,然后使用valgrind --leak-check = full --show-leak-kinds = all --track-origins = yes --verbose --log-file = valgrind-out.txt并获取
HEAP SUMMARY:
in use at exit: 0 bytes …Run Code Online (Sandbox Code Playgroud)