﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-Sunshine Alike-随笔分类-C++</title><link>http://www.cppblog.com/sunshinealike/category/9771.html</link><description>半完美主义</description><language>zh-cn</language><lastBuildDate>Wed, 07 Jul 2010 17:20:01 GMT</lastBuildDate><pubDate>Wed, 07 Jul 2010 17:20:01 GMT</pubDate><ttl>60</ttl><item><title>Windows动态链接库DLL[实践]</title><link>http://www.cppblog.com/sunshinealike/archive/2010/07/08/119658.html</link><dc:creator>Sunshine Alike</dc:creator><author>Sunshine Alike</author><pubDate>Wed, 07 Jul 2010 16:24:00 GMT</pubDate><guid>http://www.cppblog.com/sunshinealike/archive/2010/07/08/119658.html</guid><wfw:comment>http://www.cppblog.com/sunshinealike/comments/119658.html</wfw:comment><comments>http://www.cppblog.com/sunshinealike/archive/2010/07/08/119658.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sunshinealike/comments/commentRss/119658.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunshinealike/services/trackbacks/119658.html</trackback:ping><description><![CDATA[一直对动态链接库这个东西很陌生，今天刚好调试别人写的一个DLL项目，顺便就来研究一下下。<br />动态链接库的原理，好处那些就先不说了，直接来看怎么使用。<br />首先写一个动态链接库，其中<span style="color: rgb(0, 0, 0);"> __declspec(dllexport)是用于MS编译器的一个关键字，用来将DLL中的内容导出而不需要.def文件了。<br />下面的cpp里就函数的定义了，DllMain函数跟普通的win32 console的main函数很像。<br />使用cl /c DLLImplement.cpp<br />link /dll DLLImplement.obj<br />就可以得到一个DLLImplement.dll顺便也会导出一个</span><span style="color: rgb(0, 0, 0);">DLLImplement.</span><span style="color: rgb(0, 0, 0);">lib这个跟静态链接库很像的东西，实际上它的作用只是导出那些方法和变量的名字。<br /><br /></span><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 128);">1</span> <span style="color: rgb(0, 0, 255);">extern</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">C</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">2</span> <span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);">3</span> <span style="color: rgb(0, 0, 0);">    __declspec(dllexport) </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> Set(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i);<br /></span><span style="color: rgb(0, 128, 128);">4</span> <span style="color: rgb(0, 0, 0);">    __declspec(dllexport) </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> Get();<br /></span><span style="color: rgb(0, 128, 128);">5</span> <span style="color: rgb(0, 0, 0);">    __declspec(dllexport) </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> SayHello();<br /></span><span style="color: rgb(0, 128, 128);">6</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">7</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> value;<br /></span><span style="color: rgb(0, 128, 128);">8</span> <span style="color: rgb(0, 0, 0);">}</span></div><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 128);"> 1</span> <span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">iostream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 2</span> <span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">objbase.h</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 3</span> <span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">DLLHeader.h</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 4</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 5</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 6</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> Set(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i)<br /></span><span style="color: rgb(0, 128, 128);"> 7</span> <span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);"> 8</span> <span style="color: rgb(0, 0, 0);">    value </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> i;<br /></span><span style="color: rgb(0, 128, 128);"> 9</span> <span style="color: rgb(0, 0, 0);">}<br /></span><span style="color: rgb(0, 128, 128);">10</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> Get()<br /></span><span style="color: rgb(0, 128, 128);">11</span> <span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);">12</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> value;<br /></span><span style="color: rgb(0, 128, 128);">13</span> <span style="color: rgb(0, 0, 0);">}<br /></span><span style="color: rgb(0, 128, 128);">14</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> SayHello()<br /></span><span style="color: rgb(0, 128, 128);">15</span> <span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);">16</span> <span style="color: rgb(0, 0, 0);">    std::cout</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Hello, I'm DLL, value is </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">value</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">std::endl;<br /></span><span style="color: rgb(0, 128, 128);">17</span> <span style="color: rgb(0, 0, 0);">}<br /></span><span style="color: rgb(0, 128, 128);">18</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">19</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">20</span> <span style="color: rgb(0, 0, 0);">BOOL APIENTRY DllMain(HANDLE hModule, DWORD dwReason, </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> lpReserved)<br /></span><span style="color: rgb(0, 128, 128);">21</span> <span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);">22</span> <span style="color: rgb(0, 0, 0);">    HANDLE g_hModule;<br /></span><span style="color: rgb(0, 128, 128);">23</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">24</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">switch</span><span style="color: rgb(0, 0, 0);">(dwReason)<br /></span><span style="color: rgb(0, 128, 128);">25</span> <span style="color: rgb(0, 0, 0);">    {<br /></span><span style="color: rgb(0, 128, 128);">26</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">case</span><span style="color: rgb(0, 0, 0);"> DLL_PROCESS_ATTACH:<br /></span><span style="color: rgb(0, 128, 128);">27</span> <span style="color: rgb(0, 0, 0);">       std::cout</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">DLL is attached!</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">std::endl;<br /></span><span style="color: rgb(0, 128, 128);">28</span> <span style="color: rgb(0, 0, 0);">       g_hModule </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (HINSTANCE)hModule;<br /></span><span style="color: rgb(0, 128, 128);">29</span> <span style="color: rgb(0, 0, 0);">       </span><span style="color: rgb(0, 0, 255);">break</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">30</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">31</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">case</span><span style="color: rgb(0, 0, 0);"> DLL_PROCESS_DETACH:<br /></span><span style="color: rgb(0, 128, 128);">32</span> <span style="color: rgb(0, 0, 0);">       std::cout</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">DLL is detached!</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">std::endl;<br /></span><span style="color: rgb(0, 128, 128);">33</span> <span style="color: rgb(0, 0, 0);">       g_hModule</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">NULL;<br /></span><span style="color: rgb(0, 128, 128);">34</span> <span style="color: rgb(0, 0, 0);">       </span><span style="color: rgb(0, 0, 255);">break</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">35</span> <span style="color: rgb(0, 0, 0);">    }<br /></span><span style="color: rgb(0, 128, 128);">36</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">37</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> TRUE;<br /></span><span style="color: rgb(0, 128, 128);">38</span> <span style="color: rgb(0, 0, 0);">}<br /></span><span style="color: rgb(0, 128, 128);">39</span> <span style="color: rgb(0, 0, 0);"></span></div><br /><br />使用DLL的方法，有3种：使用def文件/使用LoadLibrary和GetProcAddress方法/使用导出的lib配合头文件。<br />第一种太麻烦这里不介绍，只说明一下后两种的使用方法。<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 128);"> 1</span> <span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">iostream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 2</span> <span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">windows.h</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 3</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 4</span> <span style="color: rgb(0, 0, 0);">typedef </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">SET)(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);"> 5</span> <span style="color: rgb(0, 0, 0);">typedef </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">GET)(</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);"> 6</span> <span style="color: rgb(0, 0, 0);">typedef </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">SAYHELLO)(</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);"> 7</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 8</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> main(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> argc, </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">**</span><span style="color: rgb(0, 0, 0);">argv)<br /></span><span style="color: rgb(0, 128, 128);"> 9</span> <span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);">10</span> <span style="color: rgb(0, 0, 0);">    SET pSet </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">11</span> <span style="color: rgb(0, 0, 0);">    GET pGet </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">12</span> <span style="color: rgb(0, 0, 0);">    SAYHELLO pSayHello </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">13</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">14</span> <span style="color: rgb(0, 0, 0);">    HINSTANCE hinst</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">::LoadLibrary(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">DLLImplement.dll</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">15</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> hinst)<br /></span><span style="color: rgb(0, 128, 128);">16</span> <span style="color: rgb(0, 0, 0);">    {<br /></span><span style="color: rgb(0, 128, 128);">17</span> <span style="color: rgb(0, 0, 0);">        std::cout</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">load dll failed!</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">std::endl;<br /></span><span style="color: rgb(0, 128, 128);">18</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">19</span> <span style="color: rgb(0, 0, 0);">    }<br /></span><span style="color: rgb(0, 128, 128);">20</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">21</span> <span style="color: rgb(0, 0, 0);">    pSet </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (SET)GetProcAddress(hinst, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Set</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">22</span> <span style="color: rgb(0, 0, 0);">    pGet </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (GET)GetProcAddress(hinst, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Get</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">23</span> <span style="color: rgb(0, 0, 0);">    pSayHello </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (SAYHELLO)GetProcAddress(hinst, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">SayHello</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">24</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">25</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> pSet </span><span style="color: rgb(0, 0, 0);">||</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> pGet </span><span style="color: rgb(0, 0, 0);">||</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> pSayHello)<br /></span><span style="color: rgb(0, 128, 128);">26</span> <span style="color: rgb(0, 0, 0);">    {<br /></span><span style="color: rgb(0, 128, 128);">27</span> <span style="color: rgb(0, 0, 0);">        std::cout</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">get function failed!</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">std::endl;<br /></span><span style="color: rgb(0, 128, 128);">28</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">29</span> <span style="color: rgb(0, 0, 0);">    }<br /></span><span style="color: rgb(0, 128, 128);">30</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">31</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">ready to call the function</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);">32</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);">    (</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">pSet)(</span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">33</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">pGet)();<br /></span><span style="color: rgb(0, 128, 128);">34</span> <span style="color: rgb(0, 0, 0);">    std::cout</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Get value from dll, value is </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">i</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">std::endl;<br /></span><span style="color: rgb(0, 128, 128);">35</span> <span style="color: rgb(0, 0, 0);">    (</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">pSayHello)();<br /></span><span style="color: rgb(0, 128, 128);">36</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">37</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">38</span> <span style="color: rgb(0, 0, 0);">}</span></div>这种调用方法属于显示调用，使用的时候需要知道DLL文件的路径，还有DLL里面的方法的名字，还是有点不太方便。更多的时候，都是使用最后一种隐式调用的方法，即配合头文件和导出的lib，需要在进行链接的时候加入DLLImplement.lib即可，方法如下：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 128);"> 1</span> <span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">iostream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 2</span> <span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">DLLHeader.h</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 3</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 4</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">namespace</span><span style="color: rgb(0, 0, 0);"> std;<br /></span><span style="color: rgb(0, 128, 128);"> 5</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 6</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 7</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> main (</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> argc, </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">**</span><span style="color: rgb(0, 0, 0);">argv)<br /></span><span style="color: rgb(0, 128, 128);"> 8</span> <span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);"> 9</span> <span style="color: rgb(0, 0, 0);">    Set(</span><span style="color: rgb(0, 0, 0);">100</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">10</span> <span style="color: rgb(0, 0, 0);">    cout</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Set value in DLL</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">endl;<br /></span><span style="color: rgb(0, 128, 128);">11</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> Get();<br /></span><span style="color: rgb(0, 128, 128);">12</span> <span style="color: rgb(0, 0, 0);">    cout</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Get value in DLL, value is </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">i</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">endl;<br /></span><span style="color: rgb(0, 128, 128);">13</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">14</span> <span style="color: rgb(0, 0, 0);">    SayHello();<br /></span><span style="color: rgb(0, 128, 128);">15</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">16</span> <span style="color: rgb(0, 0, 0);">}</span></div>到这里对DLL有了一个大概的感性认识，具体的理论性的知识，等有机会慢慢再写出来。<br /><br />抱怨一句，最近有点懒了，想要写的日志都没写。<br />找实习也麻烦，写代码肩膀脖子酸痛。<br />IT民工的命咋就这么苦哩。<br /><img src ="http://www.cppblog.com/sunshinealike/aggbug/119658.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunshinealike/" target="_blank">Sunshine Alike</a> 2010-07-08 00:24 <a href="http://www.cppblog.com/sunshinealike/archive/2010/07/08/119658.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Linux Note</title><link>http://www.cppblog.com/sunshinealike/archive/2010/03/14/109696.html</link><dc:creator>Sunshine Alike</dc:creator><author>Sunshine Alike</author><pubDate>Sun, 14 Mar 2010 13:06:00 GMT</pubDate><guid>http://www.cppblog.com/sunshinealike/archive/2010/03/14/109696.html</guid><wfw:comment>http://www.cppblog.com/sunshinealike/comments/109696.html</wfw:comment><comments>http://www.cppblog.com/sunshinealike/archive/2010/03/14/109696.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sunshinealike/comments/commentRss/109696.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunshinealike/services/trackbacks/109696.html</trackback:ping><description><![CDATA[最近才开始接触linux系统（Ubuntu 9.10），有些东西容易忘记，备忘之，待续。<br /><br />1.g++编译问题<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">main.cpp</span><span style="color: rgb(0, 128, 0);"></span><br /><span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">iostream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">namespace</span><span style="color: rgb(0, 0, 0);"> std;<br /><br /></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">headers</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> show();<br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> main()<br />{</span><span style="color: rgb(0, 0, 0);"><br />    cout</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">library test function show(): </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;<br />    show();<br />    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />}</span></div><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">libtest.cpp</span><span style="color: rgb(0, 0, 0);"></span><br /><span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">iostream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">namespace</span><span style="color: rgb(0, 0, 0);"> std;<br /><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> show()<br />{<br />    cout</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">hello library test!</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">endl;<br />}</span></div><br /><b>静态库</b>：<br />首先编译libtest.cpp生成中间文件 <b>libtest.o</b><br />g++ -c libtest.cpp<br />然后将其打包成静态库 <b>staticlib.a<br /></b>ar -rc staticlib.a libtest.o<br />最后使用静态库与main.cpp进行编译得到执行文件<b>staticlib<br /></b>g++ -o staticlib main.cpp staticlib.a<br />运行./staticlib 成功！！<br /><br /><b>动态库：<br /></b>首先同样需要编译生成中间文件<b>libteset.o</b>，但一般加上-fPIC选项。<br />其意思是位置独立代码 ，指示编译程序生成的代码要适合共享库的内容这样的代码能够根据载入内存的位置计算内部地址<br />g++ -c -fPIC libtest.cpp<br />然后打包 生成动态库<b>dynamiclib.so</b><br />g++ -shared libtest.o -o dynamiclib.so<br />最后生成可执行文件dynamiclib<br />g++ -o dynamic main.cpp dynamiclib.so<br />运行./dynamiclib <b>结果出错了，提示找不到动态库。</b><br />原因是linux下动态库不像windows里那样会自动寻找当前目录下的dll，这需要把编译出来的动态库放到系统的 /usr/lib 或 /lib下才能运行程序。<br />或者还可以在编译的时候就指定路径也可以，例如在最后一步的时候：<br />g++ -o dynamic main.cpp <b>./dynamiclib.so<br /></b>再次运行./dynamiclib 成功！！<br /><img src ="http://www.cppblog.com/sunshinealike/aggbug/109696.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunshinealike/" target="_blank">Sunshine Alike</a> 2010-03-14 21:06 <a href="http://www.cppblog.com/sunshinealike/archive/2010/03/14/109696.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>sizeof终极无惑 (转)</title><link>http://www.cppblog.com/sunshinealike/archive/2009/03/27/78046.html</link><dc:creator>Sunshine Alike</dc:creator><author>Sunshine Alike</author><pubDate>Fri, 27 Mar 2009 08:53:00 GMT</pubDate><guid>http://www.cppblog.com/sunshinealike/archive/2009/03/27/78046.html</guid><wfw:comment>http://www.cppblog.com/sunshinealike/comments/78046.html</wfw:comment><comments>http://www.cppblog.com/sunshinealike/archive/2009/03/27/78046.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sunshinealike/comments/commentRss/78046.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunshinealike/services/trackbacks/78046.html</trackback:ping><description><![CDATA[
		<p>
				<font size="2">转自：<a href="/andxie99/archive/2006/10/26/14230.html">http://www.cppblog.com/andxie99/archive/2006/10/26/14230.html</a><br /><br />1. 定义：<br />sizeof是C/C++中的一个操作符（operator），简单的说其作用就是返回一个对象或者类型所占的内存字节数。<br />MSDN上的解释为：<br />The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type (including aggregate types). <br />This keyword returns a value of type size_t.<br />其返回值类型为size_t，在头文件stddef.h中定义。这是一个依赖于编译系统的值，一般定义为<br />typedef unsigned int size_t;<br />世上编译器林林总总，但作为一个规范，它们都会保证char、signed char和unsigned char的sizeof值为1，毕竟char是我们编程能用的最小数据类型。 </font>
		</p>
		<p>
				<font size="2">
				</font> </p>
		<p>
				<font size="2">2. 语法：<br />sizeof有三种语法形式，如下：<br />1) sizeof( object ); // sizeof( 对象 );<br />2) sizeof( type_name ); // sizeof( 类型 );<br />3) sizeof object; // sizeof 对象;<br />所以，<br />int i;<br />sizeof( i ); // ok<br />sizeof i; // ok<br />sizeof( int ); // ok<br />sizeof int; // error<br />既然写法3可以用写法1代替，为求形式统一以及减少我们大脑的负担，第3种写法，忘掉它吧！<br />实际上，sizeof计算对象的大小也是转换成对对象类型的计算，也就是说，同种类型的不同对象其sizeof值都是一致的。这里，对象可以进一步延伸至表达式，即sizeof可以对一个表达式求值，编译器根据表达式的最终结果类型来确定大小，一般不会对表达式进行计算。如：<br />sizeof( 2 );// 2的类型为int，所以等价于 sizeof( int );<br />sizeof( 2 + 3.14 ); // 3.14的类型为double，2也会被提升成double类型，所以等价于 sizeof( double );<br />sizeof也可以对一个函数调用求值，其结果是函数返回类型的大小，函数并不会被调用，我们来看一个完整的例子：<br />char foo()<br />{<br /> printf("foo() has been called.\n");<br />    return 'a';<br />}<br />int main()<br />{<br />size_t sz = sizeof( foo() ); // foo() 的返回值类型为char，所以sz = sizeof( char )，foo()并不会被调用<br />printf("sizeof( foo() ) = %d\n", sz); <br />}<br />C99标准规定，函数、不能确定类型的表达式以及位域（bit-field）成员不能被计算sizeof值，即下面这些写法都是错误的：<br />sizeof( foo );// error</font>
		</p>
		<p>
				<font size="2">void foo2() { }<br />sizeof( foo2() );// error</font>
		</p>
		<p>
				<font size="2">struct S<br />{<br />unsigned int f1 : 1;<br />unsigned int f2 : 5;<br />unsigned int f3 : 12;<br />};<br />sizeof( S.f1 );// error</font>
		</p>
		<p>
				<br />
				<font size="2">3. sizeof的常量性<br />sizeof的计算发生在编译时刻，所以它可以被当作常量表达式使用，如：<br />char ary[ sizeof( int ) * 10 ]; // ok<br />最新的C99标准规定sizeof也可以在运行时刻进行计算，如下面的程序在Dev-C++中可以正确执行：<br />int n;<br />n = 10; // n动态赋值<br />char ary[n]; // C99也支持数组的动态定义<br />printf("%d\n", sizeof(ary)); // ok. 输出10<br />但在没有完全实现C99标准的编译器中就行不通了，上面的代码在VC6中就通不过编译。所以我们最好还是认为sizeof是在编译期执行的，这样不会带来错误，让程序的可移植性强些。</font>
		</p>
		<p>
				<br />
				<font size="2">4. 基本数据类型的sizeof<br />这里的基本数据类型指short、int、long、float、double这样的简单内置数据类型，由于它们都是和系统相关的，所以在不同的系统下取值可能不同，这务必引起我们的注意，尽量不要在这方面给自己程序的移植造成麻烦。<br />一般的，在32位编译环境中，sizeof(int)的取值为4。</font>
		</p>
		<p>
				<br />
				<font size="2">5. 指针变量的sizeof<br />学过数据结构的你应该知道指针是一个很重要的概念，它记录了另一个对象的地址。既然是来存放地址的，那么它当然等于计算机内部地址总线的宽度。所以在32位计算机中，一个指针变量的返回值必定是4（注意结果是以字节为单位），可以预计，在将来的64位系统中指针变量的sizeof结果为8。<br />char* pc = "abc";<br />int* pi;<br />string* ps;<br />char** ppc = &amp;pc;<br />void (*pf)();// 函数指针<br />sizeof( pc ); // 结果为4<br />sizeof( pi ); // 结果为4<br />sizeof( ps ); // 结果为4<br />sizeof( ppc ); // 结果为4<br />sizeof( pf );// 结果为4<br />指针变量的sizeof值与指针所指的对象没有任何关系，正是由于所有的指针变量所占内存大小相等，所以MFC消息处理函数使用两个参数WPARAM、LPARAM就能传递各种复杂的消息结构（使用指向结构体的指针）。</font>
		</p>
		<p>
				<br />
				<font size="2">6. 数组的sizeof<br />数组的sizeof值等于数组所占用的内存字节数，如：<br />char a1[] = "abc";<br />int a2[3];<br />sizeof( a1 ); // 结果为4，字符串末尾还存在一个NULL终止符<br />sizeof( a2 ); // 结果为3*4=12（依赖于int）<br />一些朋友刚开始时把sizeof当作了求数组元素的个数，现在，你应该知道这是不对的，那么应该怎么求数组元素的个数呢？Easy，通常有下面两种写法：<br />int c1 = sizeof( a1 ) / sizeof( char ); // 总长度/单个元素的长度<br />int c2 = sizeof( a1 ) / sizeof( a1[0] ); // 总长度/第一个元素的长度<br />写到这里，提一问，下面的c3，c4值应该是多少呢？<br />void foo3(char a3[3])<br />{<br />int c3 = sizeof( a3 ); // c3 == ?<br />}<br />void foo4(char a4[])<br />{<br />int c4 = sizeof( a4 ); // c4 == ?<br />}也许当你试图回答c4的值时已经意识到c3答错了，是的，c3!=3。这里函数参数a3已不再是数组类型，而是蜕变成指针，相当于char* a3，为什么？仔细想想就不难明白，我们调用函数foo1时，程序会在栈上分配一个大小为3的数组吗？不会！数组是“传址”的，调用者只需将实参的地址传递过去，所以a3自然为指针类型（char*），c3的值也就为4。</font>
		</p>
		<p>
				<font size="2">7. 结构体的sizeof<br />这是初学者问得最多的一个问题，所以这里有必要多费点笔墨。让我们先看一个结构体：<br />struct S1<br />{<br />char c;<br />int i;<br />};<br />问sizeof(s1)等于多少？聪明的你开始思考了，char占1个字节，int占4个字节，那么加起来就应该是5。是这样吗？你在你机器上试过了吗？也许你是对的，但很可能你是错的！VC6中按默认设置得到的结果为8。<br />Why？为什么受伤的总是我？<br />请不要沮丧，我们来好好琢磨一下sizeof的定义——sizeof的结果等于对象或者类型所占的内存字节数，好吧，那就让我们来看看S1的内存分配情况：<br />S1 s1 = { 'a', 0xFFFFFFFF };<br />定义上面的变量后，加上断点，运行程序，观察s1所在的内存，你发现了什么？<br />以我的VC6.0为例，s1的地址为0x0012FF78，其数据内容如下：<br />0012FF78:? 61 CC CC CC FF FF FF FF<br />发现了什么？怎么中间夹杂了3个字节的CC？看看MSDN上的说明：<br />When applied to a structure type or variable, sizeof returns the actual size, which may include padding bytes inserted for alignment.<br />原来如此，这就是传说中的字节对齐啊！一个重要的话题出现了。<br />为什么需要字节对齐？计算机组成原理教导我们这样有助于加快计算机的取数速度，否则就得多花指令周期了。为此，编译器默认会对结构体进行处理（实际上其它地方的数据变量也是如此），让宽度为2的基本数据类型（short等）都位于能被2整除的地址上，让宽度为4的基本数据类型（int等）都位于能被4整除的地址上，以此类推。这样，两个数中间就可能需要加入填充字节，所以整个结构体的sizeof值就增长了。<br />让我们交换一下S1中char与int的位置：<br />struct S2<br />{<br />int i;<br />char c;<br />};<br />看看sizeof(S2)的结果为多少，怎么还是8？再看看内存，原来成员c后面仍然有3个填充字节，这又是为什么啊？别着急，下面总结规律。<br /><br />字节对齐的细节和编译器实现相关，但一般而言，满足三个准则：<br />1) 结构体变量的首地址能够被其最宽基本类型成员的大小所整除；<br />2) 结构体每个成员相对于结构体首地址的偏移量（offset）都是成员大小的整数倍，如有需要编译器会在成员之间加上填充字节（internal adding）；<br />3) 结构体的总大小为结构体最宽基本类型成员大小的整数倍，如有需要编译器会在最末一个成员之后加上填充字节（trailing padding）。<br />对于上面的准则，有几点需要说明：<br />1) 前面不是说结构体成员的地址是其大小的整数倍，怎么又说到偏移量了呢？因为有了第1点存在，所以我们就可以只考虑成员的偏移量，这样思考起来简单。想想为什么。<br />结构体某个成员相对于结构体首地址的偏移量可以通过宏offsetof()来获得，这个宏也在stddef.h中定义，如下：<br />#define offsetof(s,m)?? (size_t)&amp;(((s *)0)-&gt;m)<br />例如，想要获得S2中c的偏移量，方法为<br />size_t pos = offsetof(S2, c);// pos等于4<br />2) 基本类型是指前面提到的像char、short、int、float、double这样的内置数据类型，这里所说的“数据宽度”就是指其sizeof的大小。由于结构体的成员可以是复合类型，比如另外一个结构体，所以在寻找最宽基本类型成员时，应当包括复合类型成员的子成员，而不是把复合成员看成是一个整体。但在确定复合类型成员的偏移位置时则是将复合类型作为整体看待。<br />这里叙述起来有点拗口，思考起来也有点挠头，还是让我们看看例子吧（具体数值仍以VC6为例，以后不再说明）：<br />struct S3<br />{<br />char c1;<br />S1 s;<br />char c2<br />};<br />S1的最宽简单成员的类型为int，S3在考虑最宽简单类型成员时是将S1“打散”看的，所以S3的最宽简单类型为int，这样，通过S3定义的变量，其存储空间首地址需要被4整除，整个sizeof(S3)的值也应该被4整除。<br />c1的偏移量为0，s的偏移量呢？这时s是一个整体，它作为结构体变量也满足前面三个准则，所以其大小为8，偏移量为4，c1与s之间便需要3个填充字节，而c2与s之间就不需要了，所以c2的偏移量为12，算上c2的大小为13，13是不能被4整除的，这样末尾还得补上3个填充字节。最后得到sizeof(S3)的值为16。<br />通过上面的叙述，我们可以得到一个公式：<br />结构体的大小等于最后一个成员的偏移量加上其大小再加上末尾的填充字节数目，即：<br />sizeof( struct ) = offsetof( last item ) + sizeof( last item ) + sizeof( trailing padding )<br /><br />到这里，朋友们应该对结构体的sizeof有了一个全新的认识，但不要高兴得太早，有一个影响sizeof的重要参量还未被提及，那便是编译器的pack指令。它是用来调整结构体对齐方式的，不同编译器名称和用法略有不同，VC6中通过#pragma pack实现，也可以直接修改/Zp编译开关。#pragma pack的基本用法为：#pragma pack( n )，n为字节对齐数，其取值为1、2、4、8、16，默认是8，如果这个值比结构体成员的sizeof值小，那么该成员的偏移量应该以此值为准，即是说，结构体成员的偏移量应该取二者的最小值，公式如下：<br />offsetof( item ) = min( n, sizeof( item ) )<br />再看示例：<br />#pragma pack(push) // 将当前pack设置压栈保存<br />#pragma pack(2)// 必须在结构体定义之前使用<br />struct S1<br />{<br />char c;<br />int i;<br />};<br />struct S3<br />{<br />char c1;<br />S1 s;<br />char c2<br />};<br />#pragma pack(pop) // 恢复先前的pack设置<br />计算sizeof(S1)时，min(2, sizeof(i))的值为2，所以i的偏移量为2，加上sizeof(i)等于6，能够被2整除，所以整个S1的大小为6。<br />同样，对于sizeof(S3)，s的偏移量为2，c2的偏移量为8，加上sizeof(c2)等于9，不能被2整除，添加一个填充字节，所以sizeof(S3)等于10。<br />现在，朋友们可以轻松的出一口气了，:)<br /><br />还有一点要注意，“空结构体”（不含数据成员）的大小不为0，而是1。试想一个“不占空间”的变量如何被取地址、两个不同的“空结构体”变量又如何得以区分呢？于是，“空结构体”变量也得被存储，这样编译器也就只能为其分配一个字节的空间用于占位了。如下：<br />struct S5 { };<br />sizeof( S5 ); // 结果为1<br /></font>
		</p>
		<p>
				<font size="2">8. 含位域结构体的sizeof<br />前面已经说过，位域成员不能单独被取sizeof值，我们这里要讨论的是含有位域的结构体的sizeof，只是考虑到其特殊性而将其专门列了出来。<br />C99规定int、unsigned int和bool可以作为位域类型，但编译器几乎都对此作了扩展，允许其它类型类型的存在。<br />使用位域的主要目的是压缩存储，其大致规则为：<br />1) 如果相邻位域字段的类型相同，且其位宽之和小于类型的sizeof大小，则后面的字段将紧邻前一个字段存储，直到不能容纳为止；<br />2) 如果相邻位域字段的类型相同，但其位宽之和大于类型的sizeof大小，则后面的字段将从新的存储单元开始，其偏移量为其类型大小的整数倍；<br />3) 如果相邻的位域字段的类型不同，则各编译器的具体实现有差异，VC6采取不压缩方式，Dev-C++采取压缩方式；<br />4) 如果位域字段之间穿插着非位域字段，则不进行压缩；<br />5) 整个结构体的总大小为最宽基本类型成员大小的整数倍。<br /><br />还是让我们来看看例子。<br />示例1：<br />struct BF1<br />{<br />char f1 : 3;<br />char f2 : 4;<br />char f3 : 5;<br />};<br />其内存布局为：<br />|_f1__|__f2__|_|____f3___|____|<br />|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|<br /><br />位域类型为char，第1个字节仅能容纳下f1和f2，所以f2被压缩到第1个字节中，而f3只能从下一个字节开始。因此sizeof(BF1)的结果为2。<br />示例2：<br />struct BF2<br />{<br />char f1 : 3;<br />short f2 : 4;<br />char f3 : 5;<br />};<br />由于相邻位域类型不同，在VC6中其sizeof为6，在Dev-C++中为2。<br />示例3：<br />struct BF3<br />{<br />char f1 : 3;<br />char f2;<br />char f3 : 5;<br />};<br />非位域字段穿插在其中，不会产生压缩，在VC6和Dev-C++中得到的大小均为3。<br /></font>
		</p>
		<p>
				<font size="2">9. 联合体的sizeof<br />结构体在内存组织上是顺序式的，联合体则是重叠式，各成员共享一段内存，所以整个联合体的sizeof也就是每个成员sizeof的最大值。结构体的成员也可以是复合类型，这里，复合类型成员是被作为整体考虑的。<br />所以，下面例子中，U的sizeof值等于sizeof(s)。<br />union U<br />{<br />int i;<br />char c;<br />S1 s;<br />};</font>
		</p>
<img src ="http://www.cppblog.com/sunshinealike/aggbug/78046.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunshinealike/" target="_blank">Sunshine Alike</a> 2009-03-27 16:53 <a href="http://www.cppblog.com/sunshinealike/archive/2009/03/27/78046.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>