在什么时候由malloc分配的内存获得类型? [英] At what point does memory allocated by malloc get a type?
问题描述
此问题询问由 malloc $分配的对象的动态类型是什么c $ c>并根据顶部答案:
This question asks what is the dynamic type of the object allocated by malloc
and according to the top answer:
malloc的返回值
是未初始化存储的块。该存储空间中未构建任何对象。因此它没有动态类型。
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.
这带来了另一个问题:什么时候说返回的存储是有意义的 malloc
获取类型。例如:
This brings another question: at what point does it make sense to say that the storage returned by malloc
gets a type. For example:
void *p = malloc(sizeof(int));
int *pi = (int*)p;
我们可以说 pi
can we say that pi
above points to an object of dynamic type int
despite the fact that it is uninitialized?
推荐答案
p>根据标准的现状是没有对象。
The status quo according to the standard is that there's no object there.
通过
new-expression ([expr.new])或实现
([class.temporary])
An object is created by a definition ([basic.def]), by a new-expression ([expr.new]) or by the implementation ([class.temporary]) when needed.
保持
malloc
单独的状态是
不足以创建对象。
(int *)p
不是其中之一。
这篇关于在什么时候由malloc分配的内存获得类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!