为什么我可以使用一个大的缓冲区作为一个矢量,而不是与Windows上的新
Why can I use a large buffer as a vector but not with new on Windows?
我使用的是64位Windows 7 Pro和Visual Studio 2010 Pro。
我正在尝试分配和使用大于4gb的缓冲区(用于高数据速率数据捕获)。
将缓冲区作为字节向量进行分配和写入工作正常。将缓冲区分配为字节数组工作得很好,但是写入该数组很快就会崩溃。(最后打印的消息是"buffer allocated")
注释掉vector部分并不能解决问题。
下面是我的测试程序:
#include <iostream>
#include <vector>
#include <BaseTsd.h>
using namespace std;
int main() {
const ULONG64 BUF_SIZE = 4 * 1024ULL * 1024ULL * 1024ULL;
{
vector<unsigned __int8> v(BUF_SIZE);
cout << "vector allocated" << endl;
for (ULONG64 i = 0; i < BUF_SIZE; ++i) {
v[i] = 0xff;
}
cout << "vector written" << endl;
}
{
unsigned __int8* buffer = new unsigned __int8[BUF_SIZE];
cout << "buffer allocated" << endl;
for (ULONG64 i = 0; i < BUF_SIZE; ++i) {
buffer[i] = 0xff;
}
cout << "buffer written" << endl;
delete[] buffer;
}
return 0;
}
更新:我相信这是一个编译器错误。在这里看到的:http://connect.microsoft.com/VisualStudio/feedback/details/553756/invalid-check-for-maximum-array-size-in-x64-compiler-c2148
我刚刚尝试用VS2010 Pro(64位版本)编译给定的代码,编译器为new
调用产生了C2148错误:
error C2148: total size of array must not exceed 0x7fffffff bytes
我在运行vcvarsx86_amd64.bat
之后从命令行编译了它。这里给出的极限似乎在某种程度上起作用了。将new
更改为[BUF_SIZE-1]
允许它编译和运行(尽管这仍然大于那些链接中讨论的0x7fffffff数)。
相关文章:
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 有没有办法让我编写一个可以在Windows和Linux上运行的命令行游戏?
- 互斥,Windows 10,c ++,在一个线程上获取,在另一个线程上发布
- 如何创建一个windows visual Studio C++/CX NOT UWP
- 是否有一个Windows驱动程序函数可以执行Windows文件api SeFileAttributes的等效功能
- 无法使我的第一个Windows OpenGL窗口抬起并运行
- 使用DllMain函数(MinGW+CMake)创建一个Windows共享库
- 如何将消息从一个 Windows 控制台应用程序发送到另一个应用程序
- 如何制作一个Windows程序,用于使用c ++将文件写入cd
- 引用另一个 Windows Phone 运行时组件项目中一个 Windows Phone 运行时组件项目中的C++类
- 如何将数据从一个windows应用程序复制到另一个
- 是否可以编写一个windows应用程序,当在另一个窗口应用程序中选择文本时,该应用程序会收到通知
- 我需要一个Windows 8 WinRT应用程序来与用C编写的独立应用程序通信
- 有可能用cygwin编译一个windows服务吗?
- 是否有一个Windows c++ API来验证Windows用户名/域名是本地机器上的有效帐户(没有密码)
- 是否有一个Windows API来检测USB过流
- Visual studio项目模板,在c++中创建一个windows服务
- 我如何得到一个windows文本winapi c++
- 如何在C++中可靠地检查一个Windows进程是否是另一个进程的父进程
- 我可以为那些知道互斥锁密码的进程全局创建一个Windows互斥锁吗?