惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Stack Overflow Blog
Stack Overflow Blog
T
Tor Project blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Palo Alto Networks Blog
T
The Exploit Database - CXSecurity.com
P
Privacy International News Feed
C
Cybersecurity and Infrastructure Security Agency CISA
MyScale Blog
MyScale Blog
D
DataBreaches.Net
I
Intezer
GbyAI
GbyAI
Jina AI
Jina AI
The GitHub Blog
The GitHub Blog
S
Security @ Cisco Blogs
C
Cyber Attacks, Cyber Crime and Cyber Security
NISL@THU
NISL@THU
Project Zero
Project Zero
博客园_首页
Martin Fowler
Martin Fowler
A
About on SuperTechFans
J
Java Code Geeks
AI
AI
WordPress大学
WordPress大学
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
LINUX DO - 热门话题
云风的 BLOG
云风的 BLOG
腾讯CDC
酷 壳 – CoolShell
酷 壳 – CoolShell
C
Cisco Blogs
L
LangChain Blog
Google Online Security Blog
Google Online Security Blog
AWS News Blog
AWS News Blog
Help Net Security
Help Net Security
Application and Cybersecurity Blog
Application and Cybersecurity Blog
D
Docker
N
Netflix TechBlog - Medium
Know Your Adversary
Know Your Adversary
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
Secure Thoughts
H
Heimdal Security Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
O
OpenAI News
S
Security Affairs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 【当耐特】
雷峰网
雷峰网
V
Visual Studio Blog
T
Threat Research - Cisco Blogs

博客园 - 刘伟_luvi

C项目错误情况小记 s3c2440设置从nfs或从board启动 Hash Functions for Hash Table Lookup 自写linux下的简单man帮助文件 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘<’ token Linux MAN命令 C数组测试 - 刘伟_luvi - 博客园 在本地计算机无法启动MySQL服务。错误1067:进程意外终止 嵌入式中地址-函数之间的转换 悄悄地,你走了 AMBA片上总线 Linux系统环境下的Socket编程详细解析 C语言宏定义技巧(常用宏定义)【转】 s3c2410 MMU详述 可重入函数与不可重入函数(转) 程序的书写规则(程序的编码规范) ARM中C和汇编混合编程及示例 嵌入式系统 Boot Loader 技术内幕(转) 关于Linux系统下网卡手写配置文件的介绍
对C语言中结构体的测试分析
刘伟_luvi · 2008-05-21 · via 博客园 - 刘伟_luvi

今天看到一本书中有与下面这种结构体类似的使用形式:
typedef struct some_dev{
        unsigned int var1 : 8;
        unsigned int var2 : 13;
}
特意在VC++ 6.0中做了几个测试:
测试1:
#include <stdio.h>
typedef struct test1{
    unsigned int testint:8;
    char *p :32;
    int test2 : 16;
} test;

int main()
{
    test obj;
    obj.testint = 32;
    obj.test2 = 256;
    char *t="hello world!";
    obj.p=t;
    printf("\n\t%u.......%d.........%s", obj.testint, obj.test2 , obj.p);
    return 0;
}
编译结果:
struct_t.obj - 2 error(s), 0 warning(s)
error C2033: 'p' : bit field cannot have indirection
error C2034: 'p' : type of bit field too small for number of bits
即便指针p的长度为32或者64,编译都不能通过。
所以我们可以这样认为:
      1:指针类型变量不能指定所占的位数
测试2:
#include <stdio.h>
typedef struct test1{
    unsigned int testint:8;
    char *p;
    int test2 : 16;
} test;

int main()
{
    test obj;
    obj.testint = 32;
    obj.test2 = 256;
    char *t="hello world!";
    obj.p=t;
    printf("\n\t%u.......%d.........%s\n", obj.testint, obj.test2 , obj.p);
    return 0;
}
编译链接均没有错误警告信息,运行结果为:
 32.......256.........hello world!Press any key to continue
测试3:
#include <stdio.h>
typedef struct test1{
    unsigned int testint:8;
    char *p;
    int test2 : 16;
} test;

int main()
{
    test obj;
    obj.testint = 256;          //这里修改obj.testint的值为256
    obj.test2 = 256;
    char *t="hello world!";
    obj.p=t;
    printf("\n\t%u.......%d.........%s\n", obj.testint, obj.test2 , obj.p);
    return 0;
}
运行结果为:
 0.......256.........hello world!

可以看到,成员变量obj.testint的运行结果已经变成“0”.
为什么呢??
原来在结构体声明的时候,我们给obj.testint的bit数声明为8b的unsigned类型,它可以表示的最大值为(2^8 - 1),即255,我们在第三个测试中,给它赋值256,它的二进制值为:1 0000 0000 b,系统在保留时,只保留了后8位的值,对溢出的位,进行了截取抛弃处理,所以这里输出的结果为0.
而输出sizeof(struct test)的结果为:12
也就是说这三个成员变量各占4个字节。
再测试几组数据,结果也都能证明下面的结论:
      2. 在声明成员变量时,可以用
         变量名 :bit数;
来确定结构体类型的成员变量的值所占的字位数,如果在实际应用中,该变量的值超出了在声明它时所声明的字位数,那么溢出的部分将会丢失。