﻿<?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++博客-Mr.Johnson is Here-随笔分类-C++</title><link>http://www.cppblog.com/koople/category/12236.html</link><description>有时候我会相信一切都会有尽头</description><language>zh-cn</language><lastBuildDate>Fri, 01 Apr 2011 12:18:00 GMT</lastBuildDate><pubDate>Fri, 01 Apr 2011 12:18:00 GMT</pubDate><ttl>60</ttl><item><title>有关于internal linkage 和 external linkage</title><link>http://www.cppblog.com/koople/archive/2011/04/01/143220.html</link><dc:creator>Mr.Johnson</dc:creator><author>Mr.Johnson</author><pubDate>Fri, 01 Apr 2011 09:58:00 GMT</pubDate><guid>http://www.cppblog.com/koople/archive/2011/04/01/143220.html</guid><wfw:comment>http://www.cppblog.com/koople/comments/143220.html</wfw:comment><comments>http://www.cppblog.com/koople/archive/2011/04/01/143220.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/koople/comments/commentRss/143220.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/koople/services/trackbacks/143220.html</trackback:ping><description><![CDATA[<span class=Apple-style-span style="WORD-SPACING: 0px; FONT: medium Simsun; TEXT-TRANSFORM: none; COLOR: rgb(0,0,0); TEXT-INDENT: 0px; WHITE-SPACE: normal; LETTER-SPACING: normal; BORDER-COLLAPSE: separate; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px; orphans: 2; widows: 2">&nbsp;&nbsp;&nbsp;&nbsp;今天看《STL templates：The Complete Guide》看到有关于Restrictions for Nontype Template Parameters 部分，提出了一个有关于external linkage概念，网上一搜基本上都是同一篇中文的有关于internal linkage 和 external linkage的解释，看了半天我还是有点迷糊，用google搜到一本书叫做The C Book 的书里面有有关于这个概念的介绍。<br>&nbsp;&nbsp;&nbsp; 在C语言中有两种linkage object:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1.external object<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; external object就是函数外的声明。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2.internal object<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;internal object就是函数内的声明。<br><br>&nbsp;&nbsp;&nbsp; 在C语言中有三种linkage：<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1.external linkage<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2.internal linkage<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3.none linkage<br>&nbsp;&nbsp;&nbsp; 有关于external linkage 就是所谓的全局变量作用域，internal linkage就是只能在单个文件内使用的变量的作用域，而none linkage 就是那些函数的参数作用域。<br>
<table style="WIDTH: 320px; BORDER-COLLAPSE: collapse" cellSpacing=0 cellPadding=3 align=center border=1>
    <tbody>
        <tr align=middle>
            <td style="WIDTH: 88px; HEIGHT: 44px; BACKGROUND-COLOR: #00ffff"><strong>Type of linkage</strong></td>
            <td><strong>Type of object</strong></td>
            <td><span class=Apple-style-span style="WORD-SPACING: 0px; FONT: medium Simsun; TEXT-TRANSFORM: none; COLOR: rgb(0,0,0); TEXT-INDENT: 0px; WHITE-SPACE: normal; LETTER-SPACING: normal; BORDER-COLLAPSE: separate; webkit-border-horizontal-spacing: 0px; webkit-border-vertical-spacing: 0px; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px; orphans: 2; widows: 2"><span class=Apple-style-span style="FONT-WEIGHT: bold; FONT-FAMILY: Arial; BORDER-COLLAPSE: collapse; TEXT-ALIGN: left; webkit-border-horizontal-spacing: 2px; webkit-border-vertical-spacing: 2px">Accessibility</span></span></td>
        </tr>
        <tr>
            <td>external</td>
            <td>external</td>
            <td>throughout the program</td>
        </tr>
        <tr>
            <td>internal</td>
            <td>external</td>
            <td>a single file</td>
        </tr>
        <tr>
            <td>none</td>
            <td>internal</td>
            <td>local to a single function</td>
        </tr>
    </tbody>
</table>
<br></span>
<img src ="http://www.cppblog.com/koople/aggbug/143220.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/koople/" target="_blank">Mr.Johnson</a> 2011-04-01 17:58 <a href="http://www.cppblog.com/koople/archive/2011/04/01/143220.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++ template 不错的教程 （转载）</title><link>http://www.cppblog.com/koople/archive/2010/12/12/136195.html</link><dc:creator>Mr.Johnson</dc:creator><author>Mr.Johnson</author><pubDate>Sat, 11 Dec 2010 16:10:00 GMT</pubDate><guid>http://www.cppblog.com/koople/archive/2010/12/12/136195.html</guid><wfw:comment>http://www.cppblog.com/koople/comments/136195.html</wfw:comment><comments>http://www.cppblog.com/koople/archive/2010/12/12/136195.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/koople/comments/commentRss/136195.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/koople/services/trackbacks/136195.html</trackback:ping><description><![CDATA[<!--TOOLBAR_START--><!--TOOLBAR_EXEMPT--><!--TOOLBAR_END-->
<table border="0" cellspacing="0" cellpadding="0" width="99%">
<tbody>
<tr valign="top">
<td width="280">
<h1>C++ Templates Tutorial</h1></td>
<td align="right"><a href="http://www.iis.sinica.edu.tw/~kathy/vcstl/default.htm" target="_top">C++ 
Library</a>&nbsp;&nbsp;<br></td></tr></tbody></table>&nbsp; <br>
<hr>

