What is the dynamic type of the object allocated by malloc?

后端 未结 5 2131
挽巷
挽巷 2021-01-12 09:31

The C++ standard refers to the term "dynamic type" (and the C standard refers to "effective type" in the similar context), for example

<
5条回答
  •  一个人的身影
    2021-01-12 10:13

    In C, the effective type is only relevant when you access an object. Then in is determined by

    • the declaration type, if it has one
    • the type of another object of which it is a copy (eg. memcpy)
    • the type of the lvalue through which it is accessed, e.g if a void* is converted to another pointer type (e.g int*), which then is dereferenced.

    The latter is usually what happens with malloced objects, if you assign the return value of malloc to a pointer type.

提交回复
热议问题