一, 为什么要跨平台?
你想过把你的 Windows 上编写的程序在 Linux 编译运行吗,以及在 Mac 或其他 OS 上运行等等?反过来也一样?这就需要涉及到跨平台编程知识。这里需要注意的是,平时很多在一个平台运行的程序在跨平台的时候变的不再正确。
Java 并非真的是跨平台的开发环境,它是运行在它自己的平台上。这里主要关注 C 和 C++ 的跨平台开发。
下面主要就几个方面来讨论跨平台编程的注意事项:
1. 字节序
2. 字节填充
3. 其他
二, 字节序
大家都知道计算机使用两种字节序,一种是 little-endian ,另一种是 big-endian 。这主要是由于当前流行的 CPU 之间的差异造成的,基本上是 IBM-PowerPC 使用的大序,而其他 CPU 使用的小序。
这里先来介绍一下 little-endian 和 big-endian 之间的具体差异。
X86 指令集合使用小序( little-endian )字节顺序;这就意味着多个字节值的最重要字节在地址的最低位。小序很早就使用,因为硬件容易实现,但和今天的制造商技术有点不同;但在第一代 IBM PC 机的 Vaxen 和 8086 处理器使用是它如此流行的主要原因。
看一个例子:
| short example[2] = {0x0001,0x3002}; |
按照 16 进制的形式来显示上面数据在内存中的存储方式:
我们看到对于数组的第一个元素,高 8 位应该是 0 ,而最终存储的时候是在低 8 位的后面。
而另一方面 PowerPC 和 Sparc 芯片是 big-endian 的,也就是说,最重要的字节存储在较低的地址。对于 CPU 需要额外的电路实现这个功能,但对于今天的处理器技术与缓存控制技术相比较显的微不足道。使用 BIG-ENDIAN 的最大好处是在使用低级调式器时比较容易理解数据的存储,同样对于文件十六进制 DUMP 或网络 Sniffer 显示也是一样的。
对于 BIG-ENDIAN ,上面的例子中内存如下表示:
这里需要注意的是:由于 BIG-ENDIAN 格式的 RAW 数据比较容易调式,如果我们有机会设计一个新的文件格式,那么使用 BIG-ENDIAN 格式,而不是根据 CPU 架构来决定。
下面看几个关于字节序的问题:
1. Long 型指针和 char 指针之间的转换
看下面这段代码
| unsigned long value = 0x03020100; unsigned long *ptr = &value; unsigned char charVal; charVal = *(unsigned char *)ptr; |
程序的含义比较简单,主要是从一个指向 long 的指针强制转换为一个指向 char 的指针,这里假设指针指向的是最不重要的字节地址。
在一个 little-endian 处理器上, charVal 是 0 ,而在一个 big-endian 处理器上, charVal 的值是 3 。这样的问题是最难以发现的问题之一。
为了避免这个错误,使用一个临时变量可以解决这个问题,如下:
| unsigned long temp = *ptr; charVal = (unsigned char)temp; |
上面的第二行代码就保证将在任何架构上都将最不重要的字节传递给 charVal ;编译器处理具体的细节。
2. 读写文件和写网络数据
在从文件读数据或写数据到文件的时候以及网络,对于字节顺序的处理一定要小心;一定记住不能将多个字节的数据写到文件或网络上;例如:
| long val = 1; int result = write(fileDes,&val,sizeof(val)); |
这段代码在 little-endian 和 big-endian 机器上执行的结果是不一样的,如果读数据的时候使用如下代码:
| long val ; int result = read(fileDes,&val,sizeof(long)); |
如果这两段代码分别位于 little-endian 和 big-endian 机器上,那么最终得到的 val 不是 1 ,而是 0x01000000 。
解决多字节的读写有很多办法,这里提供两种。
方法 1 :
写的代码
| long val = 1; char buf[4]; buf[0] = 0xff&val; buf[1] = (0xff00&val)>>8; buf[2] = (0xff0000&val)>>16; buf[3] = (0xff000000&val)>>24; int result = write(fileDes,buf,4); |
读的代码
| long val; char buf[4]; int result = read(fileDes,buf,4); val = buf[0]|(buf[1]<<8)|(buf[2]<<16)|(buf[3]<<24); |
3. 运行时检查字节顺序
| bool gIsBigEndian; void InitializeEndianFlag() { Short one = 1; Char *cp = (char *)&one; If(*cp == 0) gIsBigEndian = true; else gIsBigEndian = false; return ; } |
4. 字节交换对性能的影响
由于字节顺序的问题导致在处理的时候需要进行字节交换或类似 2 中方法 1 的处理,这里称为交换。通常情况下,做字节顺序的交换并不影响,因为交换两个字节或四个字节值只需要很少的 CPU 指令,并且完全可以在寄存器中执行。
但如果有很多数据需要交换,例如:一个 1024*768 位图的图像,在这么大的循环中执行是影响性能的。
另外对于 3 的运行时检查字节序的代码要查看具体的位置。如果仅仅调用一次或几次,不会影响性能,如果对于上面的这个循环中调用,对性能的影响是显著的,这个时候可以使用一个预编译宏来分别处理。例如:
| #ifdef BIG_ENDIAN//big-endian … #else//little-endian …
此博客中的热门博文
/* * * uncdownload.c * * * * Utility for downloading files from SMB shares using libsmbclient * * * * Copyright(C) 2006 Sophos Plc, Oxford, England. * * * * This program is free software; you can redistribute it and/or modify it under the terms of the * * GNU General Public License Version 2 as published by the Free Software Foundation. * * * * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without * * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * * See the GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License along with this program; if not, * * write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * * */ # include < sys / types . h > # include < sys / time . h > # include ...
Ether Types (last updated 2008-09-09) NOTE: Please see [RFC5342] for current information and registration procedures. This registry will be revised soon and will be replaced with up-to-date information. Many of the networks of all classes are Ethernets (10Mb) or Experimental Ethernets (3Mb). These systems use a message "type" field in much the same way the ARPANET uses the "link" field. If you need an Ether Type, contact: IEEE Registration Authority IEEE Standards Department 445 Hoes Lane Piscataway, NJ 08854 Phone +1 732 562 3813 Fax: +1 732 562 1571 Email: <ieee-registration-authority& ieee.org > http://standards.ieee.org/regauth/index.html The following list of EtherTypes is contributed unverified information from various sources. Another list of EtherTypes is maintained by Michael A. Patton and is accessible at: <URL: http://www.cavebear.com/CaveBear/Ethernet/ > <URL: ftp://ftp.cavebear.com/pub/Ethernet-codes > Assign...
UDP通讯实例 2008-04-29 15:30:05 / 个人分类: linux C编程 UDP协议的几点好处: 1.UDP不要求保持一个连接; 2.UDP没有因接收方认可收到数据包(或者当数据包没有正确抵达而自动重传)而带来的开销; 3.设计UDP的目的是用于短应用和控制信息; 4.在一个数据包接一个数据包的基础上,UDP要求的 网络 带宽比TCP更小。 UDP的几个缺点: 1.程序员必须创建代码检测传输错误并进行重传(如果应用程序要求这样做); 2.程序员必须把大数据包分片。 code: <1> /* * sender.c--UDP protocol example */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> int port = 6789; int main() { int socket_descrīptor; int iter = 0; char buf[80]; struct sockaddr_in address; /* Initialize socket address structure for Interner Protocols */ bzero(&address, sizeof(address)); // empty data structure address.sin_family = AF_INET; address.sin_addr.s_addr = inet_addr("127.0.0.1"); address...
|
评论
发表评论