<h2><em>Table of Contents</em></h2>
<h2><a href="http://www.iis.sinica.edu.tw/~kathy/vcstl/templates.htm#T1" target="MAIN">Introduction</a></h2>
<h2><a href="http://www.iis.sinica.edu.tw/~kathy/vcstl/templates.htm#T2" target="MAIN">Class Templates</a></h2>
<ul>
<li><strong><a href="http://www.iis.sinica.edu.tw/~kathy/vcstl/templates.htm#T3" target="MAIN">Implementing a class template</a></strong> 
</li><li><strong><a href="http://www.iis.sinica.edu.tw/~kathy/vcstl/templates.htm#T4" target="MAIN">Implementing class template members</a></strong> 
</li><li><strong><a href="http://www.iis.sinica.edu.tw/~kathy/vcstl/templates.htm#T5" target="MAIN">Using a class template</a></strong> </li></ul>
<h2><a href="http://www.iis.sinica.edu.tw/~kathy/vcstl/templates.htm#T6" target="MAIN">Function Templates</a></h2>
<ul>
<li><strong><a href="http://www.iis.sinica.edu.tw/~kathy/vcstl/templates.htm#T7" target="MAIN">Implementing Template Functions</a></strong> 
</li><li><strong><a href="http://www.iis.sinica.edu.tw/~kathy/vcstl/templates.htm#T8" target="MAIN">Using Template Functions</a></strong> </li></ul>
<h2><a href="http://www.iis.sinica.edu.tw/~kathy/vcstl/templates.htm#T9" target="MAIN">Template Instantiation</a></h2>
<h2><a href="http://www.iis.sinica.edu.tw/~kathy/vcstl/templates.htm#T10">Class Template Specialization</a></h2>
<ul>
<li><strong><a href="http://www.iis.sinica.edu.tw/~kathy/vcstl/templates.htm#T11" target="MAIN">Template Class Partial Specialization</a></strong> 
</li></ul>
<h2><a href="http://www.iis.sinica.edu.tw/~kathy/vcstl/templates.htm#T12" target="MAIN">Template Function Specialization</a></h2>
<h2><a href="http://www.iis.sinica.edu.tw/~kathy/vcstl/templates.htm#T13&quot;" target="MAIN">Template Parameters</a></h2>
<h2><a href="http://www.iis.sinica.edu.tw/~kathy/vcstl/templates.htm#T14" target="MAIN">Static Members and Variables</a></h2>
<h2><a href="http://www.iis.sinica.edu.tw/~kathy/vcstl/templates.htm#T15" target="MAIN">Templates and Friends</a></h2>
<hr>

<h1><a name="T1">Introduction</a></h1>
<p>Many C++ programs use common data structures like stacks, queues and lists. A 
program may require a queue of customers and a queue of messages. One could 
easily implement a queue of customers, then take the existing code and implement 
a queue of messages. The program grows, and now there is a need for a queue of 
orders. So just take the queue of messages and convert that to a queue of orders 
(Copy, paste, find, replace????). Need to make some changes to the queue 
implementation? Not a very easy task, since the code has been duplicated in many 
places. Re-inventing source code is not an intelligent approach in an object 
oriented environment which encourages re-usability. It seems to make more sense 
to implement a queue that can contain any arbitrary type rather than duplicating 
code. How does one do that? The answer is to use type parameterization, more 
commonly referred to as templates. 
</p><p>C++ templates allow one to implement a generic Queue&lt;T&gt; template that 
has a type parameter T. T can be replaced with actual types, for example, 
Queue&lt;Customers&gt;, and C++ will generate the class Queue&lt;Customers&gt;. 
Changing the implementation of the Queue becomes relatively simple. Once the 
changes are implemented in the template Queue&lt;T&gt;, they are immediately 
reflected in the classes Queue&lt;Customers&gt;, Queue&lt;Messages&gt;, and 
Queue&lt;Orders&gt;. 
</p><p>Templates are very useful when implementing generic constructs like vectors, 
stacks, lists, queues which can be used with any arbitrary type. C++ templates 
provide a way to re-use source code as opposed to inheritance and composition 
which provide a way to re-use object code. 
</p><p>C++ provides two kinds of templates: class templates and function templates. 
Use function templates to write generic functions that can be used with 
arbitrary types. For example, one can write searching and sorting routines which 
can be used with any arbitrary type. The Standard Template Library generic 
algorithms have been implemented as function templates, and the containers have 
been implemented as class templates. 
</p><h1><a name="T2">Class Templates</a></h1>
<h2><a name="T3">Implementing a class template</a></h2>
<p>A class template definition looks like a regular class definition, except it 
is prefixed by the keyword template. For example, here is the definition of a 
class template for a Stack. </p><pre>template &lt;class T&gt;
class Stack
{
public:
	Stack(int = 10) ; 
	~Stack() { delete [] stackPtr ; }
	int push(const T&amp;); 
	int pop(T&amp;) ;  
	int isEmpty()const { return top == -1 ; } 
	int isFull() const { return top == size - 1 ; } 
private:
	int size ;  // number of elements on Stack.
	int top ;  
	T* stackPtr ;  
} ;
</pre>
<p>T is a type parameter and it can be any type. For example, 
Stack&lt;Token&gt;, where Token is a user defined class. T does not have to be a 
class type as implied by the keyword class. For example, Stack&lt;int&gt; and 
Stack&lt;Message*&gt; are valid instantiations, even though int and Message* are 
not "classes". 
</p><h2><a name="T4">Implementing class template member functions</a></h2>
<p>Implementing template member functions is somewhat different compared to the 
regular class member functions. The declarations and definitions of the class 
template member functions should all be in the same header file. The 
declarations and definitions need to be in the same header file. Consider the 
following. <br><br>
<table border="1" cellpadding="5" cols="3">
<tbody>
<tr>
<td><pre>//B.H
template &lt;class t&gt;
class b
{
public:
	b() ;
	~b() ;
} ;
</pre></td>
<td><pre>// B.CPP
#include "B.H"
template &lt;class t&gt;
b&lt;t&gt;::b()
{
}
template &lt;class t&gt;
b&lt;t&gt;::~b()
{
}
</pre></td>
<td><pre>//MAIN.CPP
#include "B.H"
void main()
{
	 b&lt;int&gt; bi ;
	 b &lt;float&gt; bf ;
}
</pre></td></tr></tbody></table>
</p><p>When compiling B.cpp, the compiler has both the declarations and the 
definitions available. At this point the compiler does not need to generate any 
definitions for template classes, since there are no instantiations. When the 
compiler compiles main.cpp, there are two instantiations: template class 
B&lt;int&gt; and B&lt;float&gt;. At this point the compiler has the declarations 
but no definitions! 
</p><p>While implementing class template member functions, the definitions are 
prefixed by the keyword template. Here is the complete implementation of class 
template Stack: </p><pre>//stack.h
#pragma once
template &lt;class T&gt;
class Stack
{
public:
	Stack(int = 10) ; 
	~Stack() { delete [] stackPtr ; }
	int push(const T&amp;); 
	int pop(T&amp;) ;  // pop an element off the stack
	int isEmpty()const { return top == -1 ; } 
	int isFull() const { return top == size - 1 ; } 
private:
	int size ;  // Number of elements on Stack
	int top ;  
	T* stackPtr ;  
} ;

