The C++ standard refers to the term "dynamic type" (and the C standard refers to "effective type" in the similar context), for example
If a program attempts to access the stored value of an object through a glvalue of other than one of the following types the behavior is undefined:
- the dynamic type of the object,
But how is the dynamic type of the object allocated with malloc
determined?
For example:
void *p = malloc(sizeof(int));
int *pi = (int*)p;
Will the dynamic type of the object pointed to by pi
be int
?
According to the C++ specification:
Dynamic type:
<glvalue> type of the most derived object (1.8) to which the glvalue denoted by a glvalue expression refers
The return value of malloc
is a block of uninitialized storage. No object has been constructed within that storage. And therefore it has no dynamic type.
The void*
does not point to an object, and only objects have a dynamic type.
You can create an object within that storage by beginning its lifetime. But until you do so, it's just storage.
In C, the effective type is only relevant when you access an object. Then in is determined by
memcpy
)void*
is converted to another pointer type (e.g int*
), which then is dereferenced.The latter is usually what happens with malloc
ed objects, if you assign the return value of malloc
to a pointer type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With