visual studio c++分配内存无法分配(matlab可以分配)

amwbai8700 2013-12-09 06:28:53
我有一个matlab程序,可以运行,但是速度慢,现在要改成c程序。
c程序中,我按照要处理的数据大小(大概需要占用2GB),使用calloc给数据分配内存的时候就出错,不能分配,但是maltab程序是可以运行的(数据大小一样)。
听说matlab分配内存的时候有用了什么扩展技术是吗?有什么办法可以让我的visual studio 的c程序也可以分配这么大的内存呢?
...全文
331 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
mujiok2003 2013-12-10
  • 打赏
  • 举报
回复
引用 12 楼 amwbai8700 的回复:
[quote=引用 11 楼 mujiok2003 的回复:] [quote=引用 10 楼 amwbai8700 的回复:] [quote=引用 9 楼 buyong 的回复:] 你的是64位系统么?
8g内存,64位系统,4核cpu[/quote] 应用程序要编成64位的.[/quote] 什么意思?我要是取一小块数据出来,用c程序处理是可以处理的,但是数据大了就内存分配不了了[/quote] 64位CPU+OS上可以运行32位的应用程序,这样你的进程空间受限于2^32=4GB
amwbai8700 2013-12-10
  • 打赏
  • 举报
回复
引用 11 楼 mujiok2003 的回复:
[quote=引用 10 楼 amwbai8700 的回复:] [quote=引用 9 楼 buyong 的回复:] 你的是64位系统么?
8g内存,64位系统,4核cpu[/quote] 应用程序要编成64位的.[/quote] 什么意思?我要是取一小块数据出来,用c程序处理是可以处理的,但是数据大了就内存分配不了了
mujiok2003 2013-12-10
  • 打赏
  • 举报
回复
引用 10 楼 amwbai8700 的回复:
[quote=引用 9 楼 buyong 的回复:] 你的是64位系统么?
8g内存,64位系统,4核cpu[/quote] 应用程序要编成64位的.
amwbai8700 2013-12-10
  • 打赏
  • 举报
回复
引用 9 楼 buyong 的回复:
你的是64位系统么?
8g内存,64位系统,4核cpu
buyong 2013-12-10
  • 打赏
  • 举报
回复
你的是64位系统么?
amwbai8700 2013-12-10
  • 打赏
  • 举报
回复
引用 2 楼 truelance 的回复:
用malloc试一下. calloc会进行初始化, malloc不会初始化, 因此可以超分
malloc也不行
amwbai8700 2013-12-10
  • 打赏
  • 举报
回复
引用 3 楼 shanfeng00041 的回复:
如果4G的内存的话,malloc了2G的堆空间以后,也干不了别的了吧。
我电脑8g内存,我不需要干别的,只求能运行……
amwbai8700 2013-12-10
  • 打赏
  • 举报
回复
引用 1 楼 max_min_ 的回复:
2G的堆空间,这个。。。这个。。
我电脑是8g内存的,matlab时候能运行,虽然也是别的什么都干不了
amwbai8700 2013-12-10
  • 打赏
  • 举报
回复
引用 18 楼 mayadong7349 的回复:
额。如果看高清电影的时候播放器都按照电影文件尺寸分配内存,看高清电影也会把电脑累死。 或者考虑另外一种方案:虽然是一个大文件,可是每次只处理文件的一部分内容,那么可以考虑只分配所需空间即可(但可能要频繁地去读取数据)。实际上,对于这样大的文件,用内存映射文件是一种很好的选择。
有道理!内存映射文件是什么我还不知道,好像也是14楼讲的那个东西,我学学看
大道曙光 2013-12-10
  • 打赏
  • 举报
回复
额。如果看高清电影的时候播放器都按照电影文件尺寸分配内存,看高清电影也会把电脑累死。 或者考虑另外一种方案:虽然是一个大文件,可是每次只处理文件的一部分内容,那么可以考虑只分配所需空间即可(但可能要频繁地去读取数据)。实际上,对于这样大的文件,用内存映射文件是一种很好的选择。
大道曙光 2013-12-10
  • 打赏
  • 举报
回复
这文件太大了。 考虑内存映射文件不失为一种方法。 让我吃惊的是,64位编译器这么吊。
mujiok2003 2013-12-10
  • 打赏
  • 举报