//constructor with the default size 10
template &lt;class T&gt;
Stack&lt;T&gt;::Stack(int s)
{
	size = s &gt; 0 &amp;&amp; s &lt; 1000 ? s : 10 ;  
	top = -1 ;  // initialize stack
	stackPtr = new T[size] ; 
}
 // push an element onto the Stack 
template &lt;class T&gt;
int Stack&lt;T&gt;::push(const T&amp; item)
{
	if (!isFull())
	{
		stackPtr[++top] = item ;
		return 1 ;  // push successful
	}
	return 0 ;  // push unsuccessful
}

// pop an element off the Stack
template &lt;class T&gt; 
int Stack&lt;T&gt;::pop(T&amp; popValue) 
{
	if (!isEmpty())
	{
		popValue = stackPtr[top--] ;
		return 1 ;  // pop successful
	}
	return 0 ;  // pop unsuccessful
}
</pre>
<h2><a name="T5">Using a class template</a></h2>
<p>Using a class template is easy. Create the required classes by plugging in 
the actual type for the type parameters. This process is commonly known as 
"Instantiating a class". Here is a sample driver class that uses the Stack class 
template. </p><pre>#include &lt;iostream&gt;
#include "stack.h"
using namespace std ;
void main()
{
	typedef Stack&lt;float&gt; FloatStack ;
	typedef Stack&lt;int&gt; IntStack ;

	FloatStack fs(5) ;
	float f = 1.1 ;
	cout &lt;&lt; "Pushing elements onto fs" &lt;&lt; endl ;
	while (fs.push(f))
	{
		cout &lt;&lt; f &lt;&lt; ' ' ;
		f += 1.1 ;
	}
	cout &lt;&lt; endl &lt;&lt; "Stack Full." &lt;&lt; endl
	&lt;&lt; endl &lt;&lt; "Popping elements from fs" &lt;&lt; endl ;
	while (fs.pop(f))
		cout &lt;&lt; f &lt;&lt; ' ' ;
	cout &lt;&lt; endl &lt;&lt; "Stack Empty" &lt;&lt; endl ;
	cout &lt;&lt; endl ;

	IntStack is ;
	int i = 1.1 ;
	cout &lt;&lt; "Pushing elements onto is" &lt;&lt; endl ;
	while (is.push(i))
	{
		cout &lt;&lt; i &lt;&lt; ' ' ;
		i += 1 ;
	}
	cout &lt;&lt; endl &lt;&lt; "Stack Full" &lt;&lt; endl
	&lt;&lt; endl &lt;&lt; "Popping elements from is" &lt;&lt; endl ;
	while (is.pop(i))
			cout &lt;&lt; i &lt;&lt; ' ' ;
	cout &lt;&lt; endl &lt;&lt; "Stack Empty" &lt;&lt; endl ;
}
</pre>
<h3>Program Output</h3><pre>Pushing elements onto fs
1.1 2.2 3.3 4.4 5.5 
Stack Full.

Popping elements from fs
5.5 4.4 3.3 2.2 1.1 
Stack Empty

Pushing elements onto is
1 2 3 4 5 6 7 8 9 10 
Stack Full

Popping elements from is
10 9 8 7 6 5 4 3 2 1 
Stack Empty
</pre>
<p>In the above example we defined a class template Stack. In the driver program 
we instantiated a Stack of float (FloatStack) and a Stack of int(IntStack). Once 
the template classes are instantiated you can instantiate objects of that type 
(for example, fs and is.) 
</p><p>A good programming practice is using typedef while instantiating template 
classes. Then throughout the program, one can use the typedef name. There are 
two advantages: 
</p><ul>
<li><strong>typedef's</strong> are very useful when "templates of templates" come into 
usage. For example, when instantiating an STL vector of int's, you could use: <pre>		 typedef vector&lt;int, allocator&lt;int&gt; &gt; INTVECTOR ; 
	</pre>
</li><li>If the template definition changes, simply change the typedef definition. 
For example, currently the definition of template class vector requires a second 
parameter. <pre>		typedef vector&lt;int, allocator&lt;int&gt; &gt; INTVECTOR ;
		INTVECTOR vi1 ;
	</pre>In a future version, the second parameter may not be required, for 
example, <pre>		typedef vector&lt;int&gt; INTVECTOR ;
		INTVECTOR vi1 ;
	</pre></li></ul>
<p>Imagine how many changes would be required if there was no <strong>typedef</strong>! 
</p><h1><a name="T6">Function Templates</a></h1>
<p>To perform identical operations for each type of data compactly and 
conveniently, use function templates. You can write a single function template 
definition. Based on the argument types provided in calls to the function, the 
compiler automatically instantiates separate object code functions to handle 
each type of call appropriately. The STL algorithms are implemented as function 
templates. 
</p><h2><a name="T7">Implementing Template Functions</a></h2>
<p>Function templates are implemented like regular functions, except they are 
prefixed with the keyword template. Here is a sample with a function template. </p><pre>#include &lt;iostream&gt;
using namespace std ;
//max returns the maximum of the two elements
template &lt;class T&gt;
T max(T a, T b)
{
	return a &gt; b ? a : b ;
}
</pre>
<h2><a name="T8">Using Template Functions</a></h2>
<p>Using function templates is very easy: just use them like regular functions. 
When the compiler sees an instantiation of the function template, for example: 
the call max(10, 15) in function main, the compiler generates a function 
max(int, int). Similarly the compiler generates definitions for max(char, char) 
and max(float, float) in this case. </p><pre>#include &lt;iostream&gt;
using namespace std ;
//max returns the maximum of the two elements
template &lt;class T&gt;
T max(T a, T b)
{
	return a &gt; b ? a : b ;
}
void main()
{
   
	cout &lt;&lt; "max(10, 15) = " &lt;&lt; max(10, 15) &lt;&lt; endl ;
	cout &lt;&lt; "max('k', 's') = " &lt;&lt; max('k', 's') &lt;&lt; endl ;
	cout &lt;&lt; "max(10.1, 15.2) = " &lt;&lt; max(10.1, 15.2) &lt;&lt; endl ;
}
</pre>
<h3>Program Output</h3><pre>max(10, 15) = 15
max('k', 's') = s
max(10.1, 15.2) = 15.2
</pre>
<h1><a name="T9">Template Instantiation</a></h1>
<p>When the compiler generates a class, function or static data members from a 
template, it is referred to as template instantiation. 
</p><ul>
<li>A class generated from a class template is called a generated class. 
</li><li>A function generated from a function template is called a generated 
function. 
</li><li>A static data member generated from a static data member template is called 
a generated static data member. </li></ul>
<p>The compiler generates a class, function or static data members from a 
template when it sees an implicit instantiation or an explicit instantiation of 
the template. 
</p><ol>
<li>Consider the following sample. This is an example of implicit instantiation 
of a class template. <pre>template &lt;class T&gt;
class Z
{
  public:
Z() {} ;
~Z() {} ;
void f(){} ;
void g(){} ;
} ;

int main()
{
Z&lt;int&gt; zi ;   //implicit instantiation generates class Z&lt;int&gt;
Z&lt;float&gt; zf ; //implicit instantiation generates class Z&lt;float&gt;
return 0 ;
}
</pre>
</li><li>Consider the following sample. This sample uses the template class members 
Z&lt;T&gt;::f() and Z&lt;T&gt;::g(). <pre>template &lt;class T&gt;
class Z
{
  public:
Z() {} ;
~Z() {} ;
void f(){} ;
void g(){} ;
} ;

int main()
{
Z&lt;int&gt; zi ; //implicit instantiation generates class Z&lt;int&gt;
zi.f() ;    //and generates function Z&lt;int&gt;::f()
Z&lt;float&gt; zf ; //implicit instantiation generates class Z&lt;float&gt;
zf.g() ;      //and generates function Z&lt;float&gt;::g()
return 0 ;
}
</pre>
<p>This time in addition to the generating classes Z&lt;int&gt; and 
Z&lt;float&gt;, with constructors and destructors, the compiler also generates 
definitions for Z&lt;int&gt;::f() and Z&lt;float&gt;::g(). The compiler does not 
generate definitions for functions, nonvirtual member functions, class or member 
class that does not require instantiation. In this example, the compiler did not 
generate any definitions for Z&lt;int&gt;::g() and Z&lt;float&gt;::f(), since 
they were not required. 
</p><p>&#160;</p>
</li><li>Consider the following sample. This is an example of explicit instantiation 
of a class template. <pre>template &lt;class T&gt;
class Z
{
  public:
Z() {} ;
~Z() {} ;
void f(){} ;
void g(){} ;
} ;

int main()
{
template class Z&lt;int&gt; ; //explicit instantiation of class Z&lt;int&gt;
template class Z&lt;float&gt; ; //explicit instantiation of 
                          //class Z&lt;float&gt;
return 0 ;
}
</pre>
</li><li>Consider the following sample. Will the compiler generate any classes in 
this case? The answer is NO. <pre>template &lt;class T&gt;
class Z
{
  public:
Z() {} ;
~Z() {} ;
void f(){} ;
void g(){} ;
} ;

int main()
{
Z&lt;int&gt;* p_zi ; //instantiation of class Z&lt;int&gt; not required
Z&lt;float&gt;* p_zf ; //instantiation of class Z&lt;float&gt; not required
return 0 ;
}
</pre>
<p>This time the compiler does not generate any definitions! There is no need 
for any definitions. It is similar to declaring a pointer to an undefined class 
or struct. 
</p><p>&#160;</p>
</li><li>Consider the following sample. This is an example of implicit instantiation 
of a function template. <pre>//max returns the maximum of the two elements
template &lt;class T&gt;
T max(T a, T b)
{
    return a &gt; b ? a : b ;
}
void main()
{
int I ;
I = max(10, 15) ; //implicit instantiation of max(int, int)
char c ;
c = max('k', 's') ; //implicit instantiation of max(char, char)
}
</pre>
<p>In this case the compiler generates functions max(int, int) and max(char, 
char). The compiler generates definitions using the template function max. 
</p><p>&#160;</p>
</li><li>Consider the following sample. This is an example of explicit instantiation 
of a function template. <pre>template &lt;class T&gt;
void Test(T r_t)
{
}

int main()
{
//explicit instantiation of Test(int)
	template void Test&lt;int&gt;(int) ;
	return 0 ;
}
</pre><strong>NOTE</strong>: Visual C++ 5.0 does not support this syntax currently. The 
above sample causes compiler error C1001. 
<p>In this case the compiler would generate function Test(int). The compiler 
generates the definition using the template function Test. 
</p><p>&#160;</p>
</li><li>If an instantiation of a class template is required, and the template 
declared but not defined, the program is ill-formed. VC5.0 compiler generates 
error C2079. <pre>template &lt;class T&gt; class X ;

int main()
{
	X&lt;int&gt; xi ; //error C2079: 'xi' uses undefined class 'X&lt;int&gt;'
	return 0 ;
}
</pre>
</li><li>Instantiating virtual member functions of a class template that does not 
require instantiation is implementation defined. For example, in the following 
sample, virtual function X&lt;T&gt;::Test() is not required, VC5.0 generates a 
definition for X&lt;T&gt;::Test. <pre>template &lt;class T&gt; 
class X 
{
	public:
		virtual void Test() {} 
};

int main()
{
	X&lt;int&gt; xi ; //implicit instantiation of X&lt;int&gt;
	return 0 ;
}
</pre>
<p>In this case the compiler generates a definition for X&lt;int&gt;::Test, even 
if it is not required. </p></li></ol>
<h1><a name="T10">Class Template Specialization</a></h1>
<p>In some cases it is possible to override the template-generated code by 
providing special definitions for specific types. This is called template 
specialization. The following example defines a template class specialization 
for template class stream. </p><pre>#include &lt;iostream&gt;
using namespace std ;

template &lt;class T&gt;
class stream
{
	public:
		void f() { cout &lt;&lt; "stream&lt;T&gt;::f()"&lt;&lt; endl ;}
} ;

template &lt;&gt;
class stream&lt;char&gt;
{
	public:
		void f() { cout &lt;&lt; "stream&lt;char&gt;::f()"&lt;&lt; endl ;}
} ;

int main()
{
	stream&lt;int&gt; si ;
	stream&lt;char&gt; sc ;

	si.f() ;
	sc.f() ;
	
	return 0 ;
}
</pre>
<h3>Program Output</h3><pre>stream&lt;T&gt;::f()
stream&lt;char&gt;::f()
</pre>
<p>In the above example, stream&lt;char&gt; is used as the definition of streams 
of chars; other streams will be handled by the template class generated from the 
class template. 
</p><h2><a name="T11">Template Class Partial Specialization</a></h2>
<p>You may want to generate a specialization of the class for just one 
parameter, for example </p><pre>//base template class
template&lt;typename T1, typename T2&gt; 
class X 
{
} ;

//partial specialization
template&lt;typename T1&gt; 
class X&lt;T1, int&gt; 
{
} ; //C2989 here

int main()
{
	// generates an instantiation from the base template
	X&lt;char, char&gt; xcc ; 
	
	//generates an instantiation from the partial specialization
X&lt;char, int&gt; xii ;  

	return 0 ;
}
</pre>
<p>A partial specialization matches a given actual template argument list if the 
template arguments of the partial specialization can be deduced from the actual 
template argument list. 
</p><p><strong>NOTE</strong>: Visual C++ 5.0 does not support template class partial 
specialization. The above sample causes compiler error C2989: template class has 
already been defined as a non-template class. 
</p><h1><a name="T12">Template Function Specialization</a></h1>
<p>In some cases it is possible to override the template-generated code by 
providing special definitions for specific types. This is called template 
specialization. The following example demonstrates a situation where overriding 
the template generated code would be necessary: </p><pre>#include &lt;iostream&gt;
using namespace std ;

//max returns the maximum of the two elements of type T, where T is a
//class or data type for which operator&gt; is defined.
template &lt;class T&gt;
T max(T a, T b)
{
    return a &gt; b ? a : b ;
}

int main()
{    
    cout &lt;&lt; "max(10, 15) = " &lt;&lt; max(10, 15) &lt;&lt; endl ;
    cout &lt;&lt; "max('k', 's') = " &lt;&lt; max('k', 's') &lt;&lt; endl ;
    cout &lt;&lt; "max(10.1, 15.2) = " &lt;&lt; max(10.1, 15.2) &lt;&lt; endl ;
    cout &lt;&lt; "max(\"Aladdin\", \"Jasmine\") = " &lt;&lt; max("Aladdin", "Jasmine") &lt;&lt; endl ;
    return 0 ;
}
</pre>
<h3>Program Output</h3><pre>max(10, 15) = 15
max('k', 's') = s
max(10.1, 15.2) = 15.2
max("Aladdin", "Jasmine") = Aladdin
</pre>
<p>Not quite the expected results! Why did that happen? The function call 
max("Aladdin", "Jasmine") causes the compiler to generate code for max(char*, 
char*), which compares the addresses of the strings! To correct special cases 
like these or to provide more efficient implementations for certain types, one 
can use template specializations. The above example can be rewritten with 
specialization as follows: </p><pre>#include &lt;iostream&gt;
#include &lt;cstring&gt;
using namespace std ;

//max returns the maximum of the two elements
template &lt;class T&gt;
T max(T a, T b)
{
    return a &gt; b ? a : b ;
}

// Specialization of max for char*
template &lt;&gt;
char* max(char* a, char* b)
{
    return strcmp(a, b) &gt; 0 ? a : b ;
}

int main()
{
    
    cout &lt;&lt; "max(10, 15) = " &lt;&lt; max(10, 15) &lt;&lt; endl ;
    cout &lt;&lt; "max('k', 's') = " &lt;&lt; max('k', 's') &lt;&lt; endl ;
    cout &lt;&lt; "max(10.1, 15.2) = " &lt;&lt; max(10.1, 15.2) &lt;&lt; endl ;
    cout &lt;&lt; "max(\"Aladdin\", \"Jasmine\") = " &lt;&lt; max("Aladdin", "Jasmine") &lt;&lt; endl ;
    return 0 ;
}
</pre>
<h3>Program Output</h3><pre>max(10, 15) = 15
max('k', 's') = s
max(10.1, 15.2) = 15.2
max("Aladdin", "Jasmine") = Jasmine
</pre>
<h1><a name="T13">Template Parameters</a></h1>
<ol>
<li>C++ templates allow one to implement a generic Queue&lt;T&gt; template that 
has a type parameter T. T can be replaced with actual types, for example, 
Queue&lt;Customers&gt;, and C++ will generate the class Queue&lt;Customers&gt;. 
For example, <pre>template &lt;class T&gt;
class Stack
{
} ;
</pre>
<p>Here T is a template parameter, also referred to as type-parameter. 
</p><p>&#160;</p>
</li><li>C++ allows you to specify a default template parameter, so the definition 
could now look like: <pre>	template &lt;class T = float, int elements = 100&gt; Stack { ....} ;
</pre>
<p>Then a declaration such as </p><pre>	Stack&lt;&gt; mostRecentSalesFigures ;	
</pre>
<p>would instantiate (at compile time) a 100 element Stack template class named 
mostRecentSalesFigures of float values; this template class would be of type 
Stack&lt;float, 100&gt;. 
</p><p>Note, C++ also allows non-type template parameters. In this case, template 
class Stack has an int as a non-type parameter. 
</p><p><strong>If you specify a default template parameter for any formal parameter, the 
rules are the same as for functions and default parameters. Once a default 
parameter is declared all subsequent parameters must have defaults.</strong> 
</p><p>&#160;</p>
</li><li>Default arguments cannot be specified in a declaration or a definition of a 
specialization. For example, <pre>template &lt;class T, int size&gt;
class Stack
{
} ;

//error C2989: 'Stack&lt;int,10&gt;' : template class has already been
//defined as a non-template class
template &lt;class T, int size = 10&gt;
class Stack&lt;int, 10&gt;
{
} ;

int main()
{
	Stack&lt;float,10&gt; si ;
              return 0 ;
}
</pre>
<p>&#160;</p>
</li><li>A type-parameter defines its identifier to be a type-name in the scope of 
the template declaration, and canot be re-declared within its scope (including 
nested scopes). For example, <pre>template &lt;class T, int size&gt;
class Stack
{
	int T ; //error type-parameter re-defined.
	void f()
	{
		char T ; //error type-parameter re-defined.
	}
} ;

