Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it legal to modify any data pointer through a void **

Is it legal to access a pointer type through a void **?

I've looked over the standards quotes on pointer aliasing but I'm still unsure on whether this is legal C or not:

int *array;
void **vp = (void**)&array;
*vp = malloc(sizeof(int)*10);

Trivial example, but it applies to a more complex situation I'm seeing.

It seems that it wouldn't be legal since I'm accessing an int * through a variable whose type is not int * or char *. I can't come to a simple conclusion on this.

Related:

  • Does C have a generic "pointer to a pointer" type?
  • C-FAQ question 4.9
like image 813
Ryan Haining Avatar asked Aug 31 '13 19:08

Ryan Haining


2 Answers

No. void ** has a specific type (pointer to a pointer-to-void). I.e. the underlying type of the pointer is "pointer-to-void"

You're not storing a like-pointer value when storing a pointer-to-int. That a cast is required is a strong indicator what you're doing is not defined behavior by the standard (and it isn't). Interestingly enough, however, you can use a regular void* coming and going and it will exhibit defined behavior. In other words, this:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int *array;
    void *vp = &array;
    int **parray = vp;
    *parray = malloc(sizeof(int)*10);
}

is legitimate. Your original example won't even compile if I remove the cast and use apple llvm 4.2 (clang), due precisely to incompatible pointer types, i.e. the very subject of your question. The specific error is:

"Incompatible pointer types initializing 'void **' with an expression of type 'int **'"

and rightfully so.

like image 156
WhozCraig Avatar answered Nov 06 '22 18:11

WhozCraig


Pointer to different types can have different sizes.

You can store a pointer to any type into a void * and then you can recover it back but this means simply that a void * must be large enough to hold all other pointers.

Treating a variable that is holding an int * like it's indeed a void * is instead, in general, not permitted.

Note also that doing a cast (e.g. casting to int * the result of malloc) is something completely different from treating an area of memory containing an int * like it's containing a void *. In the first case the compiler is informed of the conversion if needed, in the second instead you're providing false information to the compiler.

On X86 however they're normally the same size and you're safe if you just play with pointers to data (pointers to functions could be different, though).

About aliasing any write operation done through a void * or a char * can mutate any object so the compiler must consider aliasing as possible. Here however in your example you're writing through a void ** (a different thing) and the compiler is free to ignore potentially aliasing effects to int *.

like image 31
6502 Avatar answered Nov 06 '22 16:11

6502