如何在64位应用程序中使用32位指针?

编程入门 行业动态 更新时间:2024-10-28 22:24:00
本文介绍了如何在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位指针?

本文发布于:2023-07-28 04:50:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1227589.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指针   应用程序   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!