是TYPE *为unsigned char型铸造*允许吗? [英] Is casting from TYPE* to unsigned char* allowed?

查看:146
本文介绍了是TYPE *为unsigned char型铸造*允许吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C99 - 特别6.2.6.1节,第4段 - 指出,复制对象重新presentation成unsigned char型数组是允许的:

 结构{
    INT富;
    双条;
}巴兹;
unsigned char型字节[sizeof的巴兹]//做事与巴兹结构。的memcpy(字节,&安培;巴兹,sizeof的字节);//做事情的字节数组。

我的问题:我们能不能​​避免额外的内存分配,并通过简单的复制铸造操作?例如:

 结构{
    INT富;
    双条;
}巴兹;
无符号字符*字节=(无效*)及巴兹;//做的东西与巴兹结构。//做事情的字节数组。

当然,大小将需要进行跟踪,但这一法律在第一位呢,还是落入实现定义或未定义行为的境界?

我问,因为我实现一个类似于的qsort 的算法,我想它对于任何数组无论类型的工作,就像的qsort 一样。


解决方案

  

6.5防爆pressions


  
  

[...]结果
  6为它的存储值的访问的有效类型的一个目的是的声明类型
  对象,如果any.87)如果一个值存储到一个对象不具有声明的类型通过
  左值具有类型不字符类型,则左值的类型成为
  对于访问和不修改后续访问有效类型的对象的
  所存储的值。如果值被复制到一个对象具有不使用声明的类型
  的memcpy或的memmove,或者被复制为字符类型的阵列,则有效类型
  对于访问和对不修改的后续访问修改的对象的
  值是有效类型从该值被复制,如果有对象的。对于
  所有其他的访问不具有声明类型的对象中,有效的对象的类型是
  简单类型用于访问左值的。结果
  7 对象应具有其存储的值仅由具有之一的左值前pression访问
  以下几种类型:88)


  
  

      
  • 一个类型的有效对象的类型,
  • 兼容
      
  • 一个合格的版型与有效对象的类型兼容,

  •   
  • 一类型,它是符号或对应于有效类型的无符号类型
      对象,

  •   
  • 一类型是签名或相应的合格的版本无符号类型
      有效类型的对象,

  •   
  • ,包括在前述类型中的一种聚合或联合类型的
      成员(包括递归,一个子聚集的成员或包含的联合),或

  •   
  • 字符类型

  •   

重点煤矿。因此,你都OK治疗任何类型的字符(无符号的char [] 的char [] 或数组签署的char [] )。

我也引述第6款,因为它使反向不适用。

C99 -- specifically section 6.2.6.1, paragraph 4 -- states that copying an object representation into an array of unsigned char is allowed:

struct {
    int foo;
    double bar;
} baz;
unsigned char bytes[sizeof baz];

// Do things with the baz structure.

memcpy(bytes, &baz, sizeof bytes);

// Do things with the bytes array.

My question: can we not avoid the extra memory allocation and copy operation by simply casting? For example:

struct {
    int foo;
    double bar;
} baz;
unsigned char *bytes = (void *)&baz;

// Do stuff with the baz structure.

// Do things with the bytes array.

Of course, the size would need to be tracked, but is this legal in the first place, or does it fall into the realm of implementation-defined or undefined behavior?

I ask because I'm implementing an algorithm similar to qsort, and I'd like it to work for any array regardless of type, just as qsort does.

解决方案

6.5 Expressions

[...]
6 The effective type of an object for an access to its stored value is the declared type of the object, if any.87) 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. 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.
7 An object shall have its stored value accessed only by an lvalue expression that has one of the following types:88)

  • a type compatible with the effective type of the object,
  • a qualified version of a type compatible with the effective type of the object,
  • a type that is the signed or unsigned type corresponding to the effective type of the object,
  • a type that is the signed or unsigned type corresponding to a qualified version of the effective type of the object,
  • an aggregate or union type that includes one of the aforementioned types among its members (including, recursively, a member of a subaggregate or contained union), or
  • a character type.

Emphasis mine. Thus, you are ok treating any type as an array of characters (unsigned char[], char[] or signed char[]).

I also quoted paragraph 6 because it makes the reverse not apply.

这篇关于是TYPE *为unsigned char型铸造*允许吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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