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

发布于 2024-07-24 21:54:56 字数 1130 浏览 7 评论 0原文

假设我有一个大小为 78719476736 字节的数组。 请注意,该数组是在我的 C 代码中使用 malloc 动态分配的。 假设 malloc 在分配这么多内存后返回一个有效的指针。 该数组的大小大于 UINT_MAX(4294967295) ,即无符号整数的最大限制(32 位)

假设我的代码如下所示,例如

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

这里 78719476736 大于 4 * UINT_MAX。

现在,如果我必须引用 buf 的所有元素,那么由于 buf 是 int* ,它将是 32 位,因此它将无法寻址我使用 malloc 分配的所有内存元素(78719476736 字节)。

我的问题是,上面的代码是否应该更改为使 buf 与 long long(64 位变量)一样长,因为只有 long long 变量才能寻址我分配的大内存。

更改代码,例如

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

事实上,我认为变量 buf 不应该再是指针,因为任何指针都将是 32 位宽,因此它将无法访问 78719476736 字节。

所以它应该是一个普通的 unsigned long long int ,我必须将 malloc 返回指针值转换为 unsigned long long int ,如上面更改的代码所示,并使用 buf 来访问分配的所有元素。

我的上述假设正确吗?

或者

我是否感到困惑/遗漏了什么?

编辑:如果有帮助的话,

我正在使用 Intel Core 2 Duo(64 位 CPU)上安装 WinXP 的台式机。 因此,就 CPU 而言,访问超过 4 GB 地址空间应该不成问题。 应启用哪些所有其他组件来支持 64 位,即

a.) 如何在编译时启用对 64 位的编译器支持(我使用的是 Visual Studio 2005 专业版)

b.) 对 64 位的操作系统支持 - 我使用的是 Windows XP 专业版。

谢谢。

-广告。

Assume if I have an array of size 78719476736 bytes. mind you that this array is dynamically allocated using malloc in my C code. Assume malloc returns a valid pointer after allocating this much memory. The size of this array is more than UINT_MAX(4294967295) , i.e. max limit of a unsigned int(32 bits)

Assume my code looks like something below e.g.

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

Here 78719476736 is greater than 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).

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.

Changed code e.g.

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.

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.

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). So CPU wise it should not be a problem accessing more than 4 GB address space. What all other components should be enabled for 64 bit support , i.e.

a.) How do i enable compiler support for 64 bit while compiling(I am using Visual Studio 2005 Professional edition)

b.) OS support for 64 bit - I am using Windows XP Professional.

Thank You.

-AD.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

你不是我要的菜∠ 2024-07-31 21:54:57

我认为您对指针是什么感到非常困惑。 至少在常见系统上,指针的大小(不同值的数量,即不同地址的数量)与类型无关!

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

a、b 和 c 是指向不同类型的指针,但它们都具有相同的大小(例如,在 32 位系统上为 4 个字节)。 这正是 64 位系统与 32 位系统相比的要点:能够从指针寻址超过 2**32 个位置。

此外,32 位 CPU 也无法在硬件中寻址超过 4 Gb [1],并且相应的虚拟地址空间通常也限制为 32 位。 因此,无论如何,分配 8 Gb 内存不太可能起作用。

[1] 这并不完全正确 - 例如,Intel CPU 具有扩展功能,以便 32 位 CPU 可以使用“扩展”地址。

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, i.e. 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). 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.

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. So mallocing 8 Gb of memory is quite unlikely to work anyway.

[1] that's not entirely true - Intel CPU for example, have extensions so that a 32 bits CPU can use 'extended' addresses.

忘羡 2024-07-31 21:54:57

int* 是指针类型。 如果您使用的系统可以分配 78719476736 字节,则它可能至少有 64 位地址,即 sizeof(int*) >= 8。 指针大小与 sizeof(int) 无关。

An int* is a pointer type. If you're on a system that can allocate 78719476736 bytes, it probably has at least 64 bit addresses i.e. sizeof(int*) >= 8. Pointer size has nothing to do what sizeof(int) is.

戏舞 2024-07-31 21:54:57

要寻址超过 4GB 的内存,您需要某种选择内存的机制。 因此,您需要将应用程序编译为 64 位,然后所有指针都将是 64 位宽。

访问超过 4GB 内存的唯一其他方法是使用额外的地址/内存选择器机制,就像在实模式 DOS 时代使用段和偏移量混乱一样。 这当然取决于您的处理器架构。

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.

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. This of course depends on your processor architecture.

陪我终i 2024-07-31 21:54:57

如果您在 64 位操作系统上运行软件并使用 64 位编译器设置,则所有指针都将是 64 位的。 无需特别声明。

If you run your software on a 64-bit Os and use 64-bit compiler settings, all your pointers will be 64 bit. No need for a special declaration.

国产ˉ祖宗 2024-07-31 21:54:57

这是不可能的,您的编译器和操作系统不支持这一点。

对于 Windows,您可以查看 _HEAP_MAXREQ 的文档。

阅读一些选项,

64 位大型 malloc

It's not possiable, your compiler and OS do not support that, period.

For windows, you can see the documentation of the _HEAP_MAXREQ.

Read up on some options,

64 bit large mallocs

娇纵 2024-07-31 21:54:56
  1. 您需要 64 位操作系统
  2. malloc 接收 size_t 作为 64 位平台上的 64 位参数
  3. 最重要的是: 您可能应该思考:我是否需要分配超过 4G 的内存?
  1. You need 64 bit OS
  2. malloc recieves size_t as parameter that is 64 bit on 64 bit platforms
  3. And the most important: You prorbably should think: Do I need to allocate more then 4G of memory?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文