Does accessing raw memory through casting to int violate a strict alias?

Suppose I want to dynamically allocate space for intand write the maximum displayed value to this memory. This code comes to mind:

auto rawMem = std::malloc(sizeof(int));         // rawMem type is void*
*(reinterpret_cast<int*>(rawMem)) = INT_MAX;    // INT_MAX from <limits.h>

Does this C ++ rule code mean a strict alias ? Neither g ++ nor clang ++ complain about -Wall -pedantic.

If the code does not violate a strict alias, why not? std::mallocreturns void*, so while I do not know what static and dynamic types of memory are returned std::malloc, there is no reason to think what it is int. And we do not access memory like charor unsigned char.

I would like to think that the code is kosher, but if so, I would like to know why.

While I'm in the neighborhood, I would also like to know the static and dynamic types of memory returned by the memory allocation functions ( std::mallocand std::operator new).

+4
source share
1 answer

A strict smoothing rule allows the compiler to assume that the same memory location cannot be obtained through two or more pointers of different types.

Consider the following code:

int* pi = ...;
double* pd = ...;

const int i1 = *pi;    // (1)
*pd = 123.456;         // (2)
const int i2 = *pi;    // (3)

, i2 == i1, , pi, (1) (3). i1 i2 ( , ). , .

malloc(). (.. , ... ummm... , - char[] , char s). , , . , . POD a reinterpret_cast ( ) , new. .

+4

All Articles