Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any pitfalls using char* instead of void* when writing cross platform code?

Is there any pitfalls when using char*'s to write cross platform code that does memory access?

UPDATE: For example, should I check before casting a dereferenced char* to a certain type (say an int) if address is aligned to the size of that type? Will certain architectures return strange results on unaligned access?

I'm working on a play memory allocator to better understand how to debug memmory issues. I have come to believe char*'s are preferable because of the ability to do pointer arithmetic and dereference them over void*'s, is that true? Do the following assumptions always hold true on different common platforms?

sizeof(char) == 1
sizeof(char*) == sizeof(void*)
sizeof(char*) == sizeof(size_t)
like image 804
UberMongoose Avatar asked Oct 24 '12 04:10

UberMongoose


1 Answers

sizeof(char)==1 is definitely always true.

sizeof(char *) == sizeof(void *) is probably always true as well. The C standard requires that they have the same representation, which at least strongly implies the same size.

sizeof(char *) == sizeof(size_t) definitely cannot be depended upon -- I know of implementations for which it is false (and while they probably don't conform perfectly with the standard, this isn't one of their problems).

like image 186
Jerry Coffin Avatar answered Nov 11 '22 03:11

Jerry Coffin