malloc分配的对象的动态类型是什么? [英] What is the dynamic type of the object allocated by malloc?
问题描述
C ++标准是指动态类型(而C标准在类似上下文中指有效类型),例如
The C++ standard refers to the term "dynamic type" (and the C standard refers to "effective type" in the similar context), for example
如果程序试图通过除以下类型之外的 glvalue 访问对象的存储值,则该行为未定义:
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:
- 对象的动态类型,
但是如何确定用 malloc
分配的对象的动态类型?
But how is the dynamic type of the object allocated with malloc
determined?
例如:
void *p = malloc(sizeof(int));
int *pi = (int*)p;
pi $ cc指向的对象的动态类型根据C ++规范:$ c> be
int
?
推荐答案
动态类型:
Dynamic type:
< glvalue> (1.8),其中由glvalue表达式表示的glvalue指向
<glvalue> type of the most derived object (1.8) to which the glvalue denoted by a glvalue expression refers
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.
void *
不指向一个对象,动态类型。
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.
这篇关于malloc分配的对象的动态类型是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!