Win32 compiler option and memory allocation

1

I tried to compile the following code:

extern "C" {
    #include "netcdf.h"
}

int main() {
    const int Ntime = 336;
    const int Nlon = 1442;
    const int Nlat = 1021;
    double* dhsum_vals = new double[Ntime * Nlat * Nlon];
}

When compiling with the 32-bit version, I get the error C2148 "total size of array must not exceed 0x7fffffff bytes". I think, the problem here is that a 32-program does not allow to use as much memory as I want here. If I compile with the 64-bit version, I get the error, that netcdf cannot be found as I downloaded the 32-bit version. So, my solution will be to download the 64-bit version of the netcdf library. However, my intention, when taking the 32-bit version, was that it should be more compatible. Is there another solution for my problem than taking the 64-bit version of netcdf?

c++
compatibility
asked on Stack Overflow Jul 14, 2020 by MPB94

1 Answer

3

On Microsoft Windows, the virtual address space of a 32-bit process is limited to 2^32 bytes, which is about 4 GB. However, the higher 2 GB are reserved by the system, so that you effectively only have about 2 GB of address space.

Also, this address space is fragmented, so you will not be able to allocate 2 GB in one consecutive chunk. You may be able to get up to 2 GB if you try several smaller memory allocations.

Although it is possible for a 32-bit process to use more than 4 GB of memory using the Address Windowing Extensions API, this requires special security privileges and makes your code unnecessarily complex, as you cannot have all the memory you are using mapped into your virtual address space at once. Therefore, I strongly suggest that you compile a 64-bit version of your application instead, when you are handling large amounts of memory.

I don't think that using 64-bit will make your program any less compatible, as nearly all modern hardware and operating systems support it.

answered on Stack Overflow Jul 14, 2020 by Andreas Wenzel • edited Jul 14, 2020 by Andreas Wenzel

User contributions licensed under CC BY-SA 3.0