Example code The Art of C++

a10002 2005-06-18 12:11:33
#include <iostream>
#include <new>
#include "gc.h"

using namespace std;

int main() {
GCPtr<int> p;

try {
p = new int;
} catch(bad_alloc exc) {
cout << "Allocation failure!\n";
return 1;
}

*p = 88;

cout << "Value at p is: " << *p << endl;

int k = *p;

cout << "k is " << k << endl;

return 0;
}
其中gc.h比较长就不贴了!
http://shop.osborne.com/cgi-bin/osborne/0072255129.html可下载到代码!
代码是不会有问题的,可是我在VC6.0中却怎么也用不了!
Windows 32 Application
Windows 32 Console Application
MFC
都试过了!就是不知要怎么才能让它可以运行!
而且单独对gc.h编译都通不过!

...全文
194 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
a10002 2005-06-18
  • 打赏
  • 举报
回复
最好帮我发过工程过来!
niit_lwb@yahoo.com
a10002 2005-06-18
  • 打赏
  • 举报
回复
代码在:CHAP2.LST中,我贴出来的是listing 2, listing 1是gc.h
luler 2005-06-18
  • 打赏
  • 举报
回复
java?
holyfire 2005-06-18
  • 打赏
  • 举报
回复
VC6.0对标准C++支持不太好,换高版本的VC2003或者用GCC试试看吧
a10002 2005-06-18
  • 打赏
  • 举报
回复
原书上说的就是用VC来编译运行的!
gclist.remove(*p);不是释放内存,而对应的iterator中对对象的引用!
delete[] p->memPtr; // delete array 才是.
我现在把这一句放到后面可以正常运行了!
不过仍有一事不明,
到底是先放掉内存还是先放掉引用!
像现在这样万一放掉内存后出现异常,引用没有被放那不更严重!
先放引用再话内存的话,程序更本就不知去哪里放内存,也就是出现像我这样的情况!

不过这个问题总算解决了!


JohnTitor 2005-06-18
  • 打赏
  • 举报
回复
原书用可能用的不是microsoft的c++库
JohnTitor 2005-06-18
  • 打赏
  • 举报
回复
问题在于下面的语句
// Remove unused entry from gclist.
gclist.remove(*p);
remove内部已经把*p的内存释放掉了,也就是说在该函数返回后,
p->memPtr
p->isArray
以及p->arraySize都已经是无效的了,而由于p->isArray是一个bool变量,其变成无效后
被置为一个缺省值true。
这个问题可能和list的实现有关,可能是一个ownership搞不清楚的问题吧,
应该参照一下原书所用的c++运行时库
a10002 2005-06-18
  • 打赏
  • 举报
回复
p->isArray也无法直接看出是true与false
我觉得这个有点像宏
a10002 2005-06-18
  • 打赏
  • 举报
回复
JohnTitor(努力学习)
正如你说的,现在错误的地方我贴了出来!
a10002 2005-06-18
  • 打赏
  • 举报
回复
现在可以运行了,但总是有异常!p->isArray只是存在数组时才为true的!可我没有用数组,也没什么地方
把它赋为true的!p->isArray也无法直接看出是true与false,可实行的总是第一段.后来我让它直接执行
p->isArray为false的情况,却在*(T *) p->memPtr出错,p->memPtr的返回值是T *,而且程序中也存在
*()这个运算符函数.问题就可能是P的原因了!p是个iterator,
======================================
代码如下:
list<GCInfo<T> >::iterator p;
do {

// Scan gclist looking for unreferenced pointers.
for(p = gclist.begin(); p != gclist.end(); p++) {
// If in-use, skip.
if(p->refcount > 0) continue;

memfreed = true;

// Remove unused entry from gclist.
gclist.remove(*p);

// Free memory unless the GCPtr is null.
if(p->memPtr) {
if(p->isArray) {
#ifdef DISPLAY
cout << "Deleting array of size "
<< p->arraySize << endl;
#endif
delete[] p->memPtr; // delete array
}
else {
#ifdef DISPLAY
cout << "Deleting: "
<< *(T *) p->memPtr << "\n";
#endif
delete p->memPtr; // delete single element
}
}

// Restart the search.
break;
}

} while(p != gclist.end());
=================================================================
附加 GCInfo 类的代码
// This class defines an element that is stored
// in the garbage collection information list.
//
template <class T> class GCInfo {
public:
unsigned refcount; // current reference count

T *memPtr; // pointer to allocated memory

/* isArray is true if memPtr points
to an allocated array. It is false
otherwise. */
bool isArray; // true if pointing to array

/* If memPtr is pointing to an allocated
array, then arraySize contains its size */
unsigned arraySize; // size of array

// Here, mPtr points to the allocated memory.
// If this is an array, then size specifies
// the size of the array.
GCInfo(T *mPtr, unsigned size=0) {
refcount = 1;
memPtr = mPtr;
if(size != 0)
isArray = true;
else
isArray = false;

arraySize = size;
}
};
JohnTitor 2005-06-18
  • 打赏
  • 举报
回复
Windows 32 Console Application
可以编译通过,但是运行时在garbage collect就出错了
NetGeek 2005-06-18
  • 打赏
  • 举报
回复
namespace是什么意思?
MagicCarmack 2005-06-18
  • 打赏
  • 举报
回复
楼主问题没有说清楚吧

64,676

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