标签 Solaris 下的文章

走马观花ANSI C标准-类型表示

类型表示(representation of types)

1、一般规则
a) 除了位域(bit field)之外的对象都是由一个或多个相邻序列字节组成的,这些字节的个数(number)、次序(order)和编码方式或是显式说明的,或是实现定义的。

b) 存储在非符号位域(unsigned bit field)和unsigned char类型对象中的值应该用纯二进制表示(pure binary notation)。(这里就可以理解为符号位当作普通二进制位看)

c) 存储在非位域的其他类型对象的值由n字节组成,这个值可以被拷贝到一个unsigned char[n]类型的对象中去。[注1]

2、整型
a) 对于无符号整型(而不是unsigned char),用于表示对象的位应分为两组:值位(value bits)和补充位(padding bits)。补充位的值是不确定的。

b) 对于有符号整型,用于表示对象的位应分为三组:值位(value bits)和补充位(padding bits)和符号位(sign bit)。这种类型应恰好有一位符号位,补充位不必要。

c) 整型的精度是指用来表示对象值所用的bit位数,不包括符号位和补充位。整型的宽度也是指用来表示对象值所用的bit位数,但它包含符号位。对于unsign整型来说其精度和宽度是相同的。

3、兼容类型(compatible type)和复合类型(composite type)
在平时的C语言使用中,我们几乎没提到过这两个概念,这里对这两个概念作简单解释:
兼容类型用来在不同翻译单元间检查类型兼容性;
复合类型产生的原因是由于这样的情况“在同一namespace和同一scope中,同一个标识符的声明不止一个”。

E.G.
void f();
void f(int p1[], const int p2, float * p3);
void f(int p1[2], int p2, float * p3);
Composite type is: void f(int p1[2], int p2, float *p3);

如果两个类型相同,则两个类型兼容。在这一规则背后隐藏着的含义是“兼容类型总是有着相同的表示(representation)和对齐(alignment)需求”。
关于兼容类型,标准中说了不少,不过觉得在使用时对之少有问津,所以到这就“浅尝辄止”了:)。

[注1]
也是由于这点,下面这个函数工作良好。
void dump_mem(const void *p, size_t size) {
        unsigned char *c = (unsigned char*)p;
        size_t i;

        YOUR_ASSERT(p != NULL);

        for (i = 0; i < size; i++) {
                printf("%02X ", c[i]);
        }

        printf("\t|");

        for (i = 0; i < size; i++) {
               if(isprint(c[i])) {
                    printf("%c ", c[i]);
               }
        }
        printf("\n");
}

用法:
int i = 0×45674142;
dump_mem(&i, sizeof(i));

输出:
42 41 67 45     |B A g E /* 在WinXP , MingW Gcc3.4.2 */
45 67 41 42     |E g A B /* 在Solaris, Gcc3.2 */

本不是第一篇的第一篇

刚入司时,给我们做技术培训的老员工强烈向我们推荐blog这个新兴(起码对我来说是新的^_^)的咚咚,当时也想写就申请了一个,可是不知一天都忙些什么了,到今天才写了这第一篇,要说今天为什么要写,两个字“心烦”,本来我的第一篇blog在我的头脑中早已构思多次了,内容也换了多次,可是都没能实现,这篇blog是在丝毫没想的前提下动手写的。我觉得这样也不错,现在我的感觉就是想写就写,不用考虑太多,呵呵。

今天我入司以来的第一个项目编码结束,上午代码评审,我的leader给我找出“一堆”问题。真是郁闷啊,原以为自己的水平可以了,原来还是有很多要学习的。以前在Windows平台上开发,早已有自己的规范或者说习惯。现在转移到Solaris上开发,有些东东还真得好好学习一下,比如C语言代码头文件和源文件的格式。下面的例子是从各个代码规范中提取出来的一部分,也是我以后在项目中要使用的形式,有可能只适合我,呵呵。

本不是第一篇的第一篇,我就写这么多了。

附代码规范示例:

/* in ANSI_C_CODING_STANDARDS.h */

/*
 * Copyright 2005, XX, Inc., China
 * All rights reserved. 
 *
 * XX's source code is an unpublished work and the use of a copyright notice does 
 * not imply otherwise. This source code contains confidential, trade secret material of 
 * XX, Inc. Any attempt or participation in deciphering, decoding, reverse engineering
 * or in any way altering the source code is strictly prohibited, unless the prior written 
 * consent of XX, Inc. is obtained.
 */
 
