<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>虚幻</title>
	<atom:link href="http://www.sizeof.cn/feed" rel="self" type="application/rss+xml" />
	<link>http://www.sizeof.cn</link>
	<description>Debug Life! Release Life!</description>
	<lastBuildDate>Wed, 17 Aug 2011 15:55:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>IcmpSendEcho2实现Ping</title>
		<link>http://www.sizeof.cn/html/2011/482.html</link>
		<comments>http://www.sizeof.cn/html/2011/482.html#comments</comments>
		<pubDate>Wed, 17 Aug 2011 15:50:13 +0000</pubDate>
		<dc:creator>虚幻</dc:creator>
				<category><![CDATA[网络]]></category>
		<category><![CDATA[icmp]]></category>
		<category><![CDATA[IcmpSendEcho2]]></category>
		<category><![CDATA[ping]]></category>

		<guid isPermaLink="false">http://www.sizeof.cn/?p=482</guid>
		<description><![CDATA[<p style="text-indent: 2em">最近需要实现的功能涉及或者某个网关的Ping值，但是如果使用Boost实现的话，在window 7操作系统下，由于权限的原因，创建raw socket会产生异常。所以，在windows 7操作系统下，只能通过IcmpSendEcho2这个函数来获得Ping值。关于这个函数的用法，可以参考<a href="http://msdn.microsoft.com/en-us/library/aa366051(v=vs.85).aspx">MSDN</a>来查看。</p>]]></description>
			<content:encoded><![CDATA[<p style="text-indent: 2em">最近需要实现的功能涉及或者某个网关的Ping值，但是如果使用Boost实现的话，在window 7操作系统下，由于权限的原因，创建raw socket会产生异常。所以，在windows 7操作系统下，只能通过IcmpSendEcho2这个函数来获得Ping值。关于这个函数的用法，可以参考<a href="http://msdn.microsoft.com/en-us/library/aa366051(v=vs.85).aspx">MSDN</a>来查看。</p>
<p><span id="more-482"></span></p>
<p style="text-indent: 2em">下面是一些开发中遇到的一些问题的总结:</p>
<p style="text-indent: 2em">1. SendData必须大于12字节，否则就会遇到IP_BUF_TOO_SMALL错误。</p>
<p style="text-indent: 2em">2. 设置TTL可以采用以下代码，然后把ip_iption传入就可以，如果不限定TTL的话，设置255；如果限定TTL的话，调用的结果是IP_TTL_EXPIRED_TRANSIT，这个是正确的。</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">IP_OPTION_INFORMATION ip_option<span style="color: #008080;">;</span>
<span style="color: #0000dd;">memset</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>ip_option, <span style="color: #0000dd;">0</span>, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>ip_option<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
ip_option.<span style="color: #007788;">Ttl</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">255</span><span style="color: #008080;">;</span></pre></div></div>

<p style="text-indent: 2em">3. 这个函数是可以有异步方式的，如果使用同步的话，网络不好就会阻塞，所以在调用这个API的时候，可以新建一个线程来调用。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sizeof.cn/html/2011/482.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CRC32校验算法以及余式表生成</title>
		<link>http://www.sizeof.cn/html/2011/476.html</link>
		<comments>http://www.sizeof.cn/html/2011/476.html#comments</comments>
		<pubDate>Wed, 09 Mar 2011 13:12:42 +0000</pubDate>
		<dc:creator>虚幻</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[crc32]]></category>
		<category><![CDATA[余式表]]></category>
		<category><![CDATA[校验算法]]></category>

		<guid isPermaLink="false">http://www.sizeof.cn/?p=476</guid>
		<description><![CDATA[CRC32算法，其中包含生成CRC32的余式表。]]></description>
			<content:encoded><![CDATA[<p style="text-indent: 2em">CRC32算法，其中包含生成CRC32的余式表。代码很简单，如下。</p>
<p><span id="more-476"></span></p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;stdio.h&gt;</span>
<span style="color: #339900;">#include &lt;stdlib.h&gt;</span>
<span style="color: #339900;">#include &lt;memory.h&gt;</span>
&nbsp;
<span style="color: #339900;">#define POLY 0x04C11DB7L</span>
&nbsp;
<span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> get_sum_poly<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">char</span> top_byte <span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">/// sum all the polys at various offsets </span>
    <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> sum_poly <span style="color: #000080;">=</span> top_byte <span style="color: #000080;">&lt;&lt;</span> <span style="color: #0000dd;">24</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">int</span> j <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> j <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">8</span><span style="color: #008080;">;</span> <span style="color: #000040;">++</span> j <span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #666666;">/// check the top bit</span>
        <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> <span style="color: #008000;">&#40;</span> sum_poly <span style="color: #000080;">&gt;&gt;</span> <span style="color: #0000dd;">31</span> <span style="color: #008000;">&#41;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span> <span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #666666;">/// TODO : understand why '&lt;&lt;' first</span>
            sum_poly <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span> sum_poly <span style="color: #000080;">&lt;&lt;</span> <span style="color: #0000dd;">1</span> <span style="color: #008000;">&#41;</span> <span style="color: #000040;">^</span> POLY<span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0000ff;">else</span>
        <span style="color: #008000;">&#123;</span>
            sum_poly <span style="color: #000080;">&lt;&lt;=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">return</span> sum_poly<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> create_table<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> <span style="color: #000040;">*</span>table <span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">256</span><span style="color: #008080;">;</span> <span style="color: #000040;">++</span> i <span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        table<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> get_sum_poly<span style="color: #008000;">&#40;</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">char</span><span style="color: #008000;">&#41;</span> i <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">/// the data </span>
    <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> data <span style="color: #000080;">=</span> <span style="color: #208080;">0x1011035b</span><span style="color: #008080;">;</span>
    <span style="color: #666666;">/// load the register with the data</span>
    <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> regi <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
    <span style="color: #666666;">/// allocate memory to contain the AUGMENTED data (added some zeros)</span>
    <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">char</span> p<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">4</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
    <span style="color: #666666;">/// copy data</span>
    <span style="color: #0000dd;">memcpy</span><span style="color: #008000;">&#40;</span> p, <span style="color: #000040;">&amp;</span>data, <span style="color: #0000dd;">4</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">/// the table</span>
    <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> table<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">256</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
    <span style="color: #666666;">/// create the table</span>
    create_table<span style="color: #008000;">&#40;</span> table <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i<span style="color: #000080;">&lt;</span><span style="color: #0000dd;">256</span><span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>i <span style="color: #000040;">%</span> <span style="color: #0000dd;">8</span> <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;0x%08x, &quot;</span>, table<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #666666;">/// because data contains 4 bytes</span>
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">4</span><span style="color: #008080;">;</span> <span style="color: #000040;">++</span> i <span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        regi <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span> regi <span style="color: #000080;">&lt;&lt;</span> <span style="color: #0000dd;">8</span> <span style="color: #008000;">&#41;</span> <span style="color: #000040;">^</span> table<span style="color: #008000;">&#91;</span> <span style="color: #008000;">&#40;</span> regi <span style="color: #000080;">&gt;&gt;</span> <span style="color: #0000dd;">24</span> <span style="color: #008000;">&#41;</span> <span style="color: #000040;">^</span> p<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #666666;">/// and now, register contains the remainder which is also called CRC value.</span>
    <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>crc32 = %08x<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, regi<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
&nbsp;
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.sizeof.cn/html/2011/476.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>在非控制台程序中打印出printf</title>
		<link>http://www.sizeof.cn/html/2011/472.html</link>
		<comments>http://www.sizeof.cn/html/2011/472.html#comments</comments>
		<pubDate>Tue, 15 Feb 2011 14:49:20 +0000</pubDate>
		<dc:creator>虚幻</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[editbin]]></category>
		<category><![CDATA[printf]]></category>
		<category><![CDATA[非控制台程序]]></category>

		<guid isPermaLink="false">http://www.sizeof.cn/?p=472</guid>
		<description><![CDATA[在windows的程序开发中，如果需要输出调试信息一般可以OutputDebugString，使用DebugView就可以看到打出来的调试信息，不过，如果在一些跨平台的程序就不会用DebugView来调试，因为linux会编译通不过，但是，如果用了printf，在windows下又看到不到。]]></description>
			<content:encoded><![CDATA[<p style="text-indent: 2em">在windows的程序开发中，如果需要输出调试信息一般可以OutputDebugString，使用DebugView就可以看到打出来的调试信息，不过，如果在一些跨平台的程序就不会用DebugView来调试，因为linux会编译通不过，但是，如果用了printf，在windows下又看到不到。</p>
<p style="text-indent: 2em">其实在VS 2005 &#038; 2008里面有个命令叫editbin，进入vs 2005命令行提示，然后输入editbin就可以看到用法，如果对一个a.exe的程序执行以下命令</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">editbin <span style="color: #000040;">/</span>SUBSYSTEM<span style="color: #008080;">:</span>CONSOL <span style="color: #FF0000;">&quot;a.exe&quot;</span></pre></div></div>

<p style="text-indent: 2em">这样，这个a.exe在运行的时候，就会多一个控制台的窗口，里面可以看到所有printf打出来的信息。如果要取消，只需要把SUBSYSTEM改为Windows就可以了。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sizeof.cn/html/2011/472.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>查内存泄露之打印程序对象个数</title>
		<link>http://www.sizeof.cn/html/2010/467.html</link>
		<comments>http://www.sizeof.cn/html/2010/467.html#comments</comments>
		<pubDate>Fri, 01 Oct 2010 02:32:43 +0000</pubDate>
		<dc:creator>虚幻</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[内存泄露]]></category>
		<category><![CDATA[对象个数]]></category>

		<guid isPermaLink="false">http://www.sizeof.cn/?p=467</guid>
		<description><![CDATA[当程序中的对象使用了智能指针后，很有可能因为互相引用导致的内存泄露，这个时候，可以在程序运行过程中打印出每个对象的个数。原理就是自己写一个map，索引是对象的名字，值为个数，每次构造的时候++，每次析构的时候--。]]></description>
			<content:encoded><![CDATA[<p style="text-indent: 2em">当程序中的对象使用了智能指针后，很有可能因为互相引用导致的内存泄露，这个时候，可以在程序运行过程中打印出每个对象的个数。原理就是自己写一个map，索引是对象的名字，值为个数，每次构造的时候++，每次析构的时候&#8211;。大概代码如下：</p>
<p><span id="more-467"></span></p>
<p style="text-indent: 2em"><strong>count_object_allocate.h</strong></p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#ifndef _COUNT_OBJECT_ALLOCATE_H_</span>
<span style="color: #339900;">#define _COUNT_OBJECT_ALLOCATE_H_</span>
&nbsp;
<span style="color: #339900;">#include &lt;map&gt;</span>
<span style="color: #339900;">#include &lt;string&gt;</span>
<span style="color: #339900;">#include &lt;typeinfo&gt;</span>
&nbsp;
<span style="color: #0000ff;">class</span> object_counter
<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
	object_counter<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	~object_counter<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">static</span> object_counter <span style="color: #000040;">*</span> get_counter<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">bool</span> add_object_count<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span> object_name <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">bool</span> delete_object_count<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span> object_name <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">void</span> dump_all_objects<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
	std<span style="color: #008080;">::</span><span style="color: #007788;">map</span><span style="color: #000080;">&lt;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> , <span style="color: #0000ff;">int</span><span style="color: #000080;">&gt;</span> allocated_objects_<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> T<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">class</span> count_object_allocate
<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
	count_object_allocate<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		object_counter<span style="color: #008080;">::</span><span style="color: #007788;">get_counter</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>add_object_count<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">typeid</span><span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span>.<span style="color: #007788;">name</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	~count_object_allocate<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		object_counter<span style="color: #008080;">::</span><span style="color: #007788;">get_counter</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>delete_object_count<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">typeid</span><span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span>.<span style="color: #007788;">name</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
    count_object_allocate<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> count_object_allocate<span style="color: #000080;">&lt;</span>T<span style="color: #000080;">&gt;</span> <span style="color: #000040;">&amp;</span> coa<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        object_counter<span style="color: #008080;">::</span><span style="color: #007788;">get_counter</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>add_object_count<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">typeid</span><span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span>.<span style="color: #007788;">name</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
<span style="color: #339900;">#endif</span></pre></div></div>

<p style="text-indent: 2em"><strong>count_object_allocate.cpp</strong></p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &quot;Common.h&quot;</span>
<span style="color: #339900;">#include &quot;count_object_allocate.h&quot;</span>
&nbsp;
<span style="color: #339900;">#ifdef DUMP_OBJECT</span>
object_counter g_count<span style="color: #008080;">;</span>
<span style="color: #339900;">#endif</span>
&nbsp;
object_counter<span style="color: #008080;">::</span><span style="color: #007788;">object_counter</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
object_counter<span style="color: #008080;">::</span>~object_counter<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #339900;">#ifdef DUMP_OBJECT</span>
object_counter <span style="color: #000040;">*</span> object_counter<span style="color: #008080;">::</span><span style="color: #007788;">get_counter</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">return</span> <span style="color: #000040;">&amp;</span>g_count<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #339900;">#endif</span>
&nbsp;
<span style="color: #0000ff;">bool</span> object_counter<span style="color: #008080;">::</span><span style="color: #007788;">add_object_count</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span> object_name <span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	std<span style="color: #008080;">::</span><span style="color: #007788;">map</span><span style="color: #000080;">&lt;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> , <span style="color: #0000ff;">int</span><span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">iterator</span> iter <span style="color: #000080;">=</span> this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>allocated_objects_.<span style="color: #007788;">find</span><span style="color: #008000;">&#40;</span> object_name <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> iter <span style="color: #000080;">==</span> this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>allocated_objects_.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> 
        <span style="color: #008000;">&#123;</span>
		<span style="color: #666666;">// new</span>
		this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>allocated_objects_.<span style="color: #007788;">insert</span><span style="color: #008000;">&#40;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">make_pair</span><span style="color: #008000;">&#40;</span> object_name , <span style="color: #0000dd;">0</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		iter <span style="color: #000080;">=</span> this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>allocated_objects_.<span style="color: #007788;">find</span><span style="color: #008000;">&#40;</span> object_name <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	iter<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>second<span style="color: #000040;">++</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">bool</span> object_counter<span style="color: #008080;">::</span><span style="color: #007788;">delete_object_count</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span> object_name <span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	std<span style="color: #008080;">::</span><span style="color: #007788;">map</span><span style="color: #000080;">&lt;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> , <span style="color: #0000ff;">int</span><span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">iterator</span> iter <span style="color: #000080;">=</span> this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>allocated_objects_.<span style="color: #007788;">find</span><span style="color: #008000;">&#40;</span> object_name <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> iter <span style="color: #000080;">==</span> this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>allocated_objects_.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> 
        <span style="color: #008000;">&#123;</span>
		<span style="color: #666666;">// new</span>
		<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	iter<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>second<span style="color: #000040;">--</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> object_counter<span style="color: #008080;">::</span><span style="color: #007788;">dump_all_objects</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;begin dump objects:<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">map</span><span style="color: #000080;">&lt;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> , <span style="color: #0000ff;">int</span><span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">iterator</span> i <span style="color: #000080;">=</span> this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>allocated_objects_.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008080;">;</span>
             i <span style="color: #000040;">!</span><span style="color: #000080;">=</span> this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>allocated_objects_.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008080;">;</span> <span style="color: #000040;">++</span>i <span style="color: #008000;">&#41;</span> 
        <span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;%s<span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span>: %d<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> , i<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>first.<span style="color: #007788;">c_str</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> , i<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>second<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;end dump objects:<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p style="text-indent: 2em">在使用的过程中，每个类只需要继承count_object_allocate即可，感谢nightsuns提供的好办法！</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sizeof.cn/html/2010/467.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NSIS脚本IfErrors的陷阱</title>
		<link>http://www.sizeof.cn/html/2010/458.html</link>
		<comments>http://www.sizeof.cn/html/2010/458.html#comments</comments>
		<pubDate>Tue, 17 Aug 2010 14:13:04 +0000</pubDate>
		<dc:creator>虚幻</dc:creator>
				<category><![CDATA[软件相关]]></category>
		<category><![CDATA[IfErrors]]></category>
		<category><![CDATA[NSIS]]></category>

		<guid isPermaLink="false">http://www.sizeof.cn/?p=458</guid>
		<description><![CDATA[<p style="text-indent: 2em">NSIS是一个开源的软件安装包制作工具，使用很简单，脚本的功能也比较强大。不过在最近的使用中发现了一个关于IfErrors的问题，其实脚本本身并没有什么问题，只是自己对脚本不了解才导致查了很久才解决遇到的这个BUG。</p>]]></description>
			<content:encoded><![CDATA[<p style="text-indent: 2em">NSIS是一个开源的软件安装包制作工具，使用很简单，脚本的功能也比较强大。不过在最近的使用中发现了一个关于IfErrors的问题，其实脚本本身并没有什么问题，只是自己对脚本不了解才导致查了很久才解决遇到的这个BUG。</p>
<p style="text-indent: 2em">IfErrors 标签1 标签2，如果发生错误则进入标签1执行，否则进入标签2执行。但是我发现我的代码每次都会发生错误，进入标签1，但实际上并没有错，因为我读取到的值是正常的，终于无意中看到ClearErrors，顿时涣然大悟！最后在IfErrors之前需要添加一行ClearErrors，果然解决问题！<br />
]]></content:encoded>
			<wfw:commentRss>http://www.sizeof.cn/html/2010/458.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Google Test初体验</title>
		<link>http://www.sizeof.cn/html/2010/453.html</link>
		<comments>http://www.sizeof.cn/html/2010/453.html#comments</comments>
		<pubDate>Tue, 10 Aug 2010 15:25:57 +0000</pubDate>
		<dc:creator>虚幻</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[google test]]></category>

		<guid isPermaLink="false">http://www.sizeof.cn/?p=453</guid>
		<description><![CDATA[<p style="text-indent: 2em">Google Test是谷歌公司一套开放的可用于C/C++语言的单元测试框架。目前gtest的最新版本是1.5.0。下载gtest之后，首先，需要打开gtest的项目进行编译。</p>]]></description>
			<content:encoded><![CDATA[<div id="attachment_452" class="wp-caption alignnone" style="width: 310px"><a href="http://www.sizeof.cn/wp-content/uploads/2010/08/gtest.jpg"><img src="http://www.sizeof.cn/wp-content/uploads/2010/08/gtest-300x144.jpg" alt="Google Test" title="Google Test" width="300" height="144" class="size-medium wp-image-452" /></a><p class="wp-caption-text">Google Test</p></div>
<p style="text-indent: 2em">Google Test是谷歌公司一套开放的可用于C/C++语言的单元测试框架。目前gtest的最新版本是1.5.0。下载gtest之后，首先，需要打开gtest的项目进行编译。</p>
<p><span id="more-453"></span></p>
<p style="text-indent: 2em">然后，新建一个测试项目，其实，编译选项中的附加包含目录指向“gtest-1.5.0\gtest-1.5.0\include”，链接选项中的附加依赖项指向“gtest-1.5.0\gtest-1.5.0\msvc\gtest\Debug\gtestd.lib”，测试代码如下：</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> Foo<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> a, <span style="color: #0000ff;">int</span> b<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>a <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span> <span style="color: #000040;">||</span> b <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">throw</span> <span style="color: #FF0000;">&quot;don't do that&quot;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0000ff;">int</span> c <span style="color: #000080;">=</span> a <span style="color: #000040;">%</span> b<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>c <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
        <span style="color: #0000ff;">return</span> b<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">return</span> Foo<span style="color: #008000;">&#40;</span>b, c<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #339900;">#include &lt;gtest/gtest.h&gt;</span>
&nbsp;
TEST<span style="color: #008000;">&#40;</span>FooTest, HandleNoneZeroInput<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    EXPECT_EQ<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">2</span>, Foo<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">4</span>, <span style="color: #0000dd;">10</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    EXPECT_EQ<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">6</span>, Foo<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">30</span>, <span style="color: #0000dd;">18</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> argv<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    testing<span style="color: #008080;">::</span><span style="color: #007788;">InitGoogleTest</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>argc, argv<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">return</span> RUN_ALL_TESTS<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.sizeof.cn/html/2010/453.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Boost asio的endpoint.address.to_string弹框报错</title>
		<link>http://www.sizeof.cn/html/2010/447.html</link>
		<comments>http://www.sizeof.cn/html/2010/447.html#comments</comments>
		<pubDate>Mon, 09 Aug 2010 14:38:04 +0000</pubDate>
		<dc:creator>虚幻</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[boost asio]]></category>
		<category><![CDATA[to_string]]></category>
		<category><![CDATA[弹框]]></category>
		<category><![CDATA[报错]]></category>

		<guid isPermaLink="false">http://www.sizeof.cn/?p=447</guid>
		<description><![CDATA[最近遇到一个用户，启动软件就弹框报错，完全无法使用，然后一步一步调试跟踪，终于发现问题。]]></description>
			<content:encoded><![CDATA[<p style="text-indent: 2em">最近遇到一个用户，启动软件就弹框报错，完全无法使用，然后一步一步调试跟踪，终于发现问题，讲出错代码定位在以下这行：</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">string remote_address <span style="color: #000080;">=</span> http_server_socket_<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>GetEndPoint<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>.<span style="color: #007788;">address</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>.<span style="color: #007788;">to_string</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p><span id="more-447"></span></p>
<p style="text-indent: 2em">经过仔细的研究，发现其实是to_string方法报错了，其实也很奇怪，全网那么多用户，偏偏就这个用户会报错，但是这又是很重要的代码，只能那么写。于是研究了一下to_string()方法，发现boost的to_sting()方法还有另外一个带error_code的版本。于是，试用了一下error_code的版本，果然不会报错。解决的代码如下：</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">boost<span style="color: #008080;">::</span><span style="color: #0000dd;">system</span><span style="color: #008080;">::</span><span style="color: #007788;">error_code</span> ec<span style="color: #008080;">;</span>
string remote_address <span style="color: #000080;">=</span> http_server_socket_<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>GetEndPoint<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>.<span style="color: #007788;">address</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>.<span style="color: #007788;">to_string</span><span style="color: #008000;">&#40;</span>ec<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>ec<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// 成功，do something</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p style="text-indent: 2em">因此，为了增强系统的健壮性，使用boost库的时候，尽量使用带error_code版本的函数。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sizeof.cn/html/2010/447.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MFC超链接库以及使用</title>
		<link>http://www.sizeof.cn/html/2010/431.html</link>
		<comments>http://www.sizeof.cn/html/2010/431.html#comments</comments>
		<pubDate>Wed, 30 Jun 2010 15:10:53 +0000</pubDate>
		<dc:creator>虚幻</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[HyperText]]></category>
		<category><![CDATA[MFC]]></category>
		<category><![CDATA[超链接]]></category>

		<guid isPermaLink="false">http://www.sizeof.cn/?p=431</guid>
		<description><![CDATA[<p style="text-indent: 2em">在Windows程序开发时候，界面上经常会做一些超链接，但是由于MFC并没有这样的控件。于是我在网上发现了一个好用的超链接库，使用如下：</p>]]></description>
			<content:encoded><![CDATA[<p style="text-indent: 2em">在Windows程序开发时候，界面上经常会做一些超链接，但是由于MFC并没有这样的控件。于是我在网上发现了一个好用的超链接库，<a href='http://www.sizeof.cn/wp-content/uploads/2010/06/HyperLink.zip'>点击这里下载</a>，其使用方法如下：</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// 首先在.h文件中定义</span>
CHyperLink m_HyperLink<span style="color: #008080;">;</span>
<span style="color: #666666;">// 在.cpp中DDX绑定，其中IDC_HYPERLINK是普通Static控件</span>
DDX_Control<span style="color: #008000;">&#40;</span>pDX, IDC_HYPERLINK, m_HyperLink<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #666666;">// 设置URL以及下划线</span>
CString sUrl <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;http://www.sizeof.cn&quot;</span><span style="color: #008080;">;</span>
m_HyperLink.<span style="color: #007788;">SetURL</span><span style="color: #008000;">&#40;</span>sUrl<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
m_HyperLink.<span style="color: #007788;">SetUnderline</span><span style="color: #008000;">&#40;</span>FALSE<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.sizeof.cn/html/2010/431.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>C++获得计算机网卡MAC地址</title>
		<link>http://www.sizeof.cn/html/2010/425.html</link>
		<comments>http://www.sizeof.cn/html/2010/425.html#comments</comments>
		<pubDate>Tue, 11 May 2010 12:31:18 +0000</pubDate>
		<dc:creator>虚幻</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[iphlpapi.dll]]></category>
		<category><![CDATA[MAC]]></category>
		<category><![CDATA[网卡]]></category>

		<guid isPermaLink="false">http://www.sizeof.cn/?p=425</guid>
		<description><![CDATA[<p style="text-indent: 2em">C++获得计算机网卡MAC地址，以下程序采用静态加载的办法来实现的，建议在实际项目中，动态加载iphlpapi.dll，来实现获得网卡MAC地址，因为不是每台计算机都有iphlpapi.dll这个问题的，如果是静态加载的话，就可能直接报错。</p>]]></description>
			<content:encoded><![CDATA[<p style="text-indent: 2em">C++获得计算机网卡MAC地址，以下程序采用静态加载的办法来实现的，建议在实际项目中，动态加载iphlpapi.dll，来实现获得网卡MAC地址，因为不是每台计算机都有iphlpapi.dll这个问题的，如果是静态加载的话，就可能直接报错。代码如下：</p>
<p><span id="more-425"></span></p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;stdio.h&gt;</span>
<span style="color: #339900;">#include &lt;Windows.h&gt;</span>
<span style="color: #339900;">#include &lt;iphlpapi.h&gt;</span>
<span style="color: #339900;">#pragma comment(lib, &quot;iphlpapi.lib&quot;)</span>
&nbsp;
<span style="color: #0000ff;">void</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    IP_ADAPTER_INFO adapter<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">5</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
    DWORD buflen<span style="color: #000080;">=</span><span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>adapter<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    DWORD status<span style="color: #000080;">=</span>GetAdaptersInfo<span style="color: #008000;">&#40;</span>adapter,<span style="color: #000040;">&amp;</span>buflen<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    BYTE s<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">8</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>status <span style="color: #000080;">==</span> ERROR_SUCCESS<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        PIP_ADAPTER_INFO painfo<span style="color: #000080;">=</span>adapter<span style="color: #008080;">;</span>
        <span style="color: #0000dd;">memcpy</span><span style="color: #008000;">&#40;</span>s,painfo<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>Address,<span style="color: #0000dd;">6</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Physical Address:%02X-%02X-%02X-%02X-%02X-%02X<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>,s<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span>,s<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span>,s<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#93;</span>,s<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">3</span><span style="color: #008000;">&#93;</span>,s<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">4</span><span style="color: #008000;">&#93;</span>,s<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">5</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.sizeof.cn/html/2010/425.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Google Code Jam 2010 Qualification Round &#8211; Problem C Theme Park</title>
		<link>http://www.sizeof.cn/html/2010/423.html</link>
		<comments>http://www.sizeof.cn/html/2010/423.html#comments</comments>
		<pubDate>Mon, 10 May 2010 15:31:11 +0000</pubDate>
		<dc:creator>虚幻</dc:creator>
				<category><![CDATA[算法]]></category>
		<category><![CDATA[google code jam]]></category>

		<guid isPermaLink="false">http://www.sizeof.cn/?p=423</guid>
		<description><![CDATA[Google Code Jam 2010 Qualification Round - Problem C Theme Park，模拟加记忆缓存]]></description>
			<content:encoded><![CDATA[<p>Roller coasters are so much fun! It seems like everybody who visits the theme park wants to ride the roller coaster. Some people go alone; other people go in groups, and don&#8217;t want to board the roller coaster unless they can all go together. And everyone who rides the roller coaster wants to ride again. A ride costs 1 Euro per person; your job is to figure out how much money the roller coaster will make today.<br />
<span id="more-423"></span><br />
The roller coaster can hold k people at once. People queue for it in groups. Groups board the roller coaster, one at a time, until there are no more groups left or there is no room for the next group; then the roller coaster goes, whether it&#8217;s full or not. Once the ride is over, all of its passengers re-queue in the same order. The roller coaster will run R times in a day.</p>
<p>For example, suppose R=4, k=6, and there are four groups of people with sizes: 1, 4, 2, 1. The first time the roller coaster goes, the first two groups [1, 4] will ride, leaving an empty seat (the group of 2 won&#8217;t fit, and the group of 1 can&#8217;t go ahead of them). Then they&#8217;ll go to the back of the queue, which now looks like 2, 1, 1, 4. The second time, the coaster will hold 4 people: [2, 1, 1]. Now the queue looks like 4, 2, 1, 1. The third time, it will hold 6 people: [4, 2]. Now the queue looks like [1, 1, 4, 2]. Finally, it will hold 6 people: [1, 1, 4]. The roller coaster has made a total of 21 Euros!</p>
<p>Input</p>
<p>The first line of the input gives the number of test cases, T. T test cases follow, with each test case consisting of two lines. The first line contains three space-separated integers: R, k and N. The second line contains N space-separated integers gi, each of which is the size of a group that wants to ride. g0 is the size of the first group, g1 is the size of the second group, etc.</p>
<p>Output</p>
<p>For each test case, output one line containing “Case #x: y”, where x is the case number (starting from 1) and y is the number of Euros made by the roller coaster.</p>
<p>Limits</p>
<p>1 ≤ T ≤ 50.<br />
gi ≤ k.</p>
<p>Small dataset</p>
<p>1 ≤ R ≤ 1000.<br />
1 ≤ k ≤ 100.<br />
1 ≤ N ≤ 10.<br />
1 ≤ gi ≤ 10.<br />
Large dataset</p>
<p>1 ≤ R ≤ 108.<br />
1 ≤ k ≤ 109.<br />
1 ≤ N ≤ 1000.<br />
1 ≤ gi ≤ 107.<br />
Sample</p>
<p>Input </p>
<p>Output </p>
<p>3<br />
4 6 4<br />
1 4 2 1<br />
100 10 1<br />
1<br />
5 5 10<br />
2 4 2 3 4 2 1 2 1 3<br />
Case #1: 21<br />
Case #2: 100<br />
Case #3: 20</p>
<p style="text-indent:2em">题目很简单，就是有很多团队在排队，一个过山车一次最多坐K个人，过山车一天运行R次，一人坐一次1欧元，问过山车一天可以赚多少钱。小规模数据模拟就可以过，但是大数据绝对超时，大数据需要在一遍算一遍记录结果，记录当前团队开始，能做多少人，后面从哪个团队开始。代码如下：</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;stdio.h&gt;</span>
&nbsp;
<span style="color: #0000ff;">struct</span> g_table
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">int</span> gi<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> total<span style="color: #008080;">;</span> 
    <span style="color: #0000ff;">int</span> next<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
g_table group<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1000</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">void</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">FILE</span> <span style="color: #000040;">*</span>fp1,<span style="color: #000040;">*</span>fp2<span style="color: #008080;">;</span>
&nbsp;
    fp1 <span style="color: #000080;">=</span> <span style="color: #0000dd;">fopen</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;c.in.txt&quot;</span>, <span style="color: #FF0000;">&quot;r&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    fp2 <span style="color: #000080;">=</span> <span style="color: #0000dd;">fopen</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;c.out.txt&quot;</span>, <span style="color: #FF0000;">&quot;w+&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">int</span> t<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">fscanf</span><span style="color: #008000;">&#40;</span>fp1, <span style="color: #FF0000;">&quot;%d&quot;</span>, <span style="color: #000040;">&amp;</span>t<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> i<span style="color: #000080;">&lt;=</span>t<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> r,k,n<span style="color: #008080;">;</span>
        <span style="color: #0000dd;">fscanf</span><span style="color: #008000;">&#40;</span>fp1, <span style="color: #FF0000;">&quot;%lu %lu %lu&quot;</span>, <span style="color: #000040;">&amp;</span>r, <span style="color: #000040;">&amp;</span>k, <span style="color: #000040;">&amp;</span>n<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
        <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> j<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> j<span style="color: #000080;">&lt;</span>n<span style="color: #008080;">;</span> j<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> gi<span style="color: #008080;">;</span>
            <span style="color: #0000dd;">fscanf</span><span style="color: #008000;">&#40;</span>fp1, <span style="color: #FF0000;">&quot;%d&quot;</span>, <span style="color: #000040;">&amp;</span>gi<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            group<span style="color: #008000;">&#91;</span>j<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">gi</span> <span style="color: #000080;">=</span> gi<span style="color: #008080;">;</span>
            group<span style="color: #008000;">&#91;</span>j<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">total</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
            group<span style="color: #008000;">&#91;</span>j<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">next</span> <span style="color: #000080;">=</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0000ff;">long</span> <span style="color: #0000ff;">long</span> total <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">int</span> pos <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> j<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> j<span style="color: #000080;">&lt;</span>r<span style="color: #008080;">;</span> j<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>group<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">next</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span> <span style="color: #000040;">&amp;&amp;</span> group<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">total</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #666666;">// 缓存</span>
                total <span style="color: #000040;">+</span><span style="color: #000080;">=</span> group<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">total</span><span style="color: #008080;">;</span>
                pos <span style="color: #000080;">=</span> group<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">next</span><span style="color: #008080;">;</span>
            <span style="color: #008000;">&#125;</span>
            <span style="color: #0000ff;">else</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #666666;">// 计算</span>
                <span style="color: #0000ff;">int</span> last_pos <span style="color: #000080;">=</span> pos<span style="color: #008080;">;</span>
                <span style="color: #0000ff;">int</span> count <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
                <span style="color: #0000ff;">unsigned</span> max_num <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
&nbsp;
                <span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span>count <span style="color: #000080;">&lt;</span> n <span style="color: #000040;">&amp;&amp;</span> max_num <span style="color: #000040;">+</span> group<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">gi</span> <span style="color: #000080;">&lt;=</span> k<span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">&#123;</span>
                    max_num <span style="color: #000040;">+</span><span style="color: #000080;">=</span> group<span style="color: #008000;">&#91;</span>pos<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">gi</span><span style="color: #008080;">;</span>
                    count<span style="color: #000040;">++</span><span style="color: #008080;">;</span>
                    pos <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>pos<span style="color: #000040;">+</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">%</span> n<span style="color: #008080;">;</span>
                <span style="color: #008000;">&#125;</span>
&nbsp;
                group<span style="color: #008000;">&#91;</span>last_pos<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">next</span> <span style="color: #000080;">=</span> pos<span style="color: #008080;">;</span>
                group<span style="color: #008000;">&#91;</span>last_pos<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">total</span> <span style="color: #000080;">=</span> max_num<span style="color: #008080;">;</span>
&nbsp;
                total <span style="color: #000040;">+</span><span style="color: #000080;">=</span> group<span style="color: #008000;">&#91;</span>last_pos<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">total</span><span style="color: #008080;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0000dd;">fprintf</span><span style="color: #008000;">&#40;</span>fp2, <span style="color: #FF0000;">&quot;Case #%d: %lld<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, i, total<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0000dd;">fclose</span><span style="color: #008000;">&#40;</span>fp1<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">fclose</span><span style="color: #008000;">&#40;</span>fp2<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.sizeof.cn/html/2010/423.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

