Reputation: 319
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)
Upvotes: 7
Views: 371
Reputation: 490178
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).
Upvotes: 5