【发布时间】:2011-12-30 21:45:55
【问题描述】:
我尝试实现外部合并排序 (wiki),我想打开 2048 个 ifstream 并将数据读取到个人缓冲区。
ifstream *file;
file = (ifstream *)malloc(2048 * sizeof(ifstream));
for (short i = 0; i < 2048; i++) {
itoa(i, fileName + 5, 10);
file[i].open(fileName, ios::in | ios::binary); // Access violation Error
if (!file[i]) {
cout << i << ".Bad open file" << endl;
}
if (!file[i].read((char*)perfile[i], 128*4)) {
cout << i << ". Bad read source file" << endl;
}
}
但是,它会因
而崩溃sorting.exe 中 0x58f3a5fd (msvcp100d.dll) 处未处理的异常:0xC0000005:访问冲突读取位置 0xcdcdcdfd。
是否可以使用这么多打开的ifstreams? 或者说开2048个ifstreams是个很糟糕的主意,还有更好的方法来实现这个算法?
【问题讨论】:
-
访问冲突发生时
i的值是多少? -
可能是因为你
malloc而不是new,所以ifstreams的构造函数没有被调用,所以有问题。 -
@Shahbaz:
i的值将是 0 - 令人惊讶
标签: c++ heap-memory ifstream access-violation