/*
 * @file ANSI_C_CODING_STANDARDS.h
 * @author tony_bai
 * @date 2005-07-01
 * @brief the template for ANSI C
 *        header file
 */
 
/* 
 * @revision
 *   @version 0.1
 *   @date 2005-08-01
 *   @Revisor tony_bai
 *   
 * @revision
 *   @version 0.2
 *   @date 2005-09-01
 *   @Revisor tony_bai
 */
 
/*
 * @glossary
 *   xx – xxxxx
 *   xx – xxxxx
 */
 
/*
 * @usage
 *
 */
 
#ifndef ANSI_C_CODING_STANDARDS_H
#define ANSI_C_CODING_STANDARDS_H
 
#include ANSI C Standard Library Header File
#include Operating System Library Header File
#include "Your System Library Header File"
 
/*
 * ####################
 * # global constants #
 * ####################
 */
 
/*
 * #################
 * # global macros #
 * #################
 */
 
/*
 * ##############################
 * # global abstract data types #
 * ##############################
 */
 
/*
 * ####################
 * # global variables #
 * ####################
 */
 
/*
 * #############
 * # externals #
 * #############
 */
 
/*
 * ###############################
 * # global functions prototypes #
 * ###############################
 */
 
#endif ANSI_C_CODING_STANDARDS_H 
 
/* in ANSI_C_CODING_STANDARDS.c */
/*
 * Copyright 2005, XX, Inc., China
 * All rights reserved. 
 *
 * XX's source code is an unpublished work and the use of a copyright notice does 
 * not imply otherwise. This source code contains confidential, trade secret material of 
 * XX, Inc. Any attempt or participation in deciphering, decoding, reverse engineering
 * or in any way altering the source code is strictly prohibited, unless the prior written 
 * consent of XX, Inc. is obtained.
 */
 
/*
 * @file ANSI_C_CODING_STANDARDS.c
 * @author tony_bai
 * @date 2005-07-01
 * @brief the template for ANSI C
 *        source file
 */
 
/* 
 * @revision
 *   @version 0.1
 *   @date 2005-08-01
 *   @Revisor tony_bai
 *   
 * @revision
 *   @version 0.2
 *   @date 2005-09-01
 *   @Revisor tony_bai
 */
 
#include ANSI C Standard Library Header File
#include Operating System Library Header File
#include "Your System Library Header File"
 
/*
 * ###################
 * # local constants #
 * ###################
 */
 
/*
 * ################
 * # local macros #
 * ################
 */
 
/*
 * #############################
 * # local abstract data types #
 * #############################
 */
 
/*
 * ###################
 * # local variables #
 * ###################
 */
 
/*
 * ##############################
 * # local functions prototypes #
 * ##############################
 */
 
/*
 * ####################################
 * # global functions implementations #
 * ####################################
 */
 
/*
 * ###################################
 * # local functions implementations #
 * ###################################
 */
如发现本站页面被黑,比如:挂载广告、挖矿等恶意代码,请朋友们及时联系我。十分感谢! Go语言第一课 Go语言精进之路1 Go语言精进之路2 商务合作请联系bigwhite.cn AT aliyun.com

欢迎使用邮件订阅我的博客

输入邮箱订阅本站,只要有新文章发布,就会第一时间发送邮件通知你哦!

这里是 Tony Bai的个人Blog,欢迎访问、订阅和留言! 订阅Feed请点击上面图片

如果您觉得这里的文章对您有帮助,请扫描上方二维码进行捐赠 ,加油后的Tony Bai将会为您呈现更多精彩的文章,谢谢!

如果您希望通过微信捐赠,请用微信客户端扫描下方赞赏码:

如果您希望通过比特币或以太币捐赠,可以扫描下方二维码:

比特币:

以太币:

如果您喜欢通过微信浏览本站内容,可以扫描下方二维码,订阅本站官方微信订阅号“iamtonybai”;点击二维码,可直达本人官方微博主页^_^:
本站Powered by Digital Ocean VPS。
选择Digital Ocean VPS主机,即可获得10美元现金充值,可 免费使用两个月哟! 著名主机提供商Linode 10$优惠码:linode10,在 这里注册即可免费获 得。阿里云推荐码: 1WFZ0V立享9折!


View Tony Bai's profile on LinkedIn
DigitalOcean Referral Badge

文章

评论

  • 正在加载...

分类

标签

归档



View My Stats