<?xml version="1.0" encoding="gb2312"?>

<!-- RSS generated by oioj.net on 4/16/2004 ; 感谢LeXRus提供 RSS 2.0 文档; 此文件可自由使用，但请保留此行信息 --> 
<!-- Source download URL: http://blogger.org.cn/blog/rss2.asp       -->
<rss version="2.0">

<channel>
<title>ela'tech_blog</title>
<link>http://blogger.org.cn/blog/blog.asp?name=ellx</link>
<description>ellx的博客</description>
<copyright>blogger.org.cn</copyright>
<generator>W3CHINA Blog</generator>
<webMaster>webmaster@blogger.org.cn</webMaster>
<item>
<title><![CDATA[Winpcap不同版本之间的一个小差异导致的错误。]]></title>
<link>http://blogger.org.cn/blog/more.asp?name=ellx&amp;id=13133</link>
<author>ellx</author>
<pubDate>2006/4/3 10:42:01</pubDate>
<description><![CDATA[<a>　原来机器中的Winpcap的版本是3.0，程序总是运行不下去，分析之后得知原来是自己的Winpcap版本出了错，其中的一个函数pcap_findalldevs_ex（），在3.0的版本和3.2的版本中的参数的个数不一样，导致程序出错。事例代码如下：<br>
<span style="color: rgb(102, 51, 255);">#include &lt;pcap.h&gt;</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">#include &lt;remote-ext.h&gt;</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">/* 4 bytes IP address */</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">typedef struct ip_address</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">{</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;u_char byte1;</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;u_char byte2;</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;u_char byte3;</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;u_char byte4;</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">}ip_address;</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">/* IPv4 header */</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">typedef struct ip_header</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">{</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;u_char ver_ihl;&nbsp; /* Version (4 bits)版本 + Internet header length 首部长度(4 bits)*/</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;u_char tos;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Type of service 服务类型*/</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;u_short tlen;&nbsp;&nbsp;&nbsp; /* Total length 16位标示*/</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;u_short identification; /* Identification */</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;u_short flags_fo;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Flags (3 bits)三位标志 + Fragment offset十三位偏移 (13 bits)*/</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;u_char ttl;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Time to live八位生存时间 */</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;u_char proto;&nbsp;&nbsp;&nbsp; /* Protocol八位协议 */</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;u_short crc;&nbsp;&nbsp;&nbsp;&nbsp; /* Header checksum 十六位首部校验和*/</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;ip_address saddr;/* Source address 32位源IP地址*/</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;ip_address daddr;/* Destination address 32位目的IP地址*/</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;u_int op_pad;&nbsp;&nbsp;&nbsp; /* Option + Padding选项和数据 */</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">}ip_header;</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">/* UDP header */</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">typedef struct udp_header</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">{</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;u_short sport;&nbsp;&nbsp; /* Source port源端口 */</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;u_short dport;&nbsp;&nbsp; /* Destination port 目的端口*/</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;u_short len;&nbsp;&nbsp;&nbsp;&nbsp; /* Datagram length 数据长度*/</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;u_short crc;&nbsp;&nbsp;&nbsp;&nbsp; /* Checksum 校验和*/</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">}udp_header;</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">//连接表元素的结构</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">typedef&nbsp;&nbsp;&nbsp; struct&nbsp; link_elem&nbsp;&nbsp;&nbsp; </span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">{</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ip_address saddr;&nbsp; //IP地址Source ip address</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ip_address daddr;&nbsp; //目标IP地址</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; u_short&nbsp;&nbsp;&nbsp; sport;&nbsp; // Source port</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; u_short&nbsp;&nbsp;&nbsp; dport;&nbsp; // Destinaion port</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; char&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; *mailsaddr; //源Email地址</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; char&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; *maildaddr; //目的Email地址</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; bool&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ifdata; //是否Data</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; time_t&nbsp;&nbsp;&nbsp;&nbsp; rtime; //到达时间</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">}link_elem; </span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;&nbsp;&nbsp; </span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;&nbsp;&nbsp; //邮件正文段</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">typedef&nbsp;&nbsp;&nbsp; struct data_content </span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;&nbsp;&nbsp; {</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; char *lcontent;&nbsp; //正文段</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; data_content *next_file; //下一个邮件正文段</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;&nbsp;&nbsp; }data_content;</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">/* Prototype of the packet handler */</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">void packet_handler(u_char* param, const struct pcap_pkthdr* header, const u_char* pkt_data);</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;int main() {</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;pcap_if_t* alldevs;</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;pcap_if_t* d;</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;int inum;</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;int i = 0;</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;pcap_t* adhandle;</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;char errbuf[PCAP_ERRBUF_SIZE];</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;u_int netmask;</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;char packet_filter[] = "ip and udp";</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;struct bpf_program fcode;////???????????????????????//</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;/* Retrieve the device list */</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &amp;alldevs, errbuf) == -1)</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;{</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;&nbsp;&nbsp; /*</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;&nbsp;&nbsp; 在UNIX操作系统结构中，它把各种外部设备也看成是文件。</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;&nbsp;&nbsp; 所以 fprintf不仅能把errbuf的内容输出到磁盘文件里，</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;&nbsp;&nbsp; 也可以输出到像终端这样的特殊文件里。printf是fprintf的一个特例，</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;&nbsp;&nbsp; 它固定的只能把相应内容输出到终端文件上。</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;&nbsp;&nbsp; */</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp; fprintf(stderr, "Error in pcap_findalldevs: %s\n", errbuf);</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp; exit(1);</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;}</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;/* Print the list*/</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;for (d = alldevs; d; d = d-&gt;next)</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;{</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp; printf("%d. %s", ++ i, d-&gt;name);</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp; if (d-&gt;description)</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp; {</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;&nbsp; printf(" (%s)\n", d-&gt;description);</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp; }</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp; else</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp; {</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;&nbsp; printf(" (No description available)\n");</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp; }</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;}</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;if (i == 0)</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;{</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp; printf("\nNo interfaces found! Make sure Winpcap is installed.\n");</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp; return -1;</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;}</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;printf("Enter the interface number (1 - %d):", i);</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;scanf("%d", &amp;inum);</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;if (inum &lt; 1 || inum &gt; i)</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;{</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp; printf("\nInterface number out of range.\n");</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp; /* Free the device list */</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp; pcap_freealldevs(alldevs);</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp; return -1;</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;}</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;/* Jump to the selected adapter */</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;for (d = alldevs, i = 0; i &lt; inum - 1; d = d-&gt;next, ++ i);</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;/* Open the device */</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;if ((adhandle = pcap_open(d-&gt;name, /* 设备名 */</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 65536, /* 所截包的大小 */</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
/* 65536 guarantees that the whole packet will be captured on all the
link layers */</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PCAP_OPENFLAG_PROMISCUOUS, /* 混杂模式 */</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1000,&nbsp; /* read timeout */</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NULL,&nbsp; /* authentication on the remote machine远程机器的权限 */</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; errbuf /* error buffer */</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;)) == NULL)</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;{</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp; fprintf(stderr, "\nUnable to open the adapter. %s is not supported by Winpcap\n");</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp; /* Free the devices list */</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp; pcap_freealldevs(alldevs);</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp; return -1;</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;}</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;/* Check the link layer. We support only Ethernet for simplicity </span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;int pcap_datalink(pcap_t* p)</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">返回链路层上的一个适配器。返回链路层的类型，链路层的类型包括：</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">l&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DLT_NULL： BSD回路封装；链路层协议头是一个4字节的域，</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 以主机字节顺序（host byte order），包含一个从socket.h来的PF_value。</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; 主机字节顺序（host byte order）是捕获数据包的机器的字节顺序，</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; 而PF_value是捕获数据包的机器的OS。如果一个读取一个文件，</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; 字节顺序和PF_value不一定是抓取文件的那些机器。</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">l&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DLT_EN10MB： 以太网（10Mb, 100Mb, 1000Mb, 或者更高）。</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">l&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DLT_IEEE802： IEEE802.5令牌环网。</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">l&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DLT_ARCNET：ARCNET。</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">l&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DLT_SLIP：SLIP。</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">l&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DLT_PPP：PPP；如果第一个字节是0xff或0x03，它是类HDLC帧上的PPP。</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">l&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DLT_FDDI：FDDI</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">l&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DLT_ATM_RFC1483：RFC1483LLC/SNAP ATM；数据包以IEEE802.2 LLC头开始。</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">l&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DLT_RAW：原始IP（raw IP）；数据包以IP头开始。</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">l&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DLT_PPP_SERIAL：按照RFC1662，基于类HDLC帧的PPP，或者按照RFC1547的4.3.1，</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 基于HDLC帧的Cisco PPP；前者的第一个字节是0xFF，后者的第一个字节是0x0F或0x8F。</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">l&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DLT_PPP_ETHER：按照RFC2516，PPPoE；数据包以PPPoE头开始。</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">l&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DLT_C_HDLC：按照RFC1547的4.3.1，基于HDLC帧的Cisco PPP。</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">l&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DLT_IEEE802_11：IEEE 802.11无线局域网。</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">l&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DLT_FRELAY：帧中继（Frame Relay）。</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">l&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DLT_LOOP：OpenBSD回路封装。</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">l&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DLT_LINUX_SLL：Linux抓包封装。</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">l&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DLT_LTALK：苹果的LocalTalk，数据包以AppleTalk LLAP头开始。</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">l&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DLT_PFLOG：OpenBSD pflog。</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">l&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DLT_PRISM_HEADER：后接802.11头的棱镜监视器模式（Prism monitor mode）信息。</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">l&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DLT_IP_OVER_FC：RFC2625 IP-over-Fiber 频道，以RFC2625中定义的Network_Header开始。</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">l&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DLT_SUNATM：SunATM设备。</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">l&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DLT_IEEE802_11_RADIO：后接802.11头的链路层信息。</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">l&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DLT_ARCNET_LINUX：没有异常帧的ARCNET。</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">l&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DLT_LINUX_IRDA：Linux-IrDA数据包，DLT_LINUX_SLL头后接IrLAP头。</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">*/</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;if (pcap_datalink(adhandle) != DLT_EN10MB)</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;{</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp; fprintf(stderr, "\nThis program works only on Ethernet networks.\n");</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp; /* Free the devices list */</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp; pcap_freealldevs(alldevs);</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp; return -1;</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;}</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">/******************************************************</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">就是将pcap_addr结构体中的netmask字段赋值给netmask变量。</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">if判断表示如果网卡d的addresses不为NULL，</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">那么就将其中的netmask转化成相应格式的数据并赋值给netmask；</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">否则，就将netmask变量赋值为ffffffff。</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">********************************************************/</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;if (d-&gt;addresses != NULL)</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;{</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp; /* Retrieve the mask of the first address of the interface */</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp; netmask = ((struct sockaddr_in *)(d-&gt;addresses-&gt;netmask))-&gt;sin_addr.S_un.S_addr;//????????????</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;}</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;else</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;{</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp; /* If the interface is without addresses we suppose to be in a C class network */</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp; netmask = 0xffffffff;</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;}</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;/* complie the filter */</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;if (pcap_compile(adhandle, &amp;fcode, packet_filter, 1, netmask) &lt; 0)</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;{</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp; fprintf(stderr, "\nUnable to compile the packet filter. Check the syntax.\n");</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp; /* Free the devices list */</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp; pcap_freealldevs(alldevs);</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp; return -1;</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;}</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;/* set the filter */</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;if (pcap_setfilter(adhandle, &amp;fcode) &lt; 0)</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;{</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp; fprintf(stderr, "\nError setting the filter.\n");</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp; /* Free the devices list */</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp; pcap_freealldevs(alldevs);</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp; return -1;</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;}</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;printf("\nlistening on %s ...\n", d-&gt;description);</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;/* At this point,we don't need any more the device list. Free it */</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;pcap_freealldevs(alldevs);</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;/* Start the capture */</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;pcap_loop(adhandle, 0, packet_handler, NULL);</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;return 1;</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">}</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">/* Callback function invoked by libpcap for every incoming packet */</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">void packet_handler(u_char* param, const struct pcap_pkthdr* header, const u_char* pkt_data){</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;struct tm* ltime;</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;char timestr[16];</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;ip_header* ih;</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;udp_header* uh;</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;u_int ip_len;</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;u_short sport, dport;</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;/* convert the timestamp to readable format */</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;ltime = localtime(&amp;header-&gt;ts.tv_sec);</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;strftime(timestr, sizeof(timestr), "%H:%M:%S", ltime);</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;/* print timestamp and length of the packet */</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;printf("%s.%.6d len: %d ", timestr, header-&gt;ts.tv_usec, header-&gt;len);</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;/* retrieve the position of the ip header */</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;ih = (ip_header*)(pkt_data + 14);&nbsp; /* length of ethernet header MAC头是14字节*/</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;/* retrieve the position of the udp header */</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;&nbsp;&nbsp; &nbsp;/***********************************************</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;&nbsp;&nbsp; &nbsp;ih-&gt;ver_ihl &amp; 0xf就是ih-&gt;ver_ihl与00001111按位与，</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;&nbsp;&nbsp; &nbsp;这样就能取得ih-&gt;ver_ihl的后四位的值，</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;&nbsp;&nbsp; &nbsp;也就是ip报头中的“报头长度”字段，</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;&nbsp;&nbsp; &nbsp;再将该按位与的结果乘以4（该字段以4个字节为基本单位），</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;&nbsp;&nbsp; &nbsp;就得到ip报头有多少个字节。</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;&nbsp;&nbsp; &nbsp;************************************************/</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;ip_len = (ih-&gt;ver_ihl &amp; 0xf) * 4</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">/*IP包头的长度域来获得Ip包头的大小*/;</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;uh = (udp_header*)((u_char*)ih + ip_len);</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;/* convert from network byte order to host byte order */</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;/*sport = ntohs(uh-&gt;sport);</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;dport = ntohs(uh-&gt;dport);*/</span><br style="color: rgb(102, 51, 255);">
<br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;/* print ip addresses and udp ports */</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp;printf("%d.%d.%d.%d -&gt; %d.%d.%d.%d\n",</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp; ih-&gt;saddr.byte1,</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp; ih-&gt;saddr.byte2,</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp; ih-&gt;saddr.byte3,</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp; ih-&gt;saddr.byte4,</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp; /*sport,*/</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp; ih-&gt;daddr.byte1,</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp; ih-&gt;daddr.byte2,</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp; ih-&gt;daddr.byte3,</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp; ih-&gt;daddr.byte4</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">&nbsp; /*dport*/);</span><br style="color: rgb(102, 51, 255);">
<span style="color: rgb(102, 51, 255);">}</span><br>
</a>]]></description>
</item><item>
<title><![CDATA[Pragma学习总结]]></title>
<link>http://blogger.org.cn/blog/more.asp?name=ellx&amp;id=13064</link>
<author>ellx</author>
<pubDate>2006/3/25 14:06:18</pubDate>
<description><![CDATA[<span style="" lang="EN-US"><span style="">1．<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;">&nbsp;
</span></span></span><!--[endif]--><span style="font-family: 宋体;">在所有的预处理指令中，</span><span lang="EN-US">#Pragma </span><span style="font-family: 宋体;">指令可能是最复杂的了，它的作用是设定编译器的状态或者是指示编译器完成一些特定的动作。</span><span lang="EN-US">#pragma</span><span style="font-family: 宋体;">指令对每个编译器给出了一个方法</span><span lang="EN-US">,</span><span style="font-family: 宋体;">在保持与</span><span lang="EN-US">C</span><span style="font-family: 宋体;">和</span><span lang="EN-US">C ++</span><span style="font-family: 宋体;">语言完全兼容的情况下</span><span lang="EN-US">,</span><span style="font-family: 宋体;">给出主机或操作系统专有的特征。依据定义</span><span lang="EN-US">,</span><span style="font-family: 宋体;">编译指示是机器或操作系统专有的</span><span lang="EN-US">,</span><span style="font-family: 宋体;">且对于每个编译器都是不同的。</span><br>
<p class="MsoNormal" style="margin-left: 18pt; text-indent: -18pt;"><span lang="EN-US">
</span><span style="font-family: 宋体;">其格式一般为</span><span lang="EN-US">:
#Pragma Para<br>
</span><span style="font-family: 宋体;">其中</span><span lang="EN-US">Para </span><span style="font-family: 宋体;">为参数，下面来看一些常用的参数。</span><span lang="EN-US"><br>
<br>
(1)message </span><span style="font-family: 宋体;">参数。</span><span lang="EN-US"> Message </span><span style="font-family: 宋体;">参数是我最喜欢的一个参数，它能够在编译信息输出窗</span><span lang="EN-US"><br>
</span><span style="font-family: 宋体;">口中输出相应的信息，这对于源代码信息的控制是非常重要的。其使用方法为：</span><span lang="EN-US"><br>
#Pragma message(“</span><span style="font-family: 宋体;">消息文本</span><span lang="EN-US">”)<br>
</span><span style="font-family: 宋体;">当编译器遇到这条指令时就在编译输出窗口中将消息文本打印出来。</span><span lang="EN-US"><br>
</span><span style="font-family: 宋体;">当我们在程序中定义了许多宏来控制源代码版本的时候，我们自己有可能都会忘记有没有正确的设置这些宏，此时我们可以用这条指令在编译的时候就进行检查。假设我们希望判断自己有没有在源代码的什么地方定义了</span><span lang="EN-US">_X86</span><span style="font-family: 宋体;">这个宏可以用下面的方法</span><span lang="EN-US"><br>
#ifdef _X86<br>
#Pragma message(“_X86 macro activated!”)<br>
#endif<br>
</span><span style="font-family: 宋体;">当我们定义了</span><span lang="EN-US">_X86</span><span style="font-family: 宋体;">这个宏以后，应用程序在编译时就会在编译输出窗口里显示</span><span lang="EN-US">“_<br>
X86 macro activated!”</span><span style="font-family: 宋体;">。我们就不会因为不记得自己定义的一些特定的宏而抓耳挠腮了</span><span lang="EN-US"><br>
</span><span style="font-family: 宋体;">。</span><span lang="EN-US"><br>
<br>
(2)</span><span style="font-family: 宋体;">另一个使用得比较多的</span><span lang="EN-US">pragma</span><span style="font-family: 宋体;">参数是</span><span lang="EN-US">code_seg</span><span style="font-family: 宋体;">。格式如：</span><span lang="EN-US"><br>
#pragma code_seg( ["section-name"[,"section-class"] ] )<br>
</span><span style="font-family: 宋体;">它能够设置程序中函数代码存放的代码段，当我们开发驱动程序的时候就会使用到它。</span><span lang="EN-US"><br>
<br>
<span style="color: red;">(3)#pragma once (</span></span><span style="font-family: 宋体; color: red;">比较常用）</span><span style="color: red;" lang="EN-US"><br>
</span><span style="font-family: 宋体; color: red;">只要在头文件的最开始加入这条指令就能够保证头文件被编译一次，这条指令实际上在</span><span style="color: red;" lang="EN-US">VC6</span><span style="font-family: 宋体; color: red;">中就已经有了，但是考虑到兼容性并没有太多的使用它。</span><span style="color: red;" lang="EN-US"><br>
</span><span lang="EN-US"><br>
(4)#pragma hdrstop</span><span style="font-family: 宋体;">表示预编译头文件到此为止，后面的头文件不进行预编译。</span><span lang="EN-US">BCB</span><span style="font-family: 宋体;">可以预编译头文件以加快链接的速度，但如果所有头文件都进行预编译又可能占太多磁盘空间，所以使用这个选项排除一些头文件。</span><span lang="EN-US"> <br>
</span><span style="font-family: 宋体;">有时单元之间有依赖关系，比如单元</span><span lang="EN-US">A</span><span style="font-family: 宋体;">依赖单元</span><span lang="EN-US">B</span><span style="font-family: 宋体;">，所以单元</span><span lang="EN-US">B</span><span style="font-family: 宋体;">要先于单元</span><span lang="EN-US">A</span><span style="font-family: 宋体;">编译。你可以用</span><span lang="EN-US">#pragma
startup</span><span style="font-family: 宋体;">指定编译优先级，如果使用了</span><span lang="EN-US">#pragma
package(smart_init) </span><span style="font-family: 宋体;">，</span><span lang="EN-US">BCB</span><span style="font-family: 宋体;">就会根据优先级的大小先后编译。</span><span lang="EN-US"> <br>
<br>
(5)#pragma resource "*.dfm"</span><span style="font-family: 宋体;">表示把</span><span lang="EN-US">*.dfm</span><span style="font-family: 宋体;">文件中的资源加入工程。</span><span lang="EN-US">*.dfm</span><span style="font-family: 宋体;">中包括窗体</span><span lang="EN-US"><br>
</span><span style="font-family: 宋体;">外观的定义。</span><span lang="EN-US"> <br>
<br>
(6)#pragma warning( disable : 4507 34; once : 4385; error : 164 )<br>
</span><span style="font-family: 宋体;">等价于：</span><span lang="EN-US"><br>
#pragma warning(disable:4507 34) // </span><span style="font-family: 宋体;">不显示</span><span lang="EN-US">4507</span><span style="font-family: 宋体;">和</span><span lang="EN-US">34</span><span style="font-family: 宋体;">号警告信息</span><span lang="EN-US"><br>
#pragma warning(once:4385) // 4385</span><span style="font-family: 宋体;">号警告信息仅报告一次</span><span lang="EN-US"><br>
#pragma warning(error:164) // </span><span style="font-family: 宋体;">把</span><span lang="EN-US">164</span><span style="font-family: 宋体;">号警告信息作为一个错误。</span><span lang="EN-US"><br>
</span><span style="font-family: 宋体;">同时这个</span><span lang="EN-US">pragma
warning </span><span style="font-family: 宋体;">也支持如下格式：</span><span lang="EN-US"><br>
#pragma warning( push [ ,n ] )<br>
#pragma warning( pop )<br>
</span><span style="font-family: 宋体;">这里</span><span lang="EN-US">n</span><span style="font-family: 宋体;">代表一个警告等级</span><span lang="EN-US">(1---4)</span><span style="font-family: 宋体;">。</span><span lang="EN-US"><br>
#pragma warning( push )</span><span style="font-family: 宋体;">保存所有警告信息的现有的警告状态。</span><span lang="EN-US"><br>
#pragma warning( push, n)</span><span style="font-family: 宋体;">保存所有警告信息的现有的警告状态，并且把全局警告</span><span lang="EN-US"><br>
</span><span style="font-family: 宋体;">等级设定为</span><span lang="EN-US">n</span><span style="font-family: 宋体;">。</span><span lang="EN-US"> <br>
#pragma warning( pop )</span><span style="font-family: 宋体;">向栈中弹出最后一个警告信息，在入栈和出栈之间所作的</span><span lang="EN-US"><br>
</span><span style="font-family: 宋体;">一切改动取消。例如：</span><span lang="EN-US"><br>
#pragma warning( push )<br>
#pragma warning( disable : 4705 )<br>
#pragma warning( disable : 4706 )<br>
#pragma warning( disable : 4707 )<br>
//.......<br>
#pragma warning( pop ) <br>
</span><span style="font-family: 宋体;">在这段代码的最后，重新保存所有的警告信息</span><span lang="EN-US">(</span><span style="font-family: 宋体;">包括</span><span lang="EN-US">4705</span><span style="font-family: 宋体;">，</span><span lang="EN-US">4706</span><span style="font-family: 宋体;">和</span><span lang="EN-US">4707)</span><span style="font-family: 宋体;">。</span><span lang="EN-US"><br>
</span><span style="font-family: 宋体;">（</span><span lang="EN-US">7</span><span style="font-family: 宋体;">）</span><span lang="EN-US">pragma comment(...)<br>
</span><span style="font-family: 宋体;">该指令将一个注释记录放入一个对象文件或可执行文件中。</span><span lang="EN-US"><br>
</span><span style="font-family: 宋体;">常用的</span><span lang="EN-US">lib</span><span style="font-family: 宋体;">关键字，可以帮我们连入一个库文件。</span></p>]]></description>
</item><item>
<title><![CDATA[SMTP协议通讯模型]]></title>
<link>http://blogger.org.cn/blog/more.asp?name=ellx&amp;id=13063</link>
<author>ellx</author>
<pubDate>2006/3/27 13:03:35</pubDate>
<description><![CDATA[<a>　

</a>
<p class="MsoNormal"><span class="txt"><b><span lang="EN-US">SMTP</span></b></span><span class="txt"><b><span style="font-family: 宋体;">协议通讯模型</span></b></span><span lang="EN-US"><br>
<br>
</span><span class="txt"><span style="font-family: 宋体;">　　</span><span lang="EN-US">SMTP</span></span><span class="txt"><span style="font-family: 宋体;">协议是</span><span lang="EN-US">TCP/IP</span></span><span class="txt"><span style="font-family: 宋体;">协议族中的一员，主要对如何将</span> </span><span class="txt"><span style="font-family: 宋体;">电子邮件从发送方地址传送到接收方地址，也即是对传输的规则做了规定。</span><span lang="EN-US">SMTP</span></span><span class="txt"><span style="font-family: 宋体;">协议的通信模型并不复杂，主要工作集中在发送</span><span lang="EN-US">SMTP</span></span><span class="txt"><span style="font-family: 宋体;">和接收</span><span lang="EN-US">SMTP</span></span><span class="txt"><span style="font-family: 宋体;">上：首</span>
</span><span class="txt"><span style="font-family: 宋体;">先针对用户发出的邮件请求，由发送</span><span lang="EN-US">SMTP</span></span><span class="txt"><span style="font-family: 宋体;">建立一条连接到接收</span><span lang="EN-US">SMTP</span></span><span class="txt"><span style="font-family: 宋体;">的双工通讯链路，这里的接收</span><span lang="EN-US">SMTP</span></span><span class="txt"><span style="font-family: 宋体;">是相对于发送</span><span lang="EN-US">SMTP</span></span><span class="txt"><span style="font-family: 宋体;">而言的，实际上它既可以是</span>
</span><span class="txt"><span style="font-family: 宋体;">最终的接收者也可以是中间传送者。发送</span><span lang="EN-US">SMTP</span></span><span class="txt"><span style="font-family: 宋体;">负责向接收</span><span lang="EN-US">SMTP</span></span><span class="txt"><span style="font-family: 宋体;">发送</span><span lang="EN-US">SMTP</span></span><span class="txt"><span style="font-family: 宋体;">命令，而接收</span><span lang="EN-US">SMTP</span></span><span class="txt"><span style="font-family: 宋体;">则负责接收并反馈应答。可大致用下面的通讯模型示意图</span>
</span><span class="txt"><span style="font-family: 宋体;">来表示：<br>
<br>
</span></span></p>


<p class="MsoNormal"><span lang="EN-US"><!--[if gte vml 1]><v:shapetype id="_x0000_t75"
 coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe"
 filled="f" stroked="f">
 <v:stroke joinstyle="miter"/>
 <v:formulas>
  <v:f eqn="if lineDrawn pixelLineWidth 0"/>
  <v:f eqn="sum @0 1 0"/>
  <v:f eqn="sum 0 0 @1"/>
  <v:f eqn="prod @2 1 2"/>
  <v:f eqn="prod @3 21600 pixelWidth"/>
  <v:f eqn="prod @3 21600 pixelHeight"/>
  <v:f eqn="sum @0 0 1"/>
  <v:f eqn="prod @6 1 2"/>
  <v:f eqn="prod @7 21600 pixelWidth"/>
  <v:f eqn="sum @8 21600 0"/>
  <v:f eqn="prod @7 21600 pixelHeight"/>
  <v:f eqn="sum @10 21600 0"/>
 </v:formulas>
 <v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"/>
 <o:lock v:ext="edit" aspectratio="t"/>
</v:shapetype><v:shape id="_x0000_i1025" type="#_x0000_t75" style='width:294.75pt;
 height:143.25pt'>
 <v:imagedata src="/C:\DOCUME~1\admin\LOCALS~1\Temp\msohtml1\01\clip_image001.png"
  o:title=""/>
</v:shape><![endif]--><!--[if !vml]--><img src="file:///C:%5CDOCUME%7E1%5Cadmin%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_image002.jpg" v:shapes="_x0000_i1025" height="191" width="393"><!--[endif]--></span></p>


<p class="MsoNormal"><span lang="EN-US"><o:p>&nbsp;</o:p></span></p>


<p class="MsoNormal"><span class="txt"><b><span style="font-family: 宋体; color: rgb(172, 0, 0);">在应用程序中使用</span><span style="color: rgb(172, 0, 0);" lang="EN-US">SMTP</span></b></span><span class="txt"><b><span style="font-family: 宋体; color: rgb(172, 0, 0);">协议</span></b></span><span lang="EN-US"><br>
<br>
</span><span class="txt"><span style="font-family: 宋体;">　　</span><b><span lang="EN-US">SMTP</span></b></span><span class="txt"><b><span style="font-family: 宋体;">协议的会话流程</span></b></span><span lang="EN-US"><br>
<br>
</span><span class="txt"><span style="font-family: 宋体;">　</span> </span><span class="txt"><span style="font-family: 宋体;">　在进行程序设计之前有必要弄清</span><span lang="EN-US">SMTP</span></span><span class="txt"><span style="font-family: 宋体;">协议的会话流程，其实前面介绍的内容已经可以大致勾勒出用</span><span lang="EN-US">SMTP</span></span><span class="txt"><span style="font-family: 宋体;">发送邮件的框架了，对于一次普通的邮件发送，其</span>
</span><span class="txt"><span style="font-family: 宋体;">过程大致为：先建立</span><span lang="EN-US">TCP</span></span><span class="txt"><span style="font-family: 宋体;">连接，随后客户端发出</span><span lang="EN-US">HELLO</span></span><span class="txt"><span style="font-family: 宋体;">命令以标识发件人自己的身份，并继续由客户端发送</span><span lang="EN-US">MAIL</span></span><span class="txt"><span style="font-family: 宋体;">命令，如服务器应答为</span><span lang="EN-US">"OK"</span></span><span class="txt"><span style="font-family: 宋体;">，可继续</span>
</span><span class="txt"><span style="font-family: 宋体;">发送</span><span lang="EN-US">RCPT</span></span><span class="txt"><span style="font-family: 宋体;">命令来标识电子邮件的收件人，在这里可以有多个</span><span lang="EN-US">RCPT</span></span><span class="txt"><span style="font-family: 宋体;">行，而服务器端则表示是否愿意为收件人接受该邮件。在双方协商结束后，用命令</span><span lang="EN-US">DATA </span></span><span class="txt"><span style="font-family: 宋体;">将邮件发送出去，其中对表示结束的</span><span lang="EN-US">"."</span></span><span class="txt"><span style="font-family: 宋体;">也一并发送出去。随后结束本次发送过程，以</span><span lang="EN-US">QUIT</span></span><span class="txt"><span style="font-family: 宋体;">命令退出。下面通过一个实例，从</span><span lang="EN-US"> langrui@sohu.com</span></span><span class="txt"><span style="font-family: 宋体;">发送邮件到</span><span lang="EN-US">renping@sina.com</span></span><span class="txt"><span style="font-family: 宋体;">来更详细直观地描述此会话流程：</span><span lang="EN-US"><o:p></o:p></span></span></p>


<p class="MsoNormal"><span class="txt"><span lang="EN-US">R:220 sina.com Simple Mail
Transfer Service Ready</span></span><span lang="EN-US"><br>
<span class="txt">S:HELLO sohu.com</span><br>
<span class="txt">R:250 sina.com</span><br>
<span class="txt">S:MAIL FROM:</span></span><span class="txt"><span style="font-family: 宋体;">＜</span><span lang="EN-US">langrui@sohu.com</span></span><span class="txt"><span style="font-family: 宋体;">＞</span></span><span lang="EN-US"><br>
<span class="txt">R:250 OK</span><br>
<span class="txt">S:RCPT TO:</span></span><span class="txt"><span style="font-family: 宋体;">＜</span><span lang="EN-US">renping@sina.com</span></span><span class="txt"><span style="font-family: 宋体;">＞</span></span><span lang="EN-US"><br>
<span class="txt">R:250 OK</span><br>
<span class="txt">S:DATA</span><br>
<span class="txt">R:354 Start mail input;end with "</span></span><span class="txt"><span style="font-family: 宋体;">＜</span><span lang="EN-US">CRLF</span></span><span class="txt"><span style="font-family: 宋体;">＞</span><span lang="EN-US">.</span></span><span class="txt"><span style="font-family: 宋体;">＜</span><span lang="EN-US">CRLF</span></span><span class="txt"><span style="font-family: 宋体;">＞</span><span lang="EN-US">"</span></span><span lang="EN-US"><br>
<span class="txt">S:……</span><br>
<span class="txt">R:250 OK</span><br>
<span class="txt">S:QUIT</span><br>
<span class="txt">R:221 sina.com Service closing transmission channel<o:p></o:p></span></span></p>


<p class="MsoNormal"><span class="txt"><span lang="EN-US"><o:p>&nbsp;</o:p></span></span></p>


<p class="MsoNormal" style="margin-bottom: 12pt; text-align: left;" align="left"><b><span style="font-size: 12pt; font-family: 宋体;">邮件的格式化</span></b><span style="font-size: 12pt; font-family: 宋体;" lang="EN-US"><br>
<br>
</span><span style="font-size: 12pt; font-family: 宋体;">　　由于电子邮件结构上的特殊性，在传输时是不能当作简单的文本来直接 处理的，而必须按照一定的格式对邮件头和邮件体进行格式化处理之后才可以被发送。需要进行格式化的部分主要有：发件人地址、收件人地址、主题和发送日期
等。在<span lang="EN-US">RFC</span>文档的<span lang="EN-US">RFC 822</span>里对邮件的格式化有详尽的说明，有关详情请参阅该文档。下面通过<span lang="EN-US">VC++6.0</span>按照<span lang="EN-US">RFC 822</span>文档规定将格式化邮件的部分编写如下（部分代码）：<span lang="EN-US"><o:p></o:p></span></span></p>


<table class="MsoNormalTable" style="background: white none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; width: 100%;" border="0" cellpadding="0" width="100%">

 <tbody><tr style="">
  <td style="padding: 0.75pt;">
  <p class="MsoNormal" style="text-align: left;" align="left"><span style="font-size: 12pt; font-family: 宋体;" lang="EN-US">//</span><span style="font-size: 12pt; font-family: 宋体;">邮件头准备<span lang="EN-US"><br>
  strTemp = _T( "From: " ) + m_strFrom; file://</span>发件人地址<span lang="EN-US"><br>
  add_header_line( (LPCTSTR)strTemp );<br>
  strTemp = _T( "To: " ) + m_strTo; file://</span>收件人地址<span lang="EN-US"><br>
  add_header_line( (LPCTSTR)strTemp );<br>
  m_tDateTime = m_tDateTime.GetCurrentTime();//</span>发送时间<span lang="EN-US"><br>
  strTemp = _T( "Data: " );<br>
  strTemp += m_tDateTime.Format( "%a, %d %b %y %H:%M:%S %Z" );<br>
  add_header_line( (LPCTSTR)strTemp );<br>
  strTemp = _T( "Subject: " ) + m_strSubject; file://</span>主题<span lang="EN-US"><br>
  add_header_line( (LPCTSTR)strTemp );<br>
  file://</span>邮件头结束<span lang="EN-US"><br>
  m_strHeader += _T( "\r\n" );<br>
  file://</span>邮件体准备<span lang="EN-US"><br>
  if( m_strBody.Right( 2 ) != _T( "\r\n" ) ) file://</span>确认最后以回车换行结束<span lang="EN-US"><br>
  m_strBody += _T( "\r\n" ); <o:p></o:p></span></span></p>
  </td>
 </tr>
</tbody>
</table>


<p class="MsoNormal"><span style="font-size: 12pt; font-family: 宋体;" lang="EN-US"><br>
</span><span style="font-size: 12pt; font-family: 宋体;">　 　其中<span lang="EN-US">add_header_line(LPCTSTR
szHeaderLine)</span>函数用于把<span lang="EN-US">szHeaderLine</span>指向的字串追加到<span lang="EN-US">m_strHeader</span>后面。其中，格式化后的邮件头保存在<span lang="EN-US"> m_strHeader</span>里，格式化后的邮件体保存在<span lang="EN-US">m_strBody</span>中。</span></p>


<p class="MsoNormal"><span lang="EN-US"><o:p>&nbsp;</o:p></span></p>


<p class="MsoNormal" style="margin-bottom: 12pt; text-align: left;" align="left"><b><span style="font-size: 12pt; font-family: 宋体;">由<span lang="EN-US">Socket</span>套接字为<span lang="EN-US">SMTP</span>提供网络通讯基础</span></b><span style="font-size: 12pt; font-family: 宋体;" lang="EN-US"><br>
<br>
</span><span style="font-size: 12pt; font-family: 宋体;">　　许多网络程序都是采用<span lang="EN-US"> Socket</span>套接字实现的，对于一些标准的网络协议如<span lang="EN-US">HTTP</span>、<span lang="EN-US">FTP</span>和<span lang="EN-US">SMTP</span>等协议的编程也是基于套接字程序的，只是端口号不再是随意设定而要由协议
来指定，比如<span lang="EN-US">HTTP</span>端口在<span lang="EN-US">80</span>、<span lang="EN-US">FTP</span>是<span lang="EN-US">21</span>，而<span lang="EN-US">SMTP</span>则是<span lang="EN-US">25</span>。<span lang="EN-US">Socket</span>只是提供在指定的端口上同指定的服务器从事网络上的通讯能力，至于客户
和服务器之间是如何通讯的则由网络协议来规定，这对于套接字是完全透明的。因此可以使用<span lang="EN-US">Socket</span>套接字为程序提供网络通讯的能力，而对于网络通讯连路
建立好之后采取什么样的通讯应答则要按<span lang="EN-US">SMTP</span>协议的规定去执行了。<span lang="EN-US">Socket</span>套接字网络编程方面的文章资料非常丰富，限于本文篇幅，在此不再赘述，有
关详情请参阅相关文档。为简便起见，没有采用编写较复杂的<span lang="EN-US">Windows Sockets API</span>进行编程，而是使用经过较好封装的<span lang="EN-US">MFC </span>的<span lang="EN-US">CSocket</span>类。在正式使用套接字之前，也要先用<span lang="EN-US">AfxSocketInit()</span>函数对套接字进行初始化，然后用<span lang="EN-US">Create()</span>创建套接字对象，并
由该套接字通过<span lang="EN-US">Connect</span>（）建立同邮件服务器的连接。如果一切正常，再后续的工作中就是遵循<span lang="EN-US">SMTP</span>协议的约定来使用<span lang="EN-US">Send</span>（）、<span lang="EN-US">Receive
()</span>函数来发送<span lang="EN-US">SMTP</span>命令和接收邮件服务器发来的应答码以完成对邮件的传送。<span lang="EN-US"><br>
<br>
</span>　　<b><span lang="EN-US">SMTP</span>会话应答的实现</b><span lang="EN-US"><br>
<br>
</span>　 　在同邮件服务器建立好链路连接后就可以按前面介绍过的会话流程进行程序设计了，对于<span lang="EN-US">SMTP</span>命令的发送，可按命令格式将其组帧完毕后用<span lang="EN-US">CSocket</span>类 的<span lang="EN-US">Send()</span>函数将其发送到服务器，并通过<span lang="EN-US">CSocket</span>类的<span lang="EN-US">Receive()</span>函数接收从邮件服务器发来的应答码，并根据<span lang="EN-US">SMTP</span>协议的应答码表对其 做出响应的处理。下面是用于接收应答码的函数<span lang="EN-US">get_response()</span>的部分实现代码：<span lang="EN-US"><o:p></o:p></span></span></p>


<table class="MsoNormalTable" style="background: white none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; width: 100%;" border="0" cellpadding="0" width="100%">

 <tbody><tr style="">
  <td style="padding: 0.75pt;">
  <p class="MsoNormal" style="text-align: left;" align="left"><span style="font-size: 12pt; font-family: 宋体;" lang="EN-US">BOOL CSMTP::get_response( UINT response_expected )//</span><span style="font-size: 12pt; font-family: 宋体;">输入参数为希望的应答码<span lang="EN-US"><br>
  {<br>
  ……<br>
  // m_wsSMTPServer</span>为<span lang="EN-US">CSocket</span>的类对象，调用<span lang="EN-US">Receive()</span>将应答码接收到缓存<span lang="EN-US"><br>
  // response_buf</span>中<span lang="EN-US"><br>
  m_wsSMTPServer.Receive( response_buf, RESPONSE_BUFFER_SIZE )<br>
  sResponse = response_buf;<br>
  sscanf( (LPCTSTR)sResponse.Left( 3 ), _T( "%d" ), &amp;response );<br>
  pResp = &amp;response_table[ response_expected ];<br>
  file://</span>检验收到的应答码是否是所希望得到的<span lang="EN-US"><br>
  if( response != pResp-</span>＞<span lang="EN-US">nResponse )<br>
  {<br>
  ……//</span>不相等的话进行错误处理<span lang="EN-US"><br>
  return FALSE;<br>
  }<br>
  return TRUE;<br>
  }<o:p></o:p></span></span></p>
  </td>
 </tr>
</tbody>
</table>


<p class="MsoNormal" style="margin-bottom: 12pt; text-align: left;" align="left"><span style="font-size: 12pt; font-family: 宋体;" lang="EN-US"><br>
</span><span style="font-size: 12pt; font-family: 宋体;">　　会话的各个部分比较类似，都是命令<span lang="EN-US">--</span>应答方式，而且均成对出现，下面是本文的重点也是实现的关键部分<span lang="EN-US">--</span>在程序控制下完成对<span lang="EN-US">SMTP</span>命令的格式化以及对命令的发送和对邮件服务器应答码的检验处理：<span lang="EN-US"><o:p></o:p></span></span></p>


<table class="MsoNormalTable" style="background: white none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; width: 100%;" border="0" cellpadding="0" width="100%">

 <tbody><tr style="">
  <td style="padding: 0.75pt;">
  <p class="MsoNormal" style="text-align: left;" align="left"><span style="font-size: 12pt; font-family: 宋体;" lang="EN-US">//</span><span style="font-size: 12pt; font-family: 宋体;">格式化并发送<span lang="EN-US">HELLO</span>命令，并接收、验证服务器应答码<span lang="EN-US"><br>
  gethostname( local_host, 80 );<br>
  sHello.Format( _T( "HELO %s\r\n" ), local_host );<br>
  m_wsSMTPServer.Send( (LPCTSTR)sHello, sHello.GetLength() );<br>
  if( !get_response( GENERIC_SUCCESS ) ) file://</span>检验应答码是否为<span lang="EN-US">250<br>
  {<br>
  ……<br>
  return FALSE;<br>
  }<br>
  file://</span>格式化并发送<span lang="EN-US">MAIL</span>命令，并接收、验证服务器应答码<span lang="EN-US"><br>
  sFrom.Format( _T( "MAIL From: </span>＜<span lang="EN-US">%s</span>＞<span lang="EN-US">\r\n" ), (LPCTSTR)msg-</span>＞<span lang="EN-US">m_strFrom );<br>
  m_wsSMTPServer.Send( (LPCTSTR)sFrom, sFrom.GetLength() );<br>
  if( !get_response( GENERIC_SUCCESS ) ) file://</span>检验应答码是否为<span lang="EN-US">250<br>
  return FALSE;<br>
  file://</span>格式化并发送<span lang="EN-US">RCPT</span>命令，并接收、验证服务器应答码<span lang="EN-US"><br>
  sEmail=(LPCTSTR)msg-</span>＞<span lang="EN-US">m_strTo;<br>
  sTo.Format( _T( "RCPT TO: </span>＜<span lang="EN-US">%s</span>＞<span lang="EN-US">\r\n" ), (LPCTSTR)sEmail );<br>
  m_wsSMTPServer.Send( (LPCTSTR)sTo, sTo.GetLength() );<br>
  if(!get_response( GENERIC_SUCCESS )) file://</span>检验应答码是否为<span lang="EN-US">250<br>
  return FALSE;<br>
  file://</span>格式化并发送<span lang="EN-US">DATA</span>命令，并接收、验证服务器应答码<span lang="EN-US"><br>
  sTemp = _T( "DATA\r\n" );<br>
  m_wsSMTPServer.Send( (LPCTSTR)sTemp, sTemp.GetLength() );<br>
  if( !get_response( DATA_SUCCESS ) ) file://</span>检验应答码是否为<span lang="EN-US">354<br>
  return FALSE;<br>
  file://</span>发送根据<span lang="EN-US">RFC 822</span>文档规定格式化过的邮件头<span lang="EN-US"><br>
  m_wsSMTPServer.Send( (LPCTSTR)msg-</span>＞<span lang="EN-US">m_strHeader, msg-</span>＞<span lang="EN-US">m_strHeader.GetLength() );<br>
  ……<br>
  file://</span>发送根据<span lang="EN-US">RFC 822</span>文档规定格式化过的邮件体<span lang="EN-US"><br>
  sTemp = msg-</span>＞<span lang="EN-US">m_strBody;<br>
  if( sTemp.Left( 3 ) == _T( ".\r\n" ) )<br>
  sTemp = _T( "." ) + sTemp;<br>
  while( (nPos = sTemp.Find( szBad )) </span>＞<span lang="EN-US"> -1 )<br>
  {<br>
  sCooked = sTemp.Mid( nStart, nPos );<br>
  sCooked += szGood;<br>
  sTemp = sCooked + sTemp.Right( sTemp.GetLength() - (nPos + nBadLength) );<br>
  }<br>
  m_wsSMTPServer.Send( (LPCTSTR)sTemp, sTemp.GetLength() );<br>
  file://</span>发送内容数据结束标志<span lang="EN-US">"</span>＜<span lang="EN-US">CRLF</span>＞<span lang="EN-US">.</span>＜<span lang="EN-US">CRLF</span>＞<span lang="EN-US">"</span>，并检验返回应答码<span lang="EN-US"><br>
  sTemp = _T( "\r\n.\r\n" );<br>
  m_wsSMTPServer.Send( (LPCTSTR)sTemp, sTemp.GetLength() );<br>
  if( !get_response( GENERIC_SUCCESS ) )// </span>检验应答码是否为<span lang="EN-US">250<br>
  return FALSE;<o:p></o:p></span></span></p>
  </td>
 </tr>
</tbody>
</table>


<p class="MsoNormal"><span style="font-size: 12pt; font-family: 宋体;" lang="EN-US"><br>
</span><span style="font-size: 12pt; font-family: 宋体;">　　到此为止，已基本在程序中体现出了<span lang="EN-US">SMTP</span>协议的会话流程，能在<span lang="EN-US">Socket</span>套接字所提供的网络通讯能力基础之上实现以<span lang="EN-US">SMTP</span>命令和<span lang="EN-US">SMTP</span>应答码为基本会话内容的通讯交互过程，从而最终实现<span lang="EN-US">SMTP</span>协议对电子邮件的发送。<span lang="EN-US"><br>
<br>
</span>　　<b><span style="color: rgb(172, 0, 0);">结论</span></b><span lang="EN-US"><br>
<br>
</span>　 　电子邮件类软件作为<span lang="EN-US">Internet</span>上的应用软件，其设计开发必须符合<span lang="EN-US">Internet</span>上成熟的技术规范（如<span lang="EN-US">RFC</span>文档系列规范）和相关协议（如<span lang="EN-US"> POP</span>、<span lang="EN-US">SMTP</span>、<span lang="EN-US">IMAP</span>以及<span lang="EN-US">LDAP</span>等）。只有在遵循了上述规范和协议的基础上进行编程才能真正实现邮件类软件产品和服务的开放性和标准化。本文着重 对<span lang="EN-US">SMTP</span>协议及其在<span lang="EN-US">VC++</span>编程中的应用做了介绍，并按照<span lang="EN-US">SMTP</span>协议对电子邮件的发送进行了开放性和标准性较好的程序设计。本文所述程序在<span lang="EN-US">
Windows 98</span>下，由<span lang="EN-US">Microsoft Visual C++ 6.0</span>编译通过。<span lang="EN-US"><o:p></o:p></span></span></p>

<a><span style="font-size: 10.5pt; font-family: &quot;Times New Roman&quot;;" lang="EN-US"><!--[if gte vml 1]><v:shapetype id="_x0000_t75" coordsize="21600,21600"
 o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f"
 stroked="f">
 <v:stroke joinstyle="miter"/>
 <v:formulas>
  <v:f eqn="if lineDrawn pixelLineWidth 0"/>
  <v:f eqn="sum @0 1 0"/>
  <v:f eqn="sum 0 0 @1"/>
  <v:f eqn="prod @2 1 2"/>
  <v:f eqn="prod @3 21600 pixelWidth"/>
  <v:f eqn="prod @3 21600 pixelHeight"/>
  <v:f eqn="sum @0 0 1"/>
  <v:f eqn="prod @6 1 2"/>
  <v:f eqn="prod @7 21600 pixelWidth"/>
  <v:f eqn="sum @8 21600 0"/>
  <v:f eqn="prod @7 21600 pixelHeight"/>
  <v:f eqn="sum @10 21600 0"/>
 </v:formulas>
 <v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"/>
 <o:lock v:ext="edit" aspectratio="t"/>
</v:shapetype><v:shape id="_x0000_i1025" type="#_x0000_t75" style='width:294.75pt;
 height:143.25pt'>
 <v:imagedata src="file:///C:\DOCUME~1\admin\LOCALS~1\Temp\msohtml1\01\clip_image001.png"
  o:title=""/>
</v:shape><![endif]--><!--[if !vml]--><!--[endif]--></span></a>]]></description>
</item><item>
<title><![CDATA[指针学习笔记]]></title>
<link>http://blogger.org.cn/blog/more.asp?name=ellx&amp;id=13062</link>
<author>ellx</author>
<pubDate>2006/3/28 13:53:45</pubDate>
<description><![CDATA[<a name="OLE_LINK1"><span style="font-size: 12pt; font-family: 宋体;" lang="EN-US"><span style="">1.<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;">&nbsp;&nbsp;&nbsp;
</span></span></span><!--[endif]--><span style="font-size: 12pt; font-family: 宋体;">注意函数的存活期为此函数块结束，其中的变量就会销毁，所以说要想让传到函数中的值能够改变，就要使得函数的变量为指针类型，这样的话函数变量所指的东西就是内存地址中存放的东西。同时对于指针进行动态负值。<span lang="EN-US"><o:p></o:p></span></span></a>

<p class="MsoNormal"><span style=""><span style="font-size: 12pt; font-family: 宋体;">错误程序如下所示：<span lang="EN-US"><o:p></o:p></span></span></span></p>


<p><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">#include </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 8.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; color: rgb(51, 51, 51);">　　</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">int *pPointer; </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 8.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; color: rgb(51, 51, 51);">　　</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">void
SomeFunction() </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 8.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; color: rgb(51, 51, 51);">　　</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">{ </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 8.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; color: rgb(51, 51, 51);">　　</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US"> int nNumber; </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 8.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; color: rgb(51, 51, 51);">　　</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US"> nNumber = 25; </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 8.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; color: rgb(51, 51, 51);">　　</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US"> // </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; color: rgb(51, 51, 51);">使</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">pPointer</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; color: rgb(51, 51, 51);">指向</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">nNumber</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; color: rgb(51, 51, 51);">：</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);"> </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 8.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; color: rgb(51, 51, 51);">　　</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US"> pPointer =
&amp;nNumber; </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 8.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; color: rgb(51, 51, 51);">　　</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">} </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 8.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; color: rgb(51, 51, 51);">　　</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">void main() </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 8.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; color: rgb(51, 51, 51);">　　</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">{ </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 8.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; color: rgb(51, 51, 51);">　　</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US"> SomeFunction();
// </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; color: rgb(51, 51, 51);">让</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">pPointer</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; color: rgb(51, 51, 51);">指向某些东西</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);"> </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 8.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; color: rgb(51, 51, 51);">　　</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US"> // </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; color: rgb(51, 51, 51);">为什么这样会失败？</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);"> </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 8.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; color: rgb(51, 51, 51);">　　</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">
printf("Value of *pPointer: %d\n", *pPointer); </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 8.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; color: rgb(51, 51, 51);">　　</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">}&nbsp; </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 8.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p class="MsoNormal"><span style=""><span style="font-size: 12pt; font-family: 宋体;" lang="EN-US"><o:p>&nbsp;</o:p></span></span></p>


<p class="MsoNormal"><span style=""><span style="font-size: 12pt; font-family: 宋体;" lang="EN-US"><o:p>&nbsp;</o:p></span></span></p>


<p class="MsoNormal"><span style=""><span style="font-size: 12pt; font-family: 宋体;" lang="EN-US"><o:p>&nbsp;</o:p></span></span></p>


<p class="MsoNormal"><span style=""><span style="font-size: 12pt; font-family: 宋体;">修正程序如下所示：<span lang="EN-US"><o:p></o:p></span></span></span></p>


<p><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; color: rgb(51, 51, 51);">　</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">#include </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 8.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; color: rgb(51, 51, 51);">　　</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">int *pPointer; </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 8.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; color: rgb(51, 51, 51);">　　</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">void
SomeFunction() </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 8.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; color: rgb(51, 51, 51);">　　</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">{ </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 8.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; color: rgb(51, 51, 51);">　　</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US"> // </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; color: rgb(51, 51, 51);">使</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">pPointer</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; color: rgb(51, 51, 51);">指向一个</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">new</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; color: rgb(51, 51, 51);">的整数</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);"> </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 8.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; color: rgb(51, 51, 51);">　　</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US"> pPointer = new
int; </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 8.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; color: rgb(51, 51, 51);">　　</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US"> *pPointer = 25;
</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 8.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; color: rgb(51, 51, 51);">　　</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">} </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 8.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p style="text-indent: 18pt;"><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">void main() <br>
</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; color: rgb(51, 51, 51);">　　</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">{ <br>
</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; color: rgb(51, 51, 51);">　　　</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">SomeFunction();
// </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; color: rgb(51, 51, 51);">让</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">pPointer</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; color: rgb(51, 51, 51);">指向某些东西</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US"> <br>
</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; color: rgb(51, 51, 51);">　　　</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">printf("Value
of *pPointer: %d\n", *pPointer); <o:p></o:p></span></span></p>


<p style="text-indent: 18pt;"><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">delete pPointer;<br>
</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; color: rgb(51, 51, 51);">　　</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">}&nbsp; <o:p></o:p></span></span></p>


<p style="text-indent: 18pt;"><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">如果你不将内存</span></span><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">delete</span></span><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">掉，你的程序就会得到一个</span></span><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">“</span></span><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">内存泄漏</span></span><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">”</span></span><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">。如果出现了内存泄漏，那么除非你关闭应用程序，否则你将无法重新使用这段泄漏的内存。</span></span><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p style="margin-left: 18pt; text-indent: -18pt;"><span style=""><!--[if !supportLists]--><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US"><span style="">2.<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;">&nbsp;&nbsp;&nbsp; </span></span></span><!--[endif]--><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">当一个指针指向类的时候不能用“</span></span><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">.</span></span><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">”来指向其中的变量而要用“</span></span><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">-&gt;</span></span><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">”</span></span><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">,</span></span><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">因为身并不包含一个变量；它指向的结构才包含这个</span></span><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">m_Number</span></span><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">。</span></span><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US"><o:p>&nbsp;</o:p></span></span></p>


<p style="margin-left: 18pt; text-indent: -18pt;"><span style=""><!--[if !supportLists]--><span style="font-size: 10.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><span style="">3.<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span></span><!--[endif]--><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">int *pArray; </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; color: rgb(51, 51, 51);">　　</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">int MyArray[6]; </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 8.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; color: rgb(51, 51, 51);">　　</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">pArray =
&amp;MyArray[0];&nbsp; </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 8.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p style="text-indent: 16.5pt;"><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">请注意，你可以只写</span></span><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">MyArray</span></span><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">来代替</span></span><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">&amp;MyArray[0]</span></span><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">。当然，这种方法只适用于数组，是</span></span><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">C/C++</span></span><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">语言的实现使然。通常出现的错误是写成了</span></span><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">“pArray
= &amp;MyArray;”</span></span><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">，这是不正确的。如果你这么写了，你会获得一个指向数组指针的指针，这种写法得到的并不是想要的东西。</span></span><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="background: yellow none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: red;" lang="EN-US">4</span></span><span style=""><span style="background: yellow none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; color: red;">．</span></span><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">如果你用</span></span><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">new</span></span><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">为一个数组分配空间的话，就像下面这个样子：</span></span><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);">
</span></span><span style=""><span style="font-size: 10.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">　　</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">int *pArray; </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">　　</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">pArray = new int[6];&nbsp; </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">　　那么必须这样释放它：</span></span><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);"> </span></span><span style=""><span style="font-size: 10.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p style="text-indent: 21pt;"><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">delete[]
pArray;&nbsp; <o:p></o:p></span></span></p>


<p style="text-indent: 21pt;"><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">请注意</span></span><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">delete</span></span><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">之后的</span></span><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">[]</span></span><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">。这告知编译器它正在删除一个整个的数组，而不是单独的一个项目。必须在使用数组的时候使用这种方法，否则可能会获得一个内存泄漏。</span></span><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">5</span></span><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">．不能</span></span><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">delete</span></span><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">掉那些没有用</span></span><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">new</span></span><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">分配的内存，像下面这个样子：</span></span><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);"> </span></span><span style=""><span style="font-size: 10.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">　　</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">void main() </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; color: rgb(51, 51, 51);">　　</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">{ </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; color: rgb(51, 51, 51);">　　　</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">int
number; </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; color: rgb(51, 51, 51);">　　　</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">int
*pNumber = number; </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; color: rgb(51, 51, 51);">　　　</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">delete
pNumber; // </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; color: rgb(51, 51, 51);">错误：</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">*pNumber</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; color: rgb(51, 51, 51);">不是用</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">new</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; color: rgb(51, 51, 51);">分配的</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);"> </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; color: rgb(51, 51, 51);">　　</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">} </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p>&nbsp;</o:p></span></span></p>


<p><span style=""><span style="font-size: 10.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US">6</span></span><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">．</span></span><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">使用</span></span><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">free</span></span><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">来释放由</span></span><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">malloc</span></span><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">分配的内存，用</span></span><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">delete</span></span><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">来释放由</span></span><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">new</span></span><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">分配的内存。</span></span><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">7</span></span><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">．除了在声明中以外（例如</span></span><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">int
*number</span></span><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">），星号应该读作</span></span><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">“<span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial;">the memory location pointed to by</span></span></span><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">（由</span></span><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">……</span></span><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">指向的内存位置）</span></span><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">”</span></span><span style=""><span style="font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">“&amp;”</span></span><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">读作</span></span><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">“<span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial;">the
address of</span></span></span><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">（</span></span><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">……</span></span><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">的地址）</span></span><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">”</span></span><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">，在声明的时候例外。在声明的这种情况下，它应该读作</span></span><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">“<span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial;">a reference to</span></span></span><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">（</span></span><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">……</span></span><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">的引用）</span></span><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">”<o:p></o:p></span></span></p>


<p><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">如下：</span></span><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);"> </span></span><span style=""><span style="font-size: 10.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">　　</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">int&amp; Number = myOtherNumber; </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; color: rgb(51, 51, 51);">　　</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">Number =
25;&nbsp; </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">　　引用就像是</span></span><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">myOtherNumber</span></span><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">的指针一样，只不过它是自动解析地址的，所以它的行为就像是指针指向的实际值一样。与其等价的指针代码如下：</span></span><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);"> </span></span><span style=""><span style="font-size: 10.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">　<span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial;">　</span></span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">int* pNumber = &amp;myOtherNumber; </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; color: rgb(51, 51, 51);">　　</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">*pNumber =
25;&nbsp; </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="background: yellow none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US">8</span></span><span style=""><span style="background: yellow none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; color: rgb(51, 51, 51);">．</span></span><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">指针和引用的另一个不同就是你不能更换引用的内容，也就是说你在声明之后就不能更换引用指向的内容了。例如，下面的代码会输出</span></span><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">20</span></span><span style=""><span style="background: red none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; color: rgb(51, 51, 51);">：？？？？？？？？？？？？？？？？</span></span><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);"> </span></span><span style=""><span style="font-size: 10.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">　<span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial;">　</span></span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">int myFirstNumber = 25; </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; color: rgb(51, 51, 51);">　　</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">int
mySecondNumber = 20; </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; color: rgb(51, 51, 51);">　　</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">int
&amp;myReference = myFirstNumber; </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; color: rgb(51, 51, 51);">　　</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">myReference = mySecondNumber;
</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; color: rgb(51, 51, 51);">　　</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">printf("%d",
myFristNumber);&nbsp;</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 10.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="font-size: 10.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p>&nbsp;</o:p></span></span></p>


