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

推荐订阅源

Google DeepMind News
Google DeepMind News
Stack Overflow Blog
Stack Overflow Blog
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
T
The Blog of Author Tim Ferriss
博客园 - 叶小钗
N
Netflix TechBlog - Medium
腾讯CDC
C
Check Point Blog
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
S
SegmentFault 最新的问题
F
Fortinet All Blogs
美团技术团队
U
Unit 42
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 司徒正美
F
Full Disclosure
Recorded Future
Recorded Future
D
DataBreaches.Net
博客园 - 【当耐特】
Martin Fowler
Martin Fowler
J
Java Code Geeks
I
InfoQ
Y
Y Combinator Blog
A
About on SuperTechFans
AI
AI
爱范儿
爱范儿
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Forbes - Security
Forbes - Security
W
WeLiveSecurity
M
MIT News - Artificial intelligence
雷峰网
雷峰网
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
Schneier on Security
Schneier on Security
The GitHub Blog
The GitHub Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
Latest news
Latest news
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
D
Docker
Recent Commits to openclaw:main
Recent Commits to openclaw:main
量子位
V2EX - 技术
V2EX - 技术
Project Zero
Project Zero

博客园 - xlander

跟实际生活相关的饭否应用 5个跟实际生活相关的Twitter应用 Google Code上两个LPC21XX开源项目 Router牌音箱 基于ARM的网络收音机 博客程序换成了Z-Blog的 音箱?储钱罐? 使用结构体中成员变量指针的教训 结构体中位段、字节对齐并用的教训 C语言的跨编译器编译 日本某商场里的智能瀑布 ADS下C语言中局部变量的存储位置分配 救活TrackBack的偏方 近期几个开源的嵌入式产品 几个自制心电图的网络资源 哇哦级的贴片焊接教学视频 自动五线谱识别及音乐合成装置 简易指纹识别授权访问系统 PIC18F2550实现的基于HID的USB示波器
显式指针转换的教训
xlander · 2009-02-11 · via 博客园 - xlander

下面的代码会有错么?

unsigned short int a;
unsigned short int *pc = 0;
unsigned char ac[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
int main(void)
{
while(1)
{
pc = (unsigned short int *)ac;
a = *pc;
pc = (unsigned short int *)(ac+1);
a = *pc;
}
}

没错。我一直这么用的,用VC开发了这么多年,这么简单的事儿我还是知道的。

这种想当然,很遗憾的,我不得不承认,我错了。

确切地说,这种代码在x86平台下,没有任何问题,但是,当我在ARM下也这么做的时候就有问题了。


这是上面代码的执行结果,尽管可以通过调试器看到*pc=0x0302,但是,变量a却赫然的显示为0x0102。

搜索一下网络上的资料,询问类似问题的人不在少数,大都是ARM之类的RISC指令集,而在x86平台下,很少有人意识到这种问题,至少没有提及到这种问题。程序编译中的字节对齐这篇文章对这个问题描述得非常详细。

Copyright © 2008

继续阅读《显式指针转换的教训》的全文内容...

分类: 软件开发 | Tags: C  跨平台  ARM  指针   | 添加评论(1)

相关文章: