Question

Let's say that I have a buffer of chars and I want to avoid using memcpy, and access to it through an int* variable:

char buffer[100];
strcpy(buffer,"Hello");
int* __restrict ptr=(int*)buffer;
*ptr= 97;
printf("%s",buffer);

Now this of course prints "a".
Am I allowed to do this without encountering an undefined behaviour?

Was it helpful?

Solution

Now this of course prints "a".

Well, only on little endian machines.

And strict aliasing would have nothing do with your example as one of the type is char and char may alias anything if the goal of restrict wasn't to increase the number of cases where the compiler may assume that there is no alias, i.e. even when typing information wouldn't prevent it.

And obviously if you want information about __restrict which is in the implementation domain, you should specify the implementation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top