更新时间:2021-08-24 02:43:05
它可能会稍微减少内存使用量-但不会提高速度,因为您必须将短指针转换为绝对指针,并且会增加开销,而且您还会失去类型检查的大部分好处.
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.