I am doing the procedure of copying a block of memory and have to deal with blocks of raw memory in efficient pieces. My question is not about the specialized copy procedure that I create, but about how to properly examine the alignment of the source pointer in C.
I have a raw memory pointer, let's say it is already represented as a non-zero char *. In my architecture, I can very efficiently copy memory in 64 byte chunks, WHEN THIS IS CONFIRMED to a block of 64 bytes in size. So the (standard) trick is that I will make a simple copy of 0-63 bytes "manually" in the head and / or tail to convert the copy from an arbitrary char * of arbitrary length to a 64-byte aligned pointer with a multiple of 64 bytes in length.
Now the question arises: how do you legally "check" a pointer to the definition (and manipulation) of its alignment? The obvious way is to include it in an integer and just check the bits:
char *pointer=something. int p=(int)pointer; char *alignedPointer=(char *)((p+63)&~63);
Please note that here I understand that alignedPointer does not point to the same memory as the pointer ... it is a rounded pointer to which I can call my efficient copy procedure and I will process any other bytes to the beginning manually.
But compilers (justifiably) cause a craze when entering a pointer into an integer. But how else can I examine and manipulate the lower bits of a pointer in LEGAL C? Ideally, with different compilers, I would not receive any errors or warnings.
c pointers bit-manipulation alignment portability
SPWorley
source share