在什么时候由malloc分配的内存获得类型? [英] At what point does memory allocated by malloc get a type?

查看:97
本文介绍了在什么时候由malloc分配的内存获得类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题询问由 malloc 并根据顶部答案:

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.

[intro.object] / 1


通过
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.

另请参阅 P0137 < a>,这将使上述引用对象的定义:


保持 malloc 单独的状态是
不足以创建对象。

(int *)p 不是其中之一。

这篇关于在什么时候由malloc分配的内存获得类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