#include <iostream>
using namespace std;
int main()
{
int *a = new int;
for (int i = 0; i < 20; i++)
{
*(a + i) = 9;
cout << *(a + i) << endl;
}
int b[1];
int *p = (int *)&b[1];
*p = 1;
for (int i = 0; i < 3; i++)
{
*(p + i) = i;//此处会报错的,栈里面的空间由系统维护,不能随便越界访问。
cout << *p << endl;
}
//我去,栈里面开辟的空间在windows是不能越界访问的,堆里面开辟的空间可以任意访问,
//但是前提该块内存没有被使用,不然会崩溃。
//但是linux里面在栈里面开辟的空间却是可以越界访问的,但是情况是未知的。
return 0;
}
//引用计数智能指针总结。
#include <iostream>
using namespace std;
template<typename T>
class scoped_ptr
{
public:
scoped_ptr(T *pt)
{
ptr = pt;
count = new int[1];
count[0]++;
}
scoped_ptr(const scoped_ptr<T> &sc)
{
ptr = sc.ptr;
count = sc.count;
count[0]++;
}
scoped_ptr &operator = (const scoped_ptr<T> &sc)
{
if (this != &sc)
{
if (ptr != NULL)
{
this->~scoped_ptr();
}
ptr = sc.ptr;
count = sc.count;
count[0]++;
}
return *this;
}
~scoped_ptr()
{
if (count[0] == 1)
{
delete ptr;
ptr = NULL;
delete[] count;
count = NULL;
}
}
T operator*()
{
return *ptr;
}
T* operator->()
{
return ptr;
}
private:
T *ptr;
int *count;
};
int main()
{
scoped_ptr<int> sc(new int(100));
cout << *sc << endl;
return 0;
}
笔试题:知识点总结
最新推荐文章于 2018-09-19 22:47:57 发布