简体   繁体   中英

Can C cope with sub-byte instruction addressing?

For example if an architecture supports nibble length instructions but data is byte aligned, will:

void *PointerToAnything;

work? In other words, can the compiler handle this?

In C, it is impossible to access data elements in units smaller than 8 bits, as the smallest possible type is char , which has CHAR_BIT bits, which is 8 at least. Bitfields are an exception, but don't allow pointers to their members; a data-pointer with sub-(8-)byte-precision can not exist in C.

However, instructions (and therefore functions) might be stored differently, and function-pointers could have sub-byte-precision. In general, function pointers and data pointers are not interchangeable, so you can not (correctly) store such a function pointer in a void* pointer. C does not support accessing the machine code anyways, so there would be no support for accessing instructions that have sub-byte-alignment and/or size.

Even on platforms with at least byte-instruction size & alignment, function pointers and data pointers might not be interchangeable, as function pointers might be larger or smaller than data pointers (imagine a system with 256 RAM bytes for data, and 64kB flash bytes for program memory). Therefore, C does not guarantee that void* can point to everything. However, some platforms such as POSIX do explicitly allow this, to allow eg dlsym() to work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM