﻿<?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++博客-阳光部落-文章分类-Data Structure</title><link>http://www.cppblog.com/kentucky/category/9651.html</link><description /><language>zh-cn</language><lastBuildDate>Tue, 03 Mar 2009 15:25:27 GMT</lastBuildDate><pubDate>Tue, 03 Mar 2009 15:25:27 GMT</pubDate><ttl>60</ttl><item><title>数组形参</title><link>http://www.cppblog.com/kentucky/articles/75318.html</link><dc:creator>易小寒</dc:creator><author>易小寒</author><pubDate>Mon, 02 Mar 2009 05:48:00 GMT</pubDate><guid>http://www.cppblog.com/kentucky/articles/75318.html</guid><wfw:comment>http://www.cppblog.com/kentucky/comments/75318.html</wfw:comment><comments>http://www.cppblog.com/kentucky/articles/75318.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/kentucky/comments/commentRss/75318.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/kentucky/services/trackbacks/75318.html</trackback:ping><description><![CDATA[<span style="FONT-SIZE: 10pt">当使用数组做函数参数时，和其他类型一样，数组形参可定义为引用或非引用。大部分情况下，数组以普通非引用类型传递，这时数组名会自动转化为指针。一般来说，非引用的形参在实际的实参传递过程中会建立一个实参的副本，但是在传递数组的过程中，由于实参是指向数组第一个元素的指针，所以形参复制的是这个指针的值而不是整个数组。</span>&nbsp;<br>&nbsp;&nbsp;<br>&nbsp; // a example <br>&nbsp; void f(int *);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // pointer <br>&nbsp; void f(int array[]);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;// array <br>&nbsp; void f(int array[5]);&nbsp;&nbsp;&nbsp;&nbsp; // array with a size <br><br><span style="FONT-SIZE: 10pt">可知上面的三种定义是等价的。对于后两种定义函数实际操纵的是指针的副本，因此不会修改实参指针的值。但是函数可以通过指针修改所指向的数组元素的值。通过指针形参所做的任何改变都是在修改数组元素本身。不需要修改数组的元素时，应将形参定义为指向const对象的指针：</span> <br><br>&nbsp; // a example <br>&nbsp; void f(const int *);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // pointer to const object <br><br><span style="FONT-SIZE: 10pt">数组形参可定义为引用传递。这样的话，编译器不会将数组转化为指针，而是传递数组的引用。</span> <br><br>&nbsp; // a example <br>&nbsp; void f(int (&amp;array) [5]); 
<img src ="http://www.cppblog.com/kentucky/aggbug/75318.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/kentucky/" target="_blank">易小寒</a> 2009-03-02 13:48 <a href="http://www.cppblog.com/kentucky/articles/75318.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Dynamic Array</title><link>http://www.cppblog.com/kentucky/articles/75187.html</link><dc:creator>易小寒</dc:creator><author>易小寒</author><pubDate>Sat, 28 Feb 2009 10:46:00 GMT</pubDate><guid>http://www.cppblog.com/kentucky/articles/75187.html</guid><wfw:comment>http://www.cppblog.com/kentucky/comments/75187.html</wfw:comment><comments>http://www.cppblog.com/kentucky/articles/75187.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/kentucky/comments/commentRss/75187.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/kentucky/services/trackbacks/75187.html</trackback:ping><description><![CDATA[Some Tips<br><br>1. Be sure to destroy every dynamic array that you created, or you will have a memory leak.<br><br>2. When you use free, always reset your pointers to 0 after freeing, even if you don't plan on using them again.<br><br>3. Never writer over a pointer to an array that you have not destroyed unless you've stored the address of the array somewhere else first.You will end up with memory leak.<br><br>&nbsp; // a example<br>&nbsp; int&nbsp; *pArray = 0;<br>&nbsp; int&nbsp; *temp = 0;<br><br>&nbsp; pArray = (int *)malloc(10 * sizeof(int));<br>&nbsp; <br>&nbsp; temp = pArray;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;// store the address of the array<br>&nbsp; pArray = (int *)realloc(pArray, 20 * sizeof(int));<br>&nbsp;<br>&nbsp; //&nbsp;memory allocation&nbsp;fail<br>&nbsp; if (pArray == 0)<br>&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pArray = temp;<br>&nbsp;&nbsp; }<br><br>4. Always check to see&nbsp; if your memory allocations have not&nbsp; failed.<br><br>5. When deleting an array, always use the bracket notation, or delete will only destroy the first cell and this will lead to memory leak. <br><br><span style="FONT-SIZE: 10pt">Cache Issues<br><br>Arrays are great&nbsp;for being able to access every item in the array randomly, in theory. In reality, arrays aren't actually randomly accessible. This has to do with the way computers actually work. A computer is a complex machine with many lays of memory. The lowest of these lays is called registers, and all processors have a larger memory area almost as fast as the registers called the level 1 cache(L1 cache). There might be other levels of cache as well. So whenever datas needs to be accessed, the processor needs to search for&nbsp;it in one of the memory levels.&nbsp;<br><br>So that's one thing you have to pay attention to when dealing with arrays. Looping through them in a single loop is nice and fast because you use as much of the array as possible at one time. However, an algorithm that randomly jumps to different cells that are far away from each other will be slow because you're causing the processor to move around a great deal of memory. Profiling programs have shown that the processor spends most of its time moving memory around, which is a major optimization concern.</span> 
<img src ="http://www.cppblog.com/kentucky/aggbug/75187.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/kentucky/" target="_blank">易小寒</a> 2009-02-28 18:46 <a href="http://www.cppblog.com/kentucky/articles/75187.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>