如何在64位应用程序中使用32位指针? [英] How to use 32-bit pointers in 64-bit application?

查看:830
本文介绍了如何在64位应用程序中使用32位指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们学校的项目仅允许我们将c程序编译为64位应用程序,并且他们测试我们的程序的速度和内存使用情况.但是,如果我能够使用32位指针,那么我的程序将比64位指针消耗更少的内存,也许它运行得更快(到malloc更快?)

Our school's project only allows us to compile the c program into 64-bit application and they test our program for speed and memory usage. However, if I am able to use 32-bit pointers, then my program will consume much less memory than in 64-bit, also maybe it runs faster (faster to malloc?)

我想知道我是否可以在64位应用程序中使用32位指针?

I am wondering if I can use 32-bit pointers in 64-bit applications?

感谢您的帮助

推荐答案

它可能会稍微减少内存使用量-但不会提高速度,因为您必须将短指针转换为绝对指针,并且会增加开销,而且您还会失去类型检查的大部分好处.

it may reduce memory usage -- marginally -- but it won't improve speed as you'd have to translate your short pointer to absolute pointer, and that adds overhead, also you lose most of the benefits of typechecking.

它看起来像这样:

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]));

更多技巧,您可以发明自己的brk()来帮助从偏移量分配内存.

with a bit more tricks, you can invent your own brk() to help allocating the memory from the offset.

值得吗? IMO编号.

Is it worth it? IMO no.

这篇关于如何在64位应用程序中使用32位指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