2013年4月17日

摘要: 这个应该是我理解的最透彻的一次啦#include <stdio.h>union _data{ struct _test { int i:1; int j:2; int k:13; }test; int n;}data;int main (){ data.n = 0x1234; printf("%x\n", data.test.i); //0 printf("%x\n", data.test.j); //fffffffe printf("%x\n", data.test.k); //246... 阅读全文
posted @ 2013-04-17 22:47 江在路上 阅读(152) 评论(0) 推荐(0) 编辑
 
摘要: 1、#include <stdio.h>#include <stdlib.h>#include <string.h>int main(int argc, char** argv){ union { struct { unsigned char a:1; unsigned char b:2; unsigned char c:3; }d; unsigned char e; } f; f.e = 1; printf("%d\n",f.d.a); ... 阅读全文
posted @ 2013-04-17 20:45 江在路上 阅读(146) 评论(0) 推荐(0) 编辑
 
摘要: 位域类型对齐如果相邻的位域字段的类型不同,在不同的位域类型间,按通用的对齐规则进行不同数据类型间的对齐(注意,struct的长度是其内部最宽类型的整数倍)。如果相邻的位域字段的类型不同,则各编译器的具体实现有差异,VC6采取不压缩方式(不同位域字段存放在不同的位域类型字节中),Dev-C++和GCC都采取压缩方式;1、struct bs{ unsigned a:4; char b:4 ; unsigned c:4;}data;//占12个字节2、#include<stdio.h>typedef unsigned int uint32_t; void inet_ntoa(u... 阅读全文
posted @ 2013-04-17 20:33 江在路上 阅读(139) 评论(0) 推荐(0) 编辑
 
摘要: #include<stdio.h>struct _node{ int a; int b;};void test(int a,int b){ int c=1; int d=1; struct _node node; node.a=1; node.b=2; printf("b: %p\n", &b); printf("a: %p\n", &a); printf("c: %p\n", &c); printf("d: %p\n", &d); printf("node: 阅读全文
posted @ 2013-04-17 16:26 江在路上 阅读(153) 评论(0) 推荐(0) 编辑
 
摘要: 一、定义 位域是指信息在存储时,并不需要占用一个完整的字节, 而只需占几个或一个二进制位。例如在存放一个开关量时,只有0和1 两种状态, 用一位二进位即可。为了节省存储空间,并使处理简便,C语言又提供了一种数据结构,称为“位域”或“位段”。所谓“位域”是把一个字节中的二进位划分为几 个不同的区域, 并说明每个区域的位数。每个域有一个域名,允许在程序中按域名进行操作。 这样就可以把几个不同的对象用一个字节的二进制位域来表示。二、例子1、struct bs{ int a:8; int b:2; int c:6;}data;//说明data为bs变量,共占2个字节。其中位域a占... 阅读全文
posted @ 2013-04-17 15:01 江在路上 阅读(213) 评论(0) 推荐(0) 编辑
 
摘要: 转载:http://blog.csdn.net/africahyena/article/details/1525393从union的sizeof问题看cpu的对界 考虑下面问题:(默认对齐方式)union u{ double a; int b;};union u2{ char a[13]; int b;};union u3{ char a[13]; char b;};cout<<sizeof(u)<<endl; // 8cout<<sizeof(u2)<<endl; // 16cout<<sizeof(u3)<<endl; 阅读全文
posted @ 2013-04-17 13:57 江在路上 阅读(176) 评论(0) 推荐(0) 编辑
 
摘要: 一、定义 现代计算机中内存空间都是按照byte划分的,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但实际上,为了减少CPU读取内存的此时,在访问特定类型变量的时候经常在特定的内存地址访问,这就需要各种类型数据按照一定的规则在空间上排列,而不是顺序的一个接一个的排放,这就是对齐。————————————————————————————————————————————————————————————二、原理解释 下面这段转载自:http://www.cnblogs.com/wuzhenbo/archive/2012/06/05/2537465.html 在IBM开发社区... 阅读全文
posted @ 2013-04-17 11:50 江在路上 阅读(179) 评论(0) 推荐(0) 编辑