简体   繁体   English

在 C 中访问大于 UINT_MAX*4 大小的 memory?

[英]Accessing memory larger than UINT_MAX*4 size in C?

Assume if I have an array of size 78719476736 bytes.假设我有一个大小为 78719476736 字节的数组。 mind you that this array is dynamically allocated using malloc in my C code.请注意,这个数组是在我的 C 代码中使用 malloc 动态分配的。 Assume malloc returns a valid pointer after allocating this much memory. The size of this array is more than UINT_MAX(4294967295), ie max limit of a unsigned int(32 bits)假设 malloc 在分配了这么多 memory 后返回一个有效指针。这个数组的大小超过了 UINT_MAX(4294967295),即 unsigned int 的最大限制(32 位)

Assume my code looks like something below eg假设我的代码如下所示

int *buf;
buf = (int*)malloc(78719476736);

Here 78719476736 is greater than 4 * UINT_MAX.这里 78719476736 大于 4 * UINT_MAX。

Now if i have to refer to all the elements of buf, then since buf is int* it will be 32 bit, so it will not be able to address all the memory elements which i have allocated using malloc(78719476736 bytes).现在,如果我必须引用 buf 的所有元素,那么由于 buf 是 int*,它将是 32 位的,因此它将无法寻址我使用 malloc(78719476736 字节)分配的所有 memory 元素。

My question is shouldn't the code above be changed to make buf as long long(64 bit variable) as only a long long variable will be able to address the large memory that i have allocated.我的问题是,上面的代码是否应该更改为使 buf 尽可能长(64 位变量),因为只有 long long 变量才能解决我分配的大 memory。

Changed code eg更改代码例如

unsigned long long int buf;
buf = (unsigned long long int*)malloc(78719476736);

In fact i think, variable buf should not be a pointer any more as any pointer is going to be 32 bit wide and hence it will not be able to access 78719476736 bytes.事实上,我认为,变量 buf 不应该再是指针,因为任何指针都将是 32 位宽,因此它将无法访问 78719476736 字节。

So it should be a plain unsigned long long int and i will have to cast the malloc return pointer value to an unsigned long long int as shown in the changed code above and use buf to access all the elements allocated.所以它应该是一个普通的 unsigned long long int,我必须将 malloc 返回指针值转换为一个 unsigned long long int,如上面更改后的代码所示,并使用 buf 访问所有分配的元素。

Am i correct in my assumptions above?我上面的假设是否正确?

or要么

Am i confusing/missing something?我混淆/遗漏了什么吗?

EDIT: If it helps,编辑:如果有帮助,

I am working on a Desktop having WinXP on a Intel Core 2 Duo(64 bit CPU).我正在使用在 Intel Core 2 Duo(64 位 CPU)上安装 WinXP 的台式机。 So CPU wise it should not be a problem accessing more than 4 GB address space.所以从 CPU 的角度来看,访问超过 4 GB 的地址空间应该不是问题。 What all other components should be enabled for 64 bit support, ie应为 64 位支持启用哪些所有其他组件,即

a.) How do i enable compiler support for 64 bit while compiling(I am using Visual Studio 2005 Professional edition) a.) 如何在编译时启用编译器对 64 位的支持(我使用的是 Visual Studio 2005 专业版)

b.) OS support for 64 bit - I am using Windows XP Professional. b.) 64 位操作系统支持 - 我使用的是 Windows XP Professional。

Thank You.谢谢你。

-AD. -广告。

  1. You need 64 bit OS你需要 64 位操作系统
  2. malloc recieves size_t as parameter that is 64 bit on 64 bit platforms malloc 接收 size_t 作为 64 位平台上的 64 位参数
  3. And the most important: You prorbably should think: Do I need to allocate more then 4G of memory?最重要的是:您可能应该考虑:我是否需要分配超过 4G 的 memory?

I think you are deeply confused about what a pointer is.我认为您对指针是什么深感困惑。 On common systems at least, the size of a pointer (the number of different values, ie the number of different addresses) is independent of the type !至少在普通系统上,指针的大小(不同值的数量,即不同地址的数量)与类型无关!

int *a;
short *b;
double *c;

a, b and c are pointers to different types, but they all have the same size (4 bytes on a 32 bits system, for example). a、b 和 c 是指向不同类型的指针,但它们都具有相同的大小(例如,在 32 位系统上为 4 个字节)。 That's exactly the point of a 64 bits system compared to 32 bits: to be able to address more than 2**32 locations from a pointer.这正是 64 位系统与 32 位系统相比的要点:能够从一个指针寻址超过 2**32 个位置。

Also, a 32 bits CPU cannot address more than 4 Gb in hardware either [1], and the corresponding virtual address space is also generally to limited to 32 bits.此外,一个 32 位的 CPU 也不能在硬件中寻址超过 4 Gb [1],相应的虚拟地址空间通常也被限制为 32 位。 So mallocing 8 Gb of memory is quite unlikely to work anyway.因此 mallocing 8 Gb 的 memory 无论如何都不太可能工作。

[1] that's not entirely true - Intel CPU for example, have extensions so that a 32 bits CPU can use 'extended' addresses. [1] 这并不完全正确——例如,英特尔 CPU 具有扩展功能,因此 32 位 CPU 可以使用“扩展”地址。

An int* is a pointer type. int*是指针类型。 If you're on a system that can allocate 78719476736 bytes, it probably has at least 64 bit addresses ie sizeof(int*) >= 8 .如果你在一个可以分配 78719476736 字节的系统上,它可能至少有 64 位地址,即sizeof(int*) >= 8 Pointer size has nothing to do what sizeof(int) is.指针大小与sizeof(int)是什么无关。

To address more than 4GB of memory you need some mechanism of selecting that memory. Therefore you need to compile your application to be 64 bit, and then all of your pointers will be 64 bits wide.要解决超过 4GB 的 memory,您需要某种机制来选择 memory。因此,您需要将应用程序编译为 64 位,然后所有指针都将是 64 位宽。

The only other way to access more than 4GB of memory would be to use an additional address/memory selector mechanism much like you had with the segment and offset mess back in the real mode DOS days.访问超过 4GB 的 memory 的唯一其他方法是使用额外的地址/内存选择器机制,就像您在实模式 DOS 时代使用段和偏移量一样。 This of course depends on your processor architecture.这当然取决于您的处理器架构。

If you run your software on a 64-bit Os and use 64-bit compiler settings, all your pointers will be 64 bit.如果您在 64 位操作系统上运行您的软件并使用 64 位编译器设置,那么您的所有指针都将是 64 位。 No need for a special declaration.无需特别申报。

It's not possiable, your compiler and OS do not support that, period.这是不可能的,你的编译器和操作系统不支持那个,期间。

For windows, you can see the documentation of the _HEAP_MAXREQ .对于 windows,您可以查看_HEAP_MAXREQ的文档。

Read up on some options,阅读一些选项,

64 bit large mallocs 64 位大型 malloc

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

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