class A {} ;
int main()
{
	Stack&lt;A,10&gt; si ;
       return 0 ;
}
</pre>
<p><strong>NOTE</strong>: VC++ 5.0 or SP1 compiles this sample without any errors. It does 
not flag the re-definition of type-parameter as an error. 
</p><p>&#160;</p>
</li><li>The value of a non-type-parameter cannot be assigned to or have its value 
changed. For example, <pre>template &lt;class T, int size&gt;
class Stack
{
	void f()
	{
		//error C2105: '++' needs l-value             
             size++ ; //error change of template argument value
	}
} ;


int main()
{
	Stack&lt;double,10&gt; si ;
       return 0 ;
}
</pre>
</li><li>A template-parameter that could be interpreted as either a 
parameter-declaration or a type-parameter, is taken as a type-parameter. For 
example, <pre>class T {} ;
int i ;

template &lt;class T, T i&gt;
void f(T t)
{
	T t1 = i ; //template arguments T and i
	::T t2 = ::i ; //globals T and i 
} 



int main()
{
	f('s') ; //C2783 here
	return 0 ;
}
</pre>
<p><strong>NOTE</strong>: Compiling the above sample using VC++ 5.0 and SP1 causes 
compiler error C2783: could not deduce template argument for 'i'. To workaround 
the problem, replace the call to f('s') with f&lt;char, 's'&gt;('s'). </p><pre>class T {} ;
int i ;

template &lt;class T, T i&gt;
void f(T t)
{
	T t1 = i ; //template arguments T and i
	::T t2 = ::i ; //globals T and i 
} 

int main()
{
	f&lt;char, 's'&gt;('s') ; //workaround
	return 0 ;
}
</pre>
</li><li>A non-type template parameter cannot be of floating type. For example, <pre>template &lt;double d&gt; class X ; //error C2079: 'xd' uses 
                              //undefined class 'X&lt;1.e66&gt;'
//template &lt;double* pd&gt; class X ; //ok
//template &lt;double&amp; rd&gt; class X ; //ok

int main()
{
	X&lt;1.0&gt; xd ;
	return 0 ;
}
</pre></li></ol>
<h1><a name="T14">Static Members and Variables</a></h1>
<ol>
<li>Each template class or function generated from a template has its own copies 
of any static variables or members. 
<p>&#160;</p>
</li><li>Each instantiation of a function template has it's own copy of any static 
variables defined within the scope of the function. For example, <pre>template &lt;class T&gt;
class X
{
    public:
	static T s ;
} ;

int main()
{
	X&lt;int&gt; xi ;
       X&lt;char*&gt; xc ;
}
</pre>
<p>Here X&lt;int&gt; has a static data member s of type int and X&lt;char*&gt; 
has a static data member s of type char*. 
</p><p>&#160;</p>
</li><li>Static members are defined as follows. <pre>#include &lt;iostream&gt;
using namespace std ;

template &lt;class T&gt;
class X
{
    public:
	static T s ;
} ;

template &lt;class T&gt; T X&lt;T&gt;::s = 0 ;
template &lt;&gt; int X&lt;int&gt;::s = 3 ;
template &lt;&gt; char* X&lt;char*&gt;::s = "Hello" ;

int main()
{
	X&lt;int&gt; xi ;
	cout &lt;&lt; "xi.s = " &lt;&lt; xi.s &lt;&lt; endl ;

       X&lt;char*&gt; xc ;
	cout &lt;&lt; "xc.s = " &lt;&lt; xc.s &lt;&lt; endl ;
	
	return 0 ;
}
</pre>
<h3>Program Output</h3><pre>xi.s = 10
xc.s = Hello
</pre>
</li><li>Each instantiation of a function template has it's own copy of the static 
variable. For example, <pre>#include &lt;iostream&gt;
using namespace std ;

template &lt;class T&gt;
void f(T t)
{
	static T s  = 0;
	s = t ;
	cout &lt;&lt; "s = " &lt;&lt; s &lt;&lt; endl ;
} 

