How to use 32-bit pointers in a 64-bit application?

Our school project only allows us to compile program c into a 64-bit application, and they test our program for speed and memory usage. However, if I can use 32-bit pointers, then my program will consume much less memory than 64-bit ones, maybe it will work faster (faster for malloc?)

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

thanks for the help

+6
source share
2 answers

this can reduce memory usage - slightly - but it will not improve speed, since you will have to move your short pointer to an absolute pointer, and this will add overhead, and you will lose most of the benefits of type checking.

It will look something like this:

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

with even more tricks, you can come up with your own brk () to help allocate memory from the offset.

Is it worth it? IMO no.

0
source

Using GCC?

The -mx32 parameter sets the int, long, and pointer types to 32 bits and generates code for the x86-64 architecture. (Intel 386 and AMD x86-64 options):

Then checkpoint :)

+27
source

All Articles