对于分配对象严格别名规则 [英] Strict aliasing rules for allocated objects

查看:129
本文介绍了对于分配对象严格别名规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C99 6.5 / 6为它的存储的访问的有效类型的一个对象的
  值是对象的声明类型,如果有的话。 75)

C99 6.5/6 The effective type of an object for an access to its stored value is the declared type of the object, if any. 75)

如果一个值存储到一个目的通过一个不具有声明的类型
  左值具有类型不字符类型,则类型
  左值成为该访问的有效对象的类型
  并为后续访问不修改存储的值。

If a value is stored into an object having no declared type through an lvalue having a type that is not a character type, then the type of the lvalue becomes the effective type of the object for that access and for subsequent accesses that do not modify the stored value.

如果一个数值被复制到一个对象具有不使用声明的类型
  的memcpy或的memmove,或者被复制为字符类型的阵列,然后
  对于访问和用于有效类型的修改的对象的
  不修改的值随后的访问是有效的类型
  从该值被复制,如果有对象的。对全部
  其他访问不具有声明类型的对象,则有效
  对象的类型只是用于左值的类型
  访问。

If a value is copied into an object having no declared type using memcpy or memmove, or is copied as an array of character type, then the effective type of the modified object for that access and for subsequent accesses that do not modify the value is the effective type of the object from which the value is copied, if it has one. For all other accesses to an object having no declared type, the effective type of the object is simply the type of the lvalue used for the access.

75)分配的对象没有声明的类型。

75) Allocated objects have no declared type.

如C99所述,有效类型的静态对象是他们的声明类型。

As stated in C99, the effective type of static objects is their declared type.

分配对象获得其有效类型的怎么办?

How do allocated objects get their effective types?

例如:

int *p = malloc(100 * sizeof(int));

为什么不自己有声明的类型开始与?

Why don't they have a declared type to begin with?

推荐答案

这是分配对象还没有得到任何声明的类型,所以它的有效类型是用于接入左值的类型。有了这个单独的语句, P 还没有有效的类型:

An allocated object hasn't got any declared type, so its effective type is the type of the lvalue used for the access. With this single statement, p hasn't effective type:

#include <stdlib.h>
int *p = malloc(100 * sizeof(int));

,否则将有一个与下一个访问

Otherwise, it will have one with the next access:

/* Effective type of p: unsigned int */
*(unsigned int *)p = 20U;

这篇关于对于分配对象严格别名规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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