How dangerous is the conversion from void ** to char **

So, we know that the standard does not force pointer sizes to be equal. ( here and here ) (and not to mention function pointers)

I was wondering how this could actually be a problem. We know that void * can contain anything, so if the pointer is different in size, it will be the largest size. Given that assigning void ** a char ** means a problem.

My question is: how dangerous is it to assume that void * and char * are the same size? Is there architecture where this is not so?

Also, 16-bit dos is not what I want to hear !;)

+4
source share
2 answers

void * and char * guaranteed to be the same size.

void ** not guaranteed to be the same size as char ** (but very similar to your implementation).

(C99, 6.2.5p27) "A pointer to void must have the same representation and alignment requirements as a pointer to a character type [...] Pointers to other types should not have the same representation or alignment requirements."

+9
source

Assigning pointers of different types of objects to each other is allowed until alignment requirements are violated: the assignment will include a (implicit) type conversion, so it is (un) problematic as assigning a float int - it works in most cases, but it is allowed to explode when meaningful transformation is impossible.

char * and void * have compatible alignment requirements for each specification, so assigning a variable to char ** a void ** (and vice versa) is never problematic. They even have a compatible view, which basically means that access to char * through an expression of type void * - for example, by dereferencing a void ** , which actually points to a char * , will work as expected in most cases. Of course, the converse is also true (access to a void * by dereferencing a char ** ).

For example, the conversion specifier p for printf() expects void * , and the transfer in an arbitrary pointer type is undefined. However, in the case of char * it should work even on exotic architectures (for example, with different representations of pointers) if the implementation complies with the C standard.

If problems arise, this is anti-aliasing analysis: due to efficient typing rules, a void ** and a char ** cannot be an alias, and if a programmer breaks this promise, strange things can happen. It should be understood that due to efficient typing (aka strict anti-aliasing), C is actually strongly typed - the type system is just very unreasonable (i.e. does not protect you from breaking its invariants) ...

+3
source

All Articles