简体   繁体   English

在Windows 7 64位中开发32位C ++应用程序

[英]Developing 32-bit C++ application in Windows 7 64-bit

I develop a Win32 application in Visual Studio 2008 (C++). 我在Visual Studio 2008(C ++)中开发了Win32应用程序。 It runs fine in either 32-bit or 64-bit Windows 7. However, sometimes I need to allocate quite big memory buffers (the application deals with lots of data), and if I do it in Windows 7 64-bit, it fails, in 32-bit it runs fine. 它可以在32位或64位Windows 7中正常运行。但是,有时我需要分配很大的内存缓冲区(该应用程序处理大量数据),并且如果我在64位Windows 7中运行它,则它将失败,在32位中运行正常。 By big memory buffers I mean one ~250MB and another ~150MB. 大内存缓冲区指的是一个〜250MB,另一个〜150MB。 I have 8GB RAM installed in my PC, and according to my information, the 64-bit OS makes 4GB availabla for a 32-bit application. 我的PC中安装了8GB RAM,根据我的信息,该64位操作系统为32位应用程序提供了4GB可用空间。 I need nowhere near that limit, still malloc fails. 我不需要接近该限制的任何地方,但malloc仍然失败。 Any ideas why and what can I do about it? 有任何想法为什么以及对此我该怎么办? Thanks in advance. 提前致谢。

You are hitting virtual address space limit in your Win32 binary. 您正在Win32二进制文件中达到虚拟地址空间限制。 The limit might be 2 to 4 GB depending on OS and environment . 根据操作系统和环境的不同,该限制可能是2到4 GB Actualy limit is less due to allocation fragmentation. 由于分配分散,实际限制较少。

Your choices are to: 您的选择是:

  • live within space you can effectively allocate 生活在可以有效分配的空间内
  • move to x64 移至x64
  • use multiple processes 使用多个过程
  • use memory allocation API which allows unmapping/re-mapping blocks into application virtual address space, see File Mappings: Virtual Memory and Virtual Address Space for details on this 使用内存分配API,该API允许将块取消映射/重新映射到应用程序虚拟地址空间,请参阅文件映射:虚拟内存和虚拟地址空间以获取有关此内容的详细信息

150MB and 250MB are not especially huge allocations. 150MB和250MB并不是特别大的分配。 As others have noted, the problem you are hitting is most likely address space fragmentation (ie there is enough free space, it's just not all in one piece). 正如其他人所指出的那样,您遇到的问题很可能是地址空间碎片化(即,有足够的可用空间,而并非一成不变)。

In addition to the other suggestions: 除了其他建议:

  • Allocate the memory as soon as the program starts. 程序启动后立即分配内存。 The address space will be less likely to be fragmented. 地址空间将不太可能被分割。 If it wouldn't be sensible to allocate this much memory for the life of the program, use VirtualAlloc with the MEM_RESERVE flag to reserve the address space and commit it later. 如果在程序生命周期内分配这么多的内存MEM_RESERVE VirtualAllocMEM_RESERVE标志一起使用以保留地址空间并在以后提交。
  • If address space is fragmented as soon as the process starts, it's probably caused by DLLs loading at unhelpful locations. 如果进程一开始就将地址空间碎片化,则可能是由于DLL加载到无用的位置引起的。 You can use VMMap to see what's happening in the address space. 您可以使用VMMap来查看地址空间中发生的情况。 If DLLs belonging to you are fragmenting the address space you can rebase them. 如果属于您的DLL碎片化了地址空间,则可以对其重新设置基准。

I think there is a bug in malloc under win7-64. 我认为在win7-64下的malloc中有一个错误。 I have run the same tests with my 32-bit app on three machines: XP32, w7-32 and win7-64. 我已经在三台机器上用32位应用程序运行了相同的测试:XP32,w7-32和win7-64。 It runs OK on the 32bit platforms but fails to allocate a 110Mb block under w7-64. 它可以在32位平台上正常运行,但无法在w7-64下分配110Mb的块。 I have de-fragmented my drive and tried again from a clean reboot with the same result. 我对驱动器进行了碎片整理,并从干净的重新启动中再次尝试,但结果相同。

K ķ

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM