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

查看:32
本文介绍了如何在 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?

感谢您的帮助

推荐答案

你可以自己动手".以下内容可能会减少内存使用量 - 略微减少 - 但它可能不会提高速度,因为您必须将短指针转换为绝对指针,这会增加开销,并且您也会失去类型检查的大部分好处.

You could "roll you own". The following may reduce memory usage -- marginally -- but it may not 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.

值得吗?海事组织编号

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

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