回复
引用 15 楼 amwbai8700 的回复:
[quote=引用 13 楼 mujiok2003 的回复:] [quote=引用 12 楼 amwbai8700 的回复:] [quote=引用 11 楼 mujiok2003 的回复:] [quote=引用 10 楼 amwbai8700 的回复:] [quote=引用 9 楼 buyong 的回复:] 你的是64位系统么?
8g内存,64位系统,4核cpu[/quote] 应用程序要编成64位的.[/quote] 什么意思?我要是取一小块数据出来,用c程序处理是可以处理的,但是数据大了就内存分配不了了[/quote] 64位CPU+OS上可以运行32位的应用程序,这样你的进程空间受限于2^32=4GB[/quote] 原来是要把活动方案平台改成x64的!真的可以运行了!你太棒了!!![/quote] 碰巧知道这个.
amwbai8700 2013-12-10
  • 打赏
  • 举报
回复
引用 13 楼 mujiok2003 的回复:
[quote=引用 12 楼 amwbai8700 的回复:] [quote=引用 11 楼 mujiok2003 的回复:] [quote=引用 10 楼 amwbai8700 的回复:] [quote=引用 9 楼 buyong 的回复:] 你的是64位系统么?
8g内存,64位系统,4核cpu[/quote] 应用程序要编成64位的.[/quote] 什么意思?我要是取一小块数据出来,用c程序处理是可以处理的,但是数据大了就内存分配不了了[/quote] 64位CPU+OS上可以运行32位的应用程序,这样你的进程空间受限于2^32=4GB[/quote] 原来是要把活动方案平台改成x64的!真的可以运行了!你太棒了!!!
赵4老师 2013-12-10
  • 打赏
  • 举报