<p><span style=""><span style="font-size: 8.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><br>
9</span></span><span style=""><span style="font-size: 8.5pt; color: rgb(51, 51, 51);">。</span></span><span style=""><span style="font-size: 10.5pt; color: rgb(51, 51, 51);">当在类中的时候，引用的值必须由构造函数设置，像下面这种方法一样：</span></span><span style=""><span style="font-size: 10.5pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);"> </span></span><span style=""><span style="font-size: 10.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="font-size: 9pt; color: rgb(51, 51, 51);">　<span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial;">　</span></span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">CMyClass::CMyClass(int &amp;variable) :
m_MyReferenceInCMyClass(variable) </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 8.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; color: rgb(51, 51, 51);">　　</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">{ </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 8.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; color: rgb(51, 51, 51);">　　　</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">//
</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; color: rgb(51, 51, 51);">这里是构造代码</span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);"> </span></span><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 8.5pt; font-family: Arial; color: rgb(51, 51, 51);" lang="EN-US"><o:p></o:p></span></span></p>


<p style="text-indent: 18pt;"><span style=""><span style="background: rgb(217, 217, 217) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; font-size: 9pt; font-family: &quot;Courier New&quot;; color: rgb(51, 51, 51);" lang="EN-US">} <o:p></o:p></span></span></p>]]></description>
</item>
</channel>
</rss>