kev*_*vin 8 c pointers 32bit-64bit
我们学校的项目只允许我们将c程序编译成64位应用程序,并测试我们的程序速度和内存使用情况.但是,如果我能够使用32位指针,那么我的程序将比64位消耗更少的内存,也许它运行得更快(更快到malloc?)
我想知道我是否可以在64位应用程序中使用32位指针?
谢谢您的帮助
Mor*_*pfh 32
使用GCC?
-mx32选项将int,long和指针类型设置为32位,并为x86-64体系结构生成代码.(Intel 386和AMD x86-64选项):
然后基准:)
你可以“滚动你自己的”。以下内容可能会稍微减少内存使用量,但可能不会提高速度,因为您必须将短指针转换为绝对指针,这会增加开销,而且您也会失去类型检查的大部分好处。
它看起来像这样:
typedef unsigned short ptr;
...
// pre-allocate all memory you'd ever need
char* offset = malloc(256); // make sure this size is less than max unsigned int
// these "pointers" are 16-bit short integer, assuming sizeof(int) == 4
ptr var1 = 0, var2 = 4, var3 = 8;
// how to read and write to those "pointer", you can hide these with macros
*((int*) &offset[var1]) = ((int) 1) << 16;
printf("%i", *((int*) &offset[var1]));
Run Code Online (Sandbox Code Playgroud)
通过更多技巧,您可以发明自己的 brk() 来帮助从偏移量分配内存。
这值得么?国际海事组织没有。