回复
CreateFileMapping The CreateFileMapping function creates a named or unnamed file-mapping object for the specified file. HANDLE CreateFileMapping( HANDLE hFile, // handle to file to map LPSECURITY_ATTRIBUTES lpFileMappingAttributes, // optional security attributes DWORD flProtect, // protection for mapping object DWORD dwMaximumSizeHigh, // high-order 32 bits of object size DWORD dwMaximumSizeLow, // low-order 32 bits of object size LPCTSTR lpName // name of file-mapping object ); Parameters hFile Handle to the file from which to create a mapping object. The file must be opened with an access mode compatible with the protection flags specified by the flProtect parameter. It is recommended, though not required, that files you intend to map be opened for exclusive access. If hFile is (HANDLE)0xFFFFFFFF, the calling process must also specify a mapping object size in the dwMaximumSizeHigh and dwMaximumSizeLow parameters. The function creates a file-mapping object of the specified size backed by the operating-system paging file rather than by a named file in the file system. The file-mapping object can be shared through duplication, through inheritance, or by name. lpFileMappingAttributes Pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by child processes. If lpFileMappingAttributes is NULL, the handle cannot be inherited. Windows NT: The lpSecurityDescriptor member of the structure specifies a security descriptor for the new file-mapping object. If lpFileMappingAttributes is NULL, the file-mapping object gets a default security descriptor. flProtect Specifies the protection desired for the file view, when the file is mapped. This parameter can be one of the following values: Value Description PAGE_READONLY Gives read-only access to the committed region of pages. An attempt to write to or execute the committed region results in an access violation. The file specified by the hFile parameter must have been created with GENERIC_READ access. PAGE_READWRITE Gives read-write access to the committed region of pages. The file specified by hFile must have been created with GENERIC_READ and GENERIC_WRITE access. PAGE_WRITECOPY Gives copy on write access to the committed region of pages. The files specified by the hFile parameter must have been created with GENERIC_READ and GENERIC_WRITE access. In addition, an application can specify certain section attributes by combining (using the bitwise OR operator) one or more of the following section attribute values with one of the preceding page protection values: Value Description SEC_COMMIT Allocates physical storage in memory or in the paging file on disk for all pages of a section. This is the default setting. SEC_IMAGE The file specified for a section's file mapping is an executable image file. Because the mapping information and file protection are taken from the image file, no other attributes are valid with SEC_IMAGE. SEC_NOCACHE All pages of a section are to be set as non-cacheable. This attribute is intended for architectures requiring various locking structures to be in memory that is never fetched into the processor's. On 80x86 and MIPS machines, using the cache for these structures only slows down the performance as the hardware keeps the caches coherent. Some device drivers require noncached data so that programs can write through to the physical memory. SEC_NOCACHE requires either the SEC_RESERVE or SEC_COMMIT to also be set. SEC_RESERVE Reserves all pages of a section without allocating physical storage. The reserved range of pages cannot be used by any other allocation operations until it is released. Reserved pages can be committed in subsequent calls to the VirtualAlloc function. This attribute is valid only if the hFile parameter is (HANDLE)0xFFFFFFFF; that is, a file mapping object backed by the operating sytem paging file. dwMaximumSizeHigh Specifies the high-order 32 bits of the maximum size of the file-mapping object. dwMaximumSizeLow Specifies the low-order 32 bits of the maximum size of the file-mapping object. If this parameter and dwMaximumSizeHig are zero, the maximum size of the file-mapping object is equal to the current size of the file identified by hFile. lpName Pointer to a null-terminated string specifying the name of the mapping object. The name can contain any character except the backslash character (\). If this parameter matches the name of an existing named mapping object, the function requests access to the mapping object with the protection specified by flProtect. If this parameter is NULL, the mapping object is created without a name. If lpName matches the name of an existing event, semaphore, mutex, waitable timer, or job, the function fails and the GetLastError function returns ERROR_INVALID_HANDLE. This occurs because these objects share the same name space. Return Values If the function succeeds, the return value is a handle to the file-mapping object. If the object existed before the function call, the function returns a handle to the existing object (with its current size, not the specified size) and GetLastError returns ERROR_ALREADY_EXISTS. If the function fails, the return value is NULL. To get extended error information, call GetLastError. Remarks After a file-mapping object has been created, the size of the file must not exceed the size of the file-mapping object; if it does, not all of the file's contents will be available for sharing. If an application specifies a size for the file-mapping object that is larger than the size of the actual named file on disk, the file on disk is grown to match the specified size of the file-mapping object. If the file cannot be grown, this results in a failure to create the file-mapping object. GetLastError will return ERROR_DISK_FULL. The handle that CreateFileMapping returns has full access to the new file-mapping object. It can be used with any function that requires a handle to a file-mapping object. File-mapping objects can be shared either through process creation, through handle duplication, or by name. For information on duplicating handles, see DuplicateHandle. For information on opening a file-mapping object by name, see OpenFileMapping. Windows 95: File handles that have been used to create file-mapping objects must not be used in subsequent calls to file I/O functions, such as ReadFile and WriteFile. In general, if a file handle has been used in a successful call to the CreateFileMapping function, do not use that handle unless you first close the corresponding file-mapping object. Creating a file-mapping object creates the potential for mapping a view of the file but does not map the view. The MapViewOfFile and MapViewOfFileEx functions map a view of a file into a process's address space. With one important exception, file views derived from a single file-mapping object are coherent, or identical, at a given time. If multiple processes have handles of the same file-mapping object, they see a coherent view of the data when they map a view of the file. The exception has to do with remote files. Although CreateFileMapping works with remote files, it does not keep them coherent. For example, if two computers both map a file as writeable, and both change the same page, each computer will only see its own writes to the page. When the data gets updated on the disk, it is not merged. A mapped file and a file accessed by means of the input and output (I/O) functions (ReadFile and WriteFile) are not necessarily coherent. To fully close a file mapping object, an application must unmap all mapped views of the file mapping object by calling UnmapViewOfFile, and close the file mapping object handle by calling CloseHandle. The order in which these functions are called does not matter. The call to UnmapViewOfFile is necessary because mapped views of a file mapping object maintain internal open handles to the object, and a file mapping object will not close until all open handles to it are closed. Note To guard against an access violation, use structured exception handling to protect any code that writes to or reads from a memory mapped view. For more information, see Reading and Writing. Windows CE: Windows CE does not use the lpFileMappingAttributes parameter. It must be NULL. This function will not work on a device that does not support Page-In. Example To implement a mapping-object creation function that fails if the object already exists, an application can use the following code. hMap = CreateFileMapping(...); if (hMap != NULL && GetLastError() == ERROR_ALREADY_EXISTS) { CloseHandle(hMap); hMap = NULL; } return hMap; QuickInfo Windows NT: Requires version 3.1 or later. Windows: Requires Windows 95 or later. Windows CE: Requires version 1.0 or later. Header: Declared in winbase.h. Import Library: Use kernel32.lib. Unicode: Implemented as Unicode and ANSI versions on Windows NT. See Also File Mapping Overview, File Mapping Functions, CloseHandle, DuplicateHandle, MapViewOfFile, MapViewOfFileEx, MapViewOfFileVlm, OpenFileMapping, ReadFile, SECURITY_ATTRIBUTES, UnmapViewOfFile, UnmapViewOfFileVlm, VirtualAlloc, WriteFile
碼上道 2013-12-09
  • 打赏
  • 举报
回复
用virtualqeury查询线性空间是否有这么大的连续空间吧
taniz 2013-12-09
  • 打赏
  • 举报
回复
大数据还是静态分配吧
signforlin 2013-12-09
  • 打赏
  • 举报
回复
如果4G的内存的话,malloc了2G的堆空间以后,也干不了别的了吧。
熊熊大叔 2013-12-09
  • 打赏
  • 举报
回复
用malloc试一下. calloc会进行初始化, malloc不会初始化, 因此可以超分
max_min_ 2013-12-09
  • 打赏
  • 举报
回复
2G的堆空间,这个。。。这个。。

65,210

社区成员

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

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