int main()
{
	f(10) ;
	f("Hello") ;
	
	return 0 ;
}
</pre>
<h3>Program Output</h3><pre>s = 10
s = Hello
</pre>
<p>Here f&lt;int&gt;(int) has a static variable s of type int, and 
f&lt;char*&gt;(char*) has a static variable s of type char*. </p></li></ol>
<h1><a name="T15">Templates and Friends</a></h1>
<p>Friendship can be established between a class template and a global function, 
a member function of another class (possibly a template class), or even an 
entire class (possible template class). The table below lists the results of 
declaring different kinds of friends of a class. 
</p><p>
<table border="1" cellpadding="5" cols="3">
<tbody>
<tr>
<td align="middle"><strong>Class Template</strong> </td>
<td align="middle"><strong>friend declaration in class template X</strong> </td>
<td align="middle"><strong>Results of giving friendship</strong> </td></tr>
<tr>
<td>template class &lt;T&gt; class X </td>
<td>friend void f1() ; </td>
<td>makes f1() a friend of all instantiations of template X. For example, f1() 
is a friend of X&lt;int&gt;, X&lt;A&gt;, and X&lt;Y&gt;. </td></tr>
<tr>
<td>template class &lt;T&gt; class X </td>
<td>friend void f2(X&lt;T&gt;&amp;) ; </td>
<td>For a particular type T for example, float, makes f2(X&lt;float&gt;&amp;) a 
friend of class X&lt;float&gt; only. f2(x&lt;float&gt;&amp;) cannot be a friend 
of class X&lt;A&gt;. </td></tr>
<tr>
<td>template class &lt;T&gt; class X </td>
<td>friend A::f4() ; // A is a user defined class with a member function f4() ; 
</td>
<td>makes A::f4() a friend of all instantiations of template X. For example, 
A::f4() is a friend of X&lt;int&gt;, X&lt;A&gt;, and X&lt;Y&gt;. </td></tr>
<tr>
<td>template class &lt;T&gt; class X </td>
<td>friend C&lt;T&gt;::f5(X&lt;T&gt;&amp;) ; // C is a class template with a 
member function f5 </td>
<td>For a particular type T for example, float, makes 
C&lt;float&gt;::f5(X&lt;float&gt;&amp;) a friend of class X&lt;float&gt; only. 
C&lt;float&gt;::f5(x&lt;float&gt;&amp;) cannot be a friend of class X&lt;A&gt;. 
</td></tr>
<tr>
<td>template class &lt;T&gt; class X </td>
<td>friend class Y ; </td>
<td>makes every member function of class Y a friend of every template class 
produced from the class template X. </td></tr>
<tr>
<td>template class &lt;T&gt; class X </td>
<td>friend class Z&lt;T&gt; ; </td>
<td>when a template class is instantiated with a particular type T, such as a 
float, all members of class Z&lt;float&gt; become friends of template class 
X&lt;float&gt;. </td></tr></tbody></table>
</p><hr>
&#169; 1997 Microsoft Corporation. All rights reserved. Terms of Use.
<img src ="http://www.cppblog.com/koople/aggbug/136195.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/koople/" target="_blank">Mr.Johnson</a> 2010-12-12 00:10 <a href="http://www.cppblog.com/koople/archive/2010/12/12/136195.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>char *  和 memcpy</title><link>http://www.cppblog.com/koople/archive/2010/11/27/134858.html</link><dc:creator>Mr.Johnson</dc:creator><author>Mr.Johnson</author><pubDate>Sat, 27 Nov 2010 15:44:00 GMT</pubDate><guid>http://www.cppblog.com/koople/archive/2010/11/27/134858.html</guid><wfw:comment>http://www.cppblog.com/koople/comments/134858.html</wfw:comment><comments>http://www.cppblog.com/koople/archive/2010/11/27/134858.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/koople/comments/commentRss/134858.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/koople/services/trackbacks/134858.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 最近项目里面经常使用到memcpy,使用memcpy的时候，如下代码
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br><img id=Codehighlighter1_48_150_Open_Image onclick="this.style.display='none'; Codehighlighter1_48_150_Open_Text.style.display='none'; Codehighlighter1_48_150_Closed_Image.style.display='inline'; Codehighlighter1_48_150_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_48_150_Closed_Image onclick="this.style.display='none'; Codehighlighter1_48_150_Closed_Text.style.display='none'; Codehighlighter1_48_150_Open_Image.style.display='inline'; Codehighlighter1_48_150_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_48_150_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_48_150_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;src&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Hello&nbsp;World</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;dst&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Hello&nbsp;World&nbsp;Again!</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">memcpy(dst,src,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(src));<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span></div>
报错说内存地址错误..也就是Unhandled exception in&nbsp;.exe：0xC0000005:Access Violation。<br><br>&nbsp;&nbsp;&nbsp;&nbsp; 没事就研究了下memcpy&nbsp;，memcpy在VC6.0版本中的代码如下
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #008080">&nbsp;1</span><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;__cdecl&nbsp;memcpy&nbsp;(<br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;dst,<br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;src,<br></span><span style="COLOR: #008080">&nbsp;4</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;size_t&nbsp;count<br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)<br></span><span style="COLOR: #008080">&nbsp;6</span><span style="COLOR: #000000"><img id=Codehighlighter1_101_744_Open_Image onclick="this.style.display='none'; Codehighlighter1_101_744_Open_Text.style.display='none'; Codehighlighter1_101_744_Closed_Image.style.display='inline'; Codehighlighter1_101_744_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_101_744_Closed_Image onclick="this.style.display='none'; Codehighlighter1_101_744_Closed_Text.style.display='none'; Codehighlighter1_101_744_Open_Image.style.display='inline'; Codehighlighter1_101_744_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_101_744_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_101_744_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;ret&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;dst;<br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br></span><span style="COLOR: #008080">&nbsp;9</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #0000ff">#if</span><span style="COLOR: #000000">&nbsp;defined&nbsp;(_M_MRX000)&nbsp;||&nbsp;defined&nbsp;(_M_ALPHA)&nbsp;||&nbsp;defined&nbsp;(_M_PPC)</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img id=Codehighlighter1_204_330_Open_Image onclick="this.style.display='none'; Codehighlighter1_204_330_Open_Text.style.display='none'; Codehighlighter1_204_330_Closed_Image.style.display='inline'; Codehighlighter1_204_330_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_204_330_Closed_Image onclick="this.style.display='none'; Codehighlighter1_204_330_Closed_Text.style.display='none'; Codehighlighter1_204_330_Open_Image.style.display='inline'; Codehighlighter1_204_330_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_204_330_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_204_330_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">extern</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;RtlMoveMemory(&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">,&nbsp;size_t&nbsp;count&nbsp;);<br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RtlMoveMemory(&nbsp;dst,&nbsp;src,&nbsp;count&nbsp;);<br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #0000ff">#else</span><span style="COLOR: #000000">&nbsp;&nbsp;/*&nbsp;defined&nbsp;(_M_MRX000)&nbsp;||&nbsp;defined&nbsp;(_M_ALPHA)&nbsp;||&nbsp;defined&nbsp;(_M_PPC)&nbsp;*/</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img id=Codehighlighter1_415_485_Open_Image onclick="this.style.display='none'; Codehighlighter1_415_485_Open_Text.style.display='none'; Codehighlighter1_415_485_Closed_Image.style.display='inline'; Codehighlighter1_415_485_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_415_485_Closed_Image onclick="this.style.display='none'; Codehighlighter1_415_485_Closed_Text.style.display='none'; Codehighlighter1_415_485_Open_Image.style.display='inline'; Codehighlighter1_415_485_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_415_485_Closed_Text>/**/</span><span id=Codehighlighter1_415_485_Open_Text><span style="COLOR: #008000">/*</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">17</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;copy&nbsp;from&nbsp;lower&nbsp;addresses&nbsp;to&nbsp;higher&nbsp;addresses<br></span><span style="COLOR: #008080">18</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">*/</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img id=Codehighlighter1_511_644_Open_Image onclick="this.style.display='none'; Codehighlighter1_511_644_Open_Text.style.display='none'; Codehighlighter1_511_644_Closed_Image.style.display='inline'; Codehighlighter1_511_644_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_511_644_Closed_Image onclick="this.style.display='none'; Codehighlighter1_511_644_Closed_Text.style.display='none'; Codehighlighter1_511_644_Open_Image.style.display='inline'; Codehighlighter1_511_644_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">&nbsp;(count</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">)&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_511_644_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_511_644_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)dst&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)src;<br></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dst&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;(</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)dst&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;(</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)src&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">24</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #0000ff">#endif</span><span style="COLOR: #000000">&nbsp;&nbsp;/*&nbsp;defined&nbsp;(_M_MRX000)&nbsp;||&nbsp;defined&nbsp;(_M_ALPHA)&nbsp;||&nbsp;defined&nbsp;(_M_PPC)&nbsp;*/</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">25</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br></span><span style="COLOR: #008080">26</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">(ret);<br></span><span style="COLOR: #008080">27</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">28</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span></div>
实际上 memcpy 使用的代码是从第19行开始的<br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;__cdecl&nbsp;memcpy&nbsp;(<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;dst,<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;src,<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;size_t&nbsp;count<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)<br><img id=Codehighlighter1_101_309_Open_Image onclick="this.style.display='none'; Codehighlighter1_101_309_Open_Text.style.display='none'; Codehighlighter1_101_309_Closed_Image.style.display='inline'; Codehighlighter1_101_309_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_101_309_Closed_Image onclick="this.style.display='none'; Codehighlighter1_101_309_Closed_Text.style.display='none'; Codehighlighter1_101_309_Open_Image.style.display='inline'; Codehighlighter1_101_309_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_101_309_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_101_309_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;ret&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;dst;<br><img id=Codehighlighter1_153_286_Open_Image onclick="this.style.display='none'; Codehighlighter1_153_286_Open_Text.style.display='none'; Codehighlighter1_153_286_Closed_Image.style.display='inline'; Codehighlighter1_153_286_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_153_286_Closed_Image onclick="this.style.display='none'; Codehighlighter1_153_286_Closed_Text.style.display='none'; Codehighlighter1_153_286_Open_Image.style.display='inline'; Codehighlighter1_153_286_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">&nbsp;(count</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">)&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_153_286_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_153_286_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)dst&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)src;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dst&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;(</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)dst&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;(</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)src&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">(ret);<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span></div>
为了可以进入 中进行调试<br>我使用以下代码调试到memcpy中&nbsp;<br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;__cdecl&nbsp;memcpy&nbsp;(<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;dst,<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;src,<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;size_t&nbsp;count<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)<br><img id=Codehighlighter1_117_323_Open_Image onclick="this.style.display='none'; Codehighlighter1_117_323_Open_Text.style.display='none'; Codehighlighter1_117_323_Closed_Image.style.display='inline'; Codehighlighter1_117_323_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_117_323_Closed_Image onclick="this.style.display='none'; Codehighlighter1_117_323_Closed_Text.style.display='none'; Codehighlighter1_117_323_Open_Image.style.display='inline'; Codehighlighter1_117_323_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_117_323_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_117_323_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;ret&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;dst;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"><br><img id=Codehighlighter1_169_300_Open_Image onclick="this.style.display='none'; Codehighlighter1_169_300_Open_Text.style.display='none'; Codehighlighter1_169_300_Closed_Image.style.display='inline'; Codehighlighter1_169_300_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_169_300_Closed_Image onclick="this.style.display='none'; Codehighlighter1_169_300_Closed_Text.style.display='none'; Codehighlighter1_169_300_Open_Image.style.display='inline'; Codehighlighter1_169_300_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">&nbsp;(count</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">)&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_169_300_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_169_300_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)dst&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)src;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dst&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;(</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)dst&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;(</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)src&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">(ret);<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br><img id=Codehighlighter1_339_441_Open_Image onclick="this.style.display='none'; Codehighlighter1_339_441_Open_Text.style.display='none'; Codehighlighter1_339_441_Closed_Image.style.display='inline'; Codehighlighter1_339_441_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_339_441_Closed_Image onclick="this.style.display='none'; Codehighlighter1_339_441_Closed_Text.style.display='none'; Codehighlighter1_339_441_Open_Image.style.display='inline'; Codehighlighter1_339_441_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_339_441_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_339_441_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;src&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Hello&nbsp;World</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;dst&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Hello&nbsp;World&nbsp;Again!</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">memcpy(dst,src,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(src));<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span></div>
最后发现错误出在memcpy函数中
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000">*</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)dst&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)src;</span></div>
为什么呢？<br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;dst&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Hello&nbsp;World&nbsp;Again!</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;</span></div>
这里dst是在文字常量区分配一个字符串常量，再把str1指向它<br>等于就是<br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;dst&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Hello&nbsp;World&nbsp;Again!</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;</span></div>
所以这里已给字符指针所指向的字符变量赋值就出错。<br>如果要想使以上代码顺利运行 可以将main中的代码改为如下代码<br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br><img id=Codehighlighter1_12_106_Open_Image onclick="this.style.display='none'; Codehighlighter1_12_106_Open_Text.style.display='none'; Codehighlighter1_12_106_Closed_Image.style.display='inline'; Codehighlighter1_12_106_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_12_106_Closed_Image onclick="this.style.display='none'; Codehighlighter1_12_106_Closed_Text.style.display='none'; Codehighlighter1_12_106_Open_Image.style.display='inline'; Codehighlighter1_12_106_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_12_106_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_12_106_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;src&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Hello&nbsp;World</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;dst&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">[</span><span style="COLOR: #000000">20</span><span style="COLOR: #000000">];<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">memcpy(dst,src,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(src));<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span></div>
或者<br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br><img id=Codehighlighter1_12_94_Open_Image onclick="this.style.display='none'; Codehighlighter1_12_94_Open_Text.style.display='none'; Codehighlighter1_12_94_Closed_Image.style.display='inline'; Codehighlighter1_12_94_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_12_94_Closed_Image onclick="this.style.display='none'; Codehighlighter1_12_94_Closed_Text.style.display='none'; Codehighlighter1_12_94_Open_Image.style.display='inline'; Codehighlighter1_12_94_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_12_94_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_12_94_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;src&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Hello&nbsp;World</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;dst[</span><span style="COLOR: #000000">20</span><span style="COLOR: #000000">];<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">memcpy(dst,src,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(src));<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span></div>
<img src ="http://www.cppblog.com/koople/aggbug/134858.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/koople/" target="_blank">Mr.Johnson</a> 2010-11-27 23:44 <a href="http://www.cppblog.com/koople/archive/2010/11/27/134858.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>