锘??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲精品WWW久久久久久,久久久这里有精品,国产精品成人99久久久久http://www.shnenglu.com/Marcky/archive/2009/12/06/102666.htmlMarckyMarckySun, 06 Dec 2009 14:01:00 GMThttp://www.shnenglu.com/Marcky/archive/2009/12/06/102666.htmlhttp://www.shnenglu.com/Marcky/comments/102666.htmlhttp://www.shnenglu.com/Marcky/archive/2009/12/06/102666.html#Feedback0http://www.shnenglu.com/Marcky/comments/commentRss/102666.htmlhttp://www.shnenglu.com/Marcky/services/trackbacks/102666.html鍙戜俊浜? nichloas (nil), 淇″尯: CPlusPlus
鏍? 棰? [FAQ] 浜岀淮鏁扮粍new灝忕粨
鍙戜俊绔? BBS 姘存湪娓呭崕绔?(Sat Jun  7 15:27:42 2003), 杞俊

1.
    A (*ga)[n] = new A[m][n];
    ...
    delete []ga;
緙虹偣錛歯蹇呴』鏄凡鐭?/div>
浼樼偣錛氳皟鐢ㄧ洿瑙傦紝榪炵畫鍌ㄥ瓨錛岀▼搴忕畝媧?緇忚繃嫻嬭瘯錛屾瀽鏋勫嚱鏁拌兘姝g‘璋冪敤)

2.  A** ga = new A*[m];
    for(int i = 0; i < m; i++)
        ga[i] = new A[n];
    ...
    for(int i = 0; i < m; i++)
        delete []ga[i];
    delete []ga;
緙虹偣錛氶潪榪炵畫鍌ㄥ瓨錛岀▼搴忕儲鐞愶紝ga涓篈**綾誨瀷
浼樼偣錛氳皟鐢ㄧ洿瑙傦紝n鍙互涓嶆槸宸茬煡

3.  A* ga = new A[m*n];
    ...
    delete []ga;
緙虹偣錛氳皟鐢ㄤ笉澶熺洿瑙?/div>
浼樼偣錛氳繛緇偍瀛橈紝n鍙互涓嶆槸宸茬煡

4.  vector<vector<A> > ga;
    ga.resize(m);                       //榪欎笁琛屽彲鐢ㄥ彲涓嶇敤
    for(int i = 1; i < n; i++)          //
        ga[i].resize(n);                //
    ...

緙虹偣錛氶潪榪炵畫鍌ㄥ瓨錛岃皟璇曚笉澶熸柟渚匡紝緙栬瘧閫熷害涓嬮檷錛岀▼搴忚啫鑳(瀹為檯閫熷害宸埆涓嶅ぇ)
浼樼偣錛氳皟鐢ㄧ洿瑙傦紝鑷姩鏋愭瀯涓庨噴鏀懼唴瀛橈紝鍙互璋冪敤stl鐩稿叧鍑芥暟錛屽姩鎬佸闀?/div>

5.  vector<A> ga;
    ga.resize(m*n);
鏂規硶3,4鐨勭粨鍚?/div>


6. 2鐨勬敼榪涚増(Penrose鎻愪緵錛屽湪姝ゆ劅璋?
    A** ga = new A*[m];
    ga[0] = new A[m*n];
    for(int i = 1; i < m; i++)
        ga[i] = ga[i-1]+n;
    ...
    delete [] ga[0];
    delete [] ga;
緙虹偣錛氱▼搴忕儲鐞愶紝ga涓篈**綾誨瀷
浼樼偣錛氳繛緇偍瀛橈紝璋冪敤鐩磋錛宯鍙互涓嶆槸宸茬煡



闄勶細1,2,3,6榪樻湁瀵瑰簲鐨刴alloc-free鐗堟湰
涓漢鎺ㄨ崘1鍜?,2鍙互鐢?鏉ヤ唬鏇匡紝3,5璋冪敤澶儲鐞愶紝姣曠珶婧愮▼搴忔槸鎷挎潵鐪嬬殑
涓嶆槸鎷挎潵榪愯鐨?/div>


涓嬮潰鏄竴浜涢敊璇拰娌℃垚鍔熺殑鐗堟湰

1. A* ga = new A[m][n];
  蹇呯劧閿欒

2. vector<A[n]> ga;
   ga.resize(m);

   gcc 3.2涓嬬紪璇戝け璐ワ紝涓嶇煡閬撳叾瀹冪紪璇戝櫒鏁堟灉濡備綍
   涔熶笉鐭ラ亾鏍囧噯鏄惁鍏佽

鎴戠煡閬撶殑灝辮繖浜涳紝嬈㈣繋澶у琛ュ厖錛屾寚姝?/div>




--



--

鈥?鏉ユ簮:·BBS 姘存湪娓呭崕绔?smth.org·[FROM: 162.105.216.213]
鈥?淇敼:·devilphoenix 浜?Sep  5 18:10:57 淇敼鏈枃·[FROM: 211.99.222.*]



Marcky 2009-12-06 22:01 鍙戣〃璇勮
]]>HuffmanTree鐨勫垱寤哄強緙栫爜http://www.shnenglu.com/Marcky/archive/2009/09/28/97474.htmlMarckyMarckyMon, 28 Sep 2009 09:49:00 GMThttp://www.shnenglu.com/Marcky/archive/2009/09/28/97474.htmlhttp://www.shnenglu.com/Marcky/comments/97474.htmlhttp://www.shnenglu.com/Marcky/archive/2009/09/28/97474.html#Feedback0http://www.shnenglu.com/Marcky/comments/commentRss/97474.htmlhttp://www.shnenglu.com/Marcky/services/trackbacks/97474.htmltypedef struct _HTNode {
    unsigned 
int weight;        /* 鏉冨?nbsp;*/
    unsigned 
int parent;        /* 鐖惰妭鐐圭儲寮?nbsp;*/
    unsigned 
int lchild;        /* 宸﹀瀛愮儲寮?nbsp;*/
    unsigned 
int rchild;        /* 鍙沖瀛愮儲寮?nbsp;*/
} HTNode, 
*HuffmanTree;         /* 鍔ㄦ佸垎閰嶆暟緇勫瓨鍌ㄥ搱澶浖鏍?nbsp;*/

typedef 
char **HuffmanCode;     /* 鍔ㄦ佸垎閰嶆暟緇勫瓨鍌ㄥ搱澶浖緙栫爜琛?nbsp;*/

/* 浠巋t鐨?锝瀗鐨勮妭鐐逛腑鎵懼嚭鏉冨兼渶灝忕殑涓や釜鑺傜偣錛屽垎鍒瓨浜巗1, s2涓?nbsp;*/
void Select(HuffmanTree ht, int n, int *s1, int *s2)
{
    
int i;

    
*s1 = 0;
    
*s2 = 0;
    
/*璁劇疆s1, s2鍒板紑濮嬩袱涓猵arent絳変簬0鐨勮妭鐐逛綅緗?/span>*/
    
for (i = 1; i <= n; ++i) {
        
if (*s1 != 0 && *s2 != 0)
            
break;
       
        
if (ht[i].parent == 0)
            
*s1 == 0 ? *s1 = i : *s2 = i;
    }
    
/*鎵懼嚭ht涓璸arent絳変簬0錛屼笖鏉冨兼渶灝忕殑涓や釜鑺傜偣浣嶇疆錛屽垎鍒瓨浜巗1, s2涓?/span>*/
    
for ( ; i <= n; ++i) {
        
if (ht[i].parent != 0continue;

        
if ( (ht[*s1].weight > ht[*s2].weight) && (ht[*s1].weight > ht[i].weight))
            
*s1 = i;
        
else if ( (ht[*s2].weight > ht[*s1].weight) && (ht[*s2].weight > ht[i].weight))
            
*s2 = i;
    }
}

/* 閫氳繃w瀛樺偍鐨刵涓潈鍊鹼紝鏉ュ垱寤轟竴棰楀搱澶浖鏍? ht_ptr鎸囧悜榪欓鍝堝か鏇兼爲 */
void CreateHuffmanTree(HuffmanTree *ht_ptr, int *w, int n)
{
    
int m;
    
int i;
    
int s1, s2;
    HuffmanTree p;
    
    
if (n <= 1return;
    m 
= 2 * n - 1;              /* n涓瓧絎︼紝闇瑕?n-1涓┖闂存潵瀛樺偍鏁撮huffman tree */
    
*ht_ptr = (HuffmanTree)malloc( (m + 1* sizeof(HTNode)); /* 0鍙峰崟鍏冧笉鐢?nbsp;*/

    
for (p = *ht_ptr + 1, i = 1; i <= n; ++i, ++p, ++w) { /* 鍒濆鍖栨暟緇勪腑鍓峮涓崟鍏冨瓨鍌ㄧ殑瀛楃 */
        p
->weight = *w;
        p
->parent = 0;
        p
->lchild = 0;
        p
->rchild = 0;
    }
    
for ( ; i <= m; ++i, ++p) { /* 鍒濆鍖栨暟緇勪腑鍓╀綑鐨勫崟鍏?nbsp;*/
        p
->weight = 0;
        p
->parent = 0;
        p
->lchild = 0;
        p
->rchild = 0;
    }

    
for (i = n + 1; i <= m; ++i) {
        Select(
*ht_ptr, i - 1&s1, &s2);
        
/* 璁劇疆s1, s2鐨勭埗浜蹭負i */
        (
*ht_ptr + s1)->parent = i;
        (
*ht_ptr + s2)->parent = i;
        
/* 璁劇疆i鐨勫乏瀛╁瓙涓簊1, 鍙沖瀛愪負s2 */
        (
*ht_ptr + i)->lchild = s1;
        (
*ht_ptr + i)->rchild = s2;
        
/* 璁劇疆i鐨勬潈鍊間負s1, s2涔嬪拰 */
        (
*ht_ptr + i)->weight = (*ht_ptr + s1)->weight + (*ht_ptr + s2)->weight;
    }
}

/* 瀵筯t_ptr瀛樺偍鐨勫搱澶浖鏍戠殑n涓彾瀛愯妭鐐硅繘琛屽搱澶浖緙栫爜 */
void HuffmanCoding(HuffmanTree *ht_ptr, HuffmanCode *hc_ptr, int n)
{
    
int i;
    
int start;
    
char *cd = NULL;
    
    
*hc_ptr = (HuffmanCode)malloc( (n + 1* sizeof(char *));

    cd 
= (char *)malloc(n * sizeof(char));
    cd[n 
- 1= '\0';

    
for (i = 1; i <= n; ++i) {
        start 
= n - 1;

        
int current, father;
        
for (current = i, father = (*ht_ptr + i)->parent; /* 浠庡彾瀛愯妭鐐瑰紑濮嬶紝騫跺彇寰楃埗鑺傜偣father */
             father 
!= 0;                                 /* 鐖惰妭鐐逛負0鏃跺強鍒拌揪浜嗘牴鑺傜偣 */
             current 
= father, father = (*ht_ptr + father)->parent) { /* 閫愭笎鍚戞牴鑺傜偣闈犳嫝 */
            
if ( (*ht_ptr + father)->lchild == current) /* 褰撳墠鑺傜偣涓哄乏瀛╁瓙 */
                cd[
--start] = '0';
            
else
                cd[
--start] = '1';

        }

        
*(*hc_ptr + i) = (char *)malloc( (n - start) * sizeof(char));
        strcpy(
*(*hc_ptr +i), &cd[start]);
    }
    free(cd);
}




Marcky 2009-09-28 17:49 鍙戣〃璇勮
]]>浜屽弶鏍戠殑鍒涘緩鍙婇亶鍘嗭紙閫掑綊浠g爜錛?/title><link>http://www.shnenglu.com/Marcky/archive/2009/09/24/97173.html</link><dc:creator>Marcky</dc:creator><author>Marcky</author><pubDate>Thu, 24 Sep 2009 14:33:00 GMT</pubDate><guid>http://www.shnenglu.com/Marcky/archive/2009/09/24/97173.html</guid><wfw:comment>http://www.shnenglu.com/Marcky/comments/97173.html</wfw:comment><comments>http://www.shnenglu.com/Marcky/archive/2009/09/24/97173.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/Marcky/comments/commentRss/97173.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/Marcky/services/trackbacks/97173.html</trackback:ping><description><![CDATA[<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #0000ff;">typedef enum</span><span style="color: #000000;"> _STATUS {ERROR, OK} STATUS;<br><br>typedef </span><span style="color: #0000ff;">struct</span><span style="color: #000000;"> _BiTNode {<br>    </span><span style="color: #0000ff;">char</span><span style="color: #000000;"> data;<br>    </span><span style="color: #0000ff;">struct</span><span style="color: #000000;"> _BiTNode </span><span style="color: #000000;">*</span><span style="color: #000000;">lchild;<br>    </span><span style="color: #0000ff;">struct</span><span style="color: #000000;"> _BiTNode </span><span style="color: #000000;">*</span><span style="color: #000000;">rchild;<br>} BiTNode, </span><span style="color: #000000;">*</span><span style="color: #000000;">BiTree;<br><br></span><span style="color: #008000;">/*</span><span style="color: #008000;">鍒涘緩浜屽弶鏍?/span><span style="color: #008000;">*/</span><span style="color: #000000;"><br>STATUS CreateBiTree(BiTree </span><span style="color: #000000;">*</span><span style="color: #000000;">T)<br>{</span><span style="color: #008000;">/*</span><span style="color: #008000;">鎸夊厛搴忔搴忚緭鍏ヤ簩鍙夋爲鑺傜偣鐨勫鹼紝絀烘牸琛ㄧず絀烘爲銆?/span><span style="color: #008000;">*/</span><span style="color: #000000;"><br>    </span><span style="color: #0000ff;">char</span><span style="color: #000000;"> ch;<br>    <br>    scanf(</span><span style="color: #000000;">"</span><span style="color: #000000;">%c</span><span style="color: #000000;">"</span><span style="color: #000000;">, </span><span style="color: #000000;">&</span><span style="color: #000000;">ch);<br>    </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (ch </span><span style="color: #000000;">==</span><span style="color: #000000;"> </span><span style="color: #000000;">'</span><span style="color: #000000;"> </span><span style="color: #000000;">'</span><span style="color: #000000;">) { <br>        </span><span style="color: #000000;">*</span><span style="color: #000000;">T </span><span style="color: #000000;">=</span><span style="color: #000000;"> NULL;<br>    } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {<br>        </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> ( </span><span style="color: #000000;">!</span><span style="color: #000000;">(</span><span style="color: #000000;">*</span><span style="color: #000000;">T </span><span style="color: #000000;">=</span><span style="color: #000000;"> (BiTNode </span><span style="color: #000000;">*</span><span style="color: #000000;">)malloc(</span><span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(BiTNode)))) exit(</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">);<br>        (</span><span style="color: #000000;">*</span><span style="color: #000000;">T)</span><span style="color: #000000;">-></span><span style="color: #000000;">data </span><span style="color: #000000;">=</span><span style="color: #000000;"> ch;                     </span><span style="color: #008000;">//</span><span style="color: #008000;">鐢熸垚鏍硅妭鐐?/span><span style="color: #008000;"><br></span><span style="color: #000000;">        CreateBiTree(</span><span style="color: #000000;">&</span><span style="color: #000000;">((</span><span style="color: #000000;">*</span><span style="color: #000000;">T)</span><span style="color: #000000;">-></span><span style="color: #000000;">lchild));       </span><span style="color: #008000;">//</span><span style="color: #008000;">鏋勯犲乏瀛愭爲</span><span style="color: #008000;"><br></span><span style="color: #000000;">        CreateBiTree(</span><span style="color: #000000;">&</span><span style="color: #000000;">((</span><span style="color: #000000;">*</span><span style="color: #000000;">T)</span><span style="color: #000000;">-></span><span style="color: #000000;">rchild));       </span><span style="color: #008000;">//</span><span style="color: #008000;">鏋勯犲彸瀛愭爲</span><span style="color: #008000;"><br></span><span style="color: #000000;">    }<br>    </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> OK;<br>}<br><br></span><span style="color: #008000;">/*</span><span style="color: #008000;">涓簭閬嶅巻浜屽弶鏍?/span><span style="color: #008000;">*/</span><span style="color: #000000;"><br>STATUS InOrderTraverse(BiTree </span><span style="color: #000000;">*</span><span style="color: #000000;">T)<br>{<br>    </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (</span><span style="color: #000000;">*</span><span style="color: #000000;">T) {<br>        </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (InOrderTraverse(</span><span style="color: #000000;">&</span><span style="color: #000000;">((</span><span style="color: #000000;">*</span><span style="color: #000000;">T)</span><span style="color: #000000;">-></span><span style="color: #000000;">lchild)))<br>            printf(</span><span style="color: #000000;">"</span><span style="color: #000000;">%c </span><span style="color: #000000;">"</span><span style="color: #000000;">, (</span><span style="color: #000000;">*</span><span style="color: #000000;">T)</span><span style="color: #000000;">-></span><span style="color: #000000;">data);<br>            </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (InOrderTraverse(</span><span style="color: #000000;">&</span><span style="color: #000000;">((</span><span style="color: #000000;">*</span><span style="color: #000000;">T)</span><span style="color: #000000;">-></span><span style="color: #000000;">rchild)))<br>                </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> OK;<br>        </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> ERROR;<br>    } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {<br>        </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> OK;<br>    }<br>}</span></div> <br> <img src ="http://www.shnenglu.com/Marcky/aggbug/97173.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/Marcky/" target="_blank">Marcky</a> 2009-09-24 22:33 <a href="http://www.shnenglu.com/Marcky/archive/2009/09/24/97173.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>Python鐨勫鍏冭祴鍊兼柟寮忓疄鐜頒氦鎹袱涓彉閲忕殑鍊?/title><link>http://www.shnenglu.com/Marcky/archive/2009/08/26/94473.html</link><dc:creator>Marcky</dc:creator><author>Marcky</author><pubDate>Wed, 26 Aug 2009 08:44:00 GMT</pubDate><guid>http://www.shnenglu.com/Marcky/archive/2009/08/26/94473.html</guid><wfw:comment>http://www.shnenglu.com/Marcky/comments/94473.html</wfw:comment><comments>http://www.shnenglu.com/Marcky/archive/2009/08/26/94473.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/Marcky/comments/commentRss/94473.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/Marcky/services/trackbacks/94473.html</trackback:ping><description><![CDATA[鍒╃敤Python鐨勫鍏冭祴鍊兼柟寮忓彲浠ユ棤欏諱復鏃朵腑闂村彉閲忓疄鐜頒袱涓彉閲忓肩殑浜ゆ崲銆?br>浠g爜錛?br> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #000000;">(x, y) </span><span style="color: #000000;">=</span><span style="color: #000000;"> (</span><span style="color: #000000;">1</span><span style="color: #000000;">, </span><span style="color: #000000;">2</span><span style="color: #000000;">) </span><span style="color: #008000;">#</span><span style="color: #008000;">x = 1, y = 2</span><span style="color: #008000;"><br></span><span style="color: #000000;">(x, y) </span><span style="color: #000000;">=</span><span style="color: #000000;"> (y, x) </span><span style="color: #008000;">#</span><span style="color: #008000;">x = 2, y = 1</span></div> <br><img src ="http://www.shnenglu.com/Marcky/aggbug/94473.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/Marcky/" target="_blank">Marcky</a> 2009-08-26 16:44 <a href="http://www.shnenglu.com/Marcky/archive/2009/08/26/94473.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>system V IPC 鈥斺?鍏變韓鍐呭瓨(涓?http://www.shnenglu.com/Marcky/archive/2009/08/20/93911.htmlMarckyMarckyThu, 20 Aug 2009 06:57:00 GMThttp://www.shnenglu.com/Marcky/archive/2009/08/20/93911.htmlhttp://www.shnenglu.com/Marcky/comments/93911.htmlhttp://www.shnenglu.com/Marcky/archive/2009/08/20/93911.html#Feedback0http://www.shnenglu.com/Marcky/comments/commentRss/93911.htmlhttp://www.shnenglu.com/Marcky/services/trackbacks/93911.html
浠g爜濡備笅錛?br>
#include <stdio.h>
#include 
<stdlib.h>
#include 
<string.h>

#include 
<unistd.h>
#include 
<sys/types.h>

#include 
<sys/wait.h>
#include 
<fcntl.h>

#include 
<sys/ipc.h>
#include 
<sys/shm.h>

#define SHM_SIZE 1024            /* shared memory size(bytes) */
#define SHM_MODE 0600            /* user read/write */

#define WRITE_LOCK(fd) regLock((fd), F_SETLKW, F_WRLCK, 0, SEEK_SET, 0) 
#define UN_LOCK(fd) regLock((fd), F_SETLK, F_UNLCK, 0, SEEK_SET, 0) 
/* 鍒涘緩涓涓枃浠?nbsp;*/
int createFile()
{
    
int fd;

    
if ( (fd = open("/tmp/emptyfile4shm", O_RDWR | O_CREAT, 0666)) < 0) {
        fprintf(stderr, 
"Create a empty file failed!\n");
        exit(EXIT_FAILURE);
    }

    
return fd;
}
/* 鍦ㄦ枃浠秄d涓婂姞閿佹垨瑙i攣*/
int regLock(int fd, int cmd, int type, off_t offset, int whence, off_t len)
{
    
struct flock lock;

    
lock.l_type = type;         /* lock type: F_RDLCK, F_WRLCK, F_UNLCK */
    
lock.l_start = offset;      /* byte offset relative to l_whence */
    
lock.l_whence = whence;    /* it's value : SEEK_SET, SEEK_CUR, SEEK_END */
    
lock.l_len = len;           /* bytes (0 means to EOF) */

    
return fcntl(fd, cmd, &lock);
}

int main(void)
{
    
int shmid;
    
int filed;
    pid_t pid;
    
char *shmptr = NULL;

    filed 
= createFile();

    
if ( (shmid = shmget(IPC_PRIVATE, SHM_SIZE, SHM_MODE)) < 0) { /* Create shared memory */
        fprintf(stderr, 
"Create shared memory failed!\n");
        exit(EXIT_FAILURE);
    }

    
if ( (pid = fork()) < 0) {  /* Create a child process */
        fprintf(stderr, 
"Create child process failed!\n");
        exit(EXIT_FAILURE);
    }

    
if (pid == 0) {             /* In child process */
        
if ( (shmptr = shmat(shmid, 00)) == (void *)-1) {
            fprintf(stderr, 
"Attached shared memory failed!\n");
            exit(EXIT_FAILURE);
        }
        
while (1) {
            WRITE_LOCK(filed);  
/* add a write lock to filed */
            
/* shared memory 鍓╀綑鐨勭┖闂翠笉鑳藉瓨鍌?Child"瀛楃涓插拰緇撴潫絎?鏃跺氨閫鍑哄驚鐜?nbsp;*/
            
if (SHM_SIZE - strlen(shmptr) < strlen("Child"+ 1break
            strcat(shmptr, 
"C"); /* 鐢變簬鍔犱簡閿侊紝姣忎釜Child灝嗕細榪炵畫鍑虹幇 */
            strcat(shmptr, 
"h");
            strcat(shmptr, 
"i");
            strcat(shmptr, 
"l");
            strcat(shmptr, 
"d");
            UN_LOCK(filed);     
/* release lock */
        }
        printf(
"child process:\n\t%s\n", shmptr); /* child process print shared memory */
            
        exit(
0);
    }

    
/* In parent process */
    
if ( (shmptr = shmat(shmid, 00)) == (void *)-1) { /* Attached shared memory */
        fprintf(stderr, 
"Attached shared memory failed!\n");
        exit(EXIT_FAILURE);
    }
    
    
while (1) {
        WRITE_LOCK(filed);
        
/* shared memory 鍓╀綑鐨勭┖闂翠笉鑳藉瓨鍌?Parent"瀛楃涓插拰緇撴潫絎?鏃跺氨閫鍑哄驚鐜?nbsp;*/
        
if (SHM_SIZE - strlen(shmptr) < strlen("Parent"+ 1break;
        strcat(shmptr, 
"P");/* 鐢變簬鍔犱簡閿侊紝姣忎釜Parent灝嗕細榪炵畫鍑虹幇 */
        strcat(shmptr, 
"a");
        strcat(shmptr, 
"r");
        strcat(shmptr, 
"e");
        strcat(shmptr, 
"n");
        strcat(shmptr, 
"t");
        UN_LOCK(filed);
    }
    printf(
"parent process:\n\t%s\n", shmptr); /* parent print shared memory */

    wait(
0);
    exit(
0);
}
    
            
    
    




Marcky 2009-08-20 14:57 鍙戣〃璇勮
]]>
system V IPC 鈥斺?鍏變韓鍐呭瓨(涓?http://www.shnenglu.com/Marcky/archive/2009/08/20/93909.htmlMarckyMarckyThu, 20 Aug 2009 06:48:00 GMThttp://www.shnenglu.com/Marcky/archive/2009/08/20/93909.htmlhttp://www.shnenglu.com/Marcky/comments/93909.htmlhttp://www.shnenglu.com/Marcky/archive/2009/08/20/93909.html#Feedback0http://www.shnenglu.com/Marcky/comments/commentRss/93909.htmlhttp://www.shnenglu.com/Marcky/services/trackbacks/93909.html
linux鍐呮牳瀹氫箟鐨剆hared memory緇撴瀯shmid_ds濡備笅錛?br>
struct shmid_ds {
    
struct ipc_perm  shm_perm;  /*鏉冮檺*/
    size_t           shm_segsz; 
/*澶у皬*/
    pid_t            shm_lpid;  
    pid_t            shm_cpid;  
/*鍒涘緩鑰卲id*/
    shmatt_t         shm_nattch;
/*榪炴帴鍒版孌靛唴瀛樼殑榪涚▼鏁?/span>*/
    time_t           shm_atime;
    time_t           shm_dtime;
    time_t           shm_ctime;
    
};

1銆佸垱寤烘垨浣跨敤涓孌靛叡浜唴瀛樹嬌鐢╯hmget鍑芥暟錛屾鍑芥暟灝嗚繑鍥炲叡浜唴瀛樻爣紺虹銆?br>
#include <sys/shm.h>
int shmget(key_t key, size_t size, int flag);
濡傛灉key鍙栧間負IPC_PRIVATE鎴栬卥ey褰撳墠涓哄拰鐗瑰畾綾誨瀷鐨処PC緇撴瀯鐩哥粨鍚堬紝騫朵笖flag鎸囧畾浜咺PC_CREAT浣嶏紝鍒欏垱寤轟竴涓柊鐨剆hare memory緇撴瀯銆?br>size涓哄叡浜唴瀛樻鐨勯暱搴︼紙瀛楄妭錛夈?br>
2銆佸涓涓叡浜唴瀛樻榪涜鎿嶄綔浣跨敤shmctl銆?br>
#include <sys/shm.h>
int shmctl(int shmid, int cmd, struct shmid_ds *buf);
shmid鎸囧畾闇瑕佹搷浣滅殑shared memory
cmd鎸囧畾闇瑕佽繘琛岀殑鎿嶄綔
       IPC_STAT鍙栧緱姝ゆ鐨剆hmid_ds緇撴瀯鏀懼叆buf涓?br>       IPC_SET鐢╞uf鐨勫艱緗孌典腑鐨勶細shm_perm.uid錛宻hm_perm.gid錛宻hm_perm.mode銆?br>       IPC_RMID浠庣郴緇熶腑鍒犻櫎姝ゅ叡浜唴瀛樻銆?br>       SHM_LOCK灝嗗叡浜唴瀛橀攣瀹氬埌鍐呭瓨涓?br>       SHM_UNLOCK瑙i攣鍏變韓鍐呭瓨孌點?br>
3銆佸皢涓涓叡浜唴瀛樻榪炴帴鍒拌嚜宸辯殑鍦板潃絀洪棿浣跨敤shmat錛?br>
#include <sys/shm.h>
void *shmat(int shmid, const void *addr, int flag);
鎺ㄨ崘addr鍙?鍊鹼紝灝嗘孌佃繛鎺ュ埌鍐呮牳閫夋嫨鐨勭涓涓彲鐢ㄧ殑鍦板潃涓娿傚鍔犵▼搴忕殑鍙Щ妞嶆с?br>
4銆佸鍏變韓鍐呭瓨鎿嶄綔緇撴潫鍚庯紝瑕佽劚紱昏孌電敤shmdt錛?br>
#include <sys/shm.h>
int shmdt(void *addr);
addr鏄痵hmat鐨勮繑鍥炲箋?br>


Marcky 2009-08-20 14:48 鍙戣〃璇勮
]]>
瀹夊叏璁塊棶鏁扮粍鐨勬寚閽堢被妯℃澘http://www.shnenglu.com/Marcky/archive/2009/08/13/93233.htmlMarckyMarckyThu, 13 Aug 2009 10:29:00 GMThttp://www.shnenglu.com/Marcky/archive/2009/08/13/93233.htmlhttp://www.shnenglu.com/Marcky/comments/93233.htmlhttp://www.shnenglu.com/Marcky/archive/2009/08/13/93233.html#Feedback0http://www.shnenglu.com/Marcky/comments/commentRss/93233.htmlhttp://www.shnenglu.com/Marcky/services/trackbacks/93233.html
#include <iostream>
#include 
<stdexcept>

using namespace std;

template
<typename T>
class Ptr2T {
public:
//鏋勯犲嚱鏁幫紝褰㈠弬涓烘暟緇勮搗濮嬪湴鍧鍜屽ぇ灝?/span>
    Ptr2T(T *p, int size)
        : m_p(p), m_array(p), m_size(size) { };

    Ptr2T
& operator++();                //鍓嶇紑++
    const Ptr2T operator++(int);        //鍚庣紑++

    Ptr2T
& operator--();                //鍓嶇紑--
    const Ptr2T operator--(int);        //鍚庣紑--

    Ptr2T
& operator+=(int n);
    Ptr2T
& operator -=(int n);
//瀹夊叏鐨勬暟緇勫厓绱犺闂搷浣?/span>
    T& operator*() const;  
private:
    T 
*m_p;           //璁塊棶鏁扮粍鐨勬寚閽?/span>
    T *m_array;       //淇濆瓨鏁扮粍鐨勮搗濮嬪湴鍧
    int m_size;       //淇濆瓨鏁扮粍鐨勫ぇ灝?/span>
};

template
<typename T>
inline Ptr2T
<T>& Ptr2T<T>::operator++()
{
    m_p 
+= 1;
    
return *this;
}

template
<typename T>
inline 
const Ptr2T<T> Ptr2T<T>::operator++(int)
{
    Ptr2T current 
= *this;
    
++(*this);       //鐢ㄩ噸杞界殑鍓嶇紑++鏉ュ疄鐜?/span>
    
    
return current;
}

template
<typename T>
inline Ptr2T
<T>& Ptr2T<T>::operator--()
{
    m_p 
-= 1;
    
return *this;
}

template
<typename T>
inline 
const Ptr2T<T> Ptr2T<T>::operator--(int)
{
    Ptr2T current 
= *this;
    
--(*this);       //鐢ㄩ噸杞界殑鍓嶇紑--鏉ュ疄鐜?/span>

    
return current;
}

template
<typename T>
inline T
& Ptr2T<T>::operator*() const
{
    
if (m_p < m_array || m_p > m_array + m_size - 1) {  //瓚婄晫媯鏌?/span>
        throw out_of_range("out of range");
    }

    
return *m_p;
}

template
<typename T>
inline Ptr2T
<T>& Ptr2T<T>::operator+=(int n)
{
    m_p 
+= n;
    
return *this;
}

template
<typename T>
inline Ptr2T
<T>& Ptr2T<T>::operator-=(int n)
{
    m_p 
-= n;
    
return *this;
}

template
<typename T>
Ptr2T
<T> operator+(const Ptr2T<T> &p, const int n)
{
    
return Ptr2T<T>(p) += n;   //鐢ㄩ噸杞界殑+=鏉ュ疄鐜?/span>
}

template
<typename T>
Ptr2T
<T> operator+(const int n, const Ptr2T<T> &p)
{
    
return p + n;
}

template
<typename T>
Ptr2T
<T> operator-(const Ptr2T<T> &p, const int n)
{
    
return Ptr2T<T>(p) -= n;  //鐢ㄩ噸杞界殑-=鏉ュ疄鐜?/span>
}

//浣跨敤鏂規硶
int main(void)
{
    
char a[5= {'a''b''c''d''e'};
    
int b[5= {12345}; 

    Ptr2T
<char> pc(a, 5);
    Ptr2T
<int> pi(b, 5);

    cout 
<< *pc++ << endl;
    pi
--;
    pi 
+= 2;
    cout 
<< *(pi - 1<< endl;

    
*++pi = 100;
    cout 
<< *pi << endl;
    
    
return 0;
}



Marcky 2009-08-13 18:29 鍙戣〃璇勮
]]>
鏄劇ず鏋勯犲嚱鏁頒笌杞崲榪愮畻絎︾殑鍚堜綔http://www.shnenglu.com/Marcky/archive/2009/08/13/93193.htmlMarckyMarckyThu, 13 Aug 2009 06:39:00 GMThttp://www.shnenglu.com/Marcky/archive/2009/08/13/93193.htmlhttp://www.shnenglu.com/Marcky/comments/93193.htmlhttp://www.shnenglu.com/Marcky/archive/2009/08/13/93193.html#Feedback0http://www.shnenglu.com/Marcky/comments/commentRss/93193.htmlhttp://www.shnenglu.com/Marcky/services/trackbacks/93193.html
class Year {
    
int m_y;
public:
//explicit闄愬埗int鍒癥ear鐨勯殣寮忚漿鎹?/span>
    explicit Year(int y)
        : y(m_y) { }
//Year鍒癷nt鐨勭被鍨嬭漿鎹?nbsp;  
    operator int() const 
        { 
return m_y; }
    
//other funtion
}


class Date {
public :
    Date(
int d, Month m, Year y);
    
//
};

Date d1(
1987, feb, 21);   //error, 21涓嶈兘闅愬紡杞崲涓篩ear
Date d2(21, feb, Year(1987)); //ok



鍦ㄨ繖閲孻ear灝卞彧鏄寘瑁逛綇浜唅nt錛屽int鎻愪緵涓灞備繚鎶よ屽凡銆傜敱浜巓perator int()鐨勫瓨鍦紝鍙闇瑕侊紝Year鍙互闅愬紡鐨勮漿鍖栦負int鍑虹幇榪愮畻琛ㄨ揪寮忎腑鍙傚姞榪愮畻銆傝岄氳繃緇欐瀯閫犲嚱鏁板0鏄庝負explicit錛屽氨鑳藉淇濊瘉錛宨nt鍒癥ear鐨勮漿鍖栧彧鑳藉湪鏄庣‘鏃犺鐨勬儏鍐佃繘琛岋紝閬垮厤浜嗘剰澶栫殑璧嬪箋?br>
鏄劇ず鏋勯犲嚱鏁板拰杞崲榪愮畻絎︾殑鍚堜綔錛岃Year鍙互褰搃nt浣跨敤錛屽悓鏃跺張瀵筜ear榪涜涓瀹氱殑淇濇姢銆傘傘?br>



Marcky 2009-08-13 14:39 鍙戣〃璇勮
]]>
Allocating Arrays Using Placement new (zz)http://www.shnenglu.com/Marcky/archive/2009/08/13/93110.htmlMarckyMarckyWed, 12 Aug 2009 16:48:00 GMThttp://www.shnenglu.com/Marcky/archive/2009/08/13/93110.htmlhttp://www.shnenglu.com/Marcky/comments/93110.htmlhttp://www.shnenglu.com/Marcky/archive/2009/08/13/93110.html#Feedback0http://www.shnenglu.com/Marcky/comments/commentRss/93110.htmlhttp://www.shnenglu.com/Marcky/services/trackbacks/93110.htmlnew enables you to construct an object or an array of objects at a predetermined memory position. This version is called placement new and has many useful applications, including building a custom-made memory pool or a garbage collector. Additionally, it can be used in mission-critical applications because there's no danger of allocation failure; the memory that's used by placement new has already been allocated. Placement new is also faster because the construction of an object on a preallocated buffer takes less time.

You already know how to use placement new to allocate a single object on a predetermined memory address. However, some programming tasks require the allocation of arrays on a predetermined memory address. Here's how you do it.

Placement new Overview

Mobile devices, embedded systems and custom garbage collectors are only a few instances of programming environments that may require placement new allocation of arrays. Before I discuss the details of such array allocations, let's remind ourselves briefly how scalar (i.e. non-array) placement new works.

The scalar version of placement new takes a user-supplied address on which it constructs a single object. Unlike the ordinary version of the new operator, placement new doesn't allocate storage for the object; it merely constructs the object on the memory address you provide:

#include <new> //required for using placement new
class Widget {
public:
    Widget();
    
virtual ~Widget
    
virtual void Draw();
};
char* buf=new char [sizeof (Widget)];//preallocate
Widget* widget= new(buf) Widget; //construct Widget on buf
widget->Draw(); //use Widget

To destroy widget you first have to invoke its destructor explicitly:

widget->~Widget(); //explicit destructor invocation

Next, reclaim the raw memory like this:

delete[] buf;

Array Allocation

Allocating arrays with placement new follows the same steps more or less, but you have to pay attention to additional nuances. Here is a step-by-step guide:

First, allocate a buffer large enough to hold an array of the desired type:

const int ARRSIZE = 15;
char * buf= new [sizeof(Widget)*ARRSIZE];

Don't be tempted to calculate the size manually; always use sizeof to ensure that the buffer is properly aligned and has the right size.

Next, construct an array of ARRSIZE objects on the buffer using placement new[] :

Widget* widgets=new(buf) Widget[ARRSIZE];//construct an array

You can now use the allocated array as usual:

for (int i=0; i<ARRSIZE; i++)
{
 widgets[i].Draw();
}
Make sure that your target class -- Widget in this example -- has a public default constructor. Otherwise, it would be impossible to create arrays thereof.

Destroying the Array

To destroy such an array allocated by placement new you have to call the destructor for each element explicitly:

int i=ARRSIZE;
while (i)
    widgets[
--i].~Widget();

The while -loop uses a descending order to preserve the canonical destruction order of C++ -- the object that was constructed last must be destroyed first. To comply with this requirement, the element with the highest index is destroyed first.

Finally, you release the raw memory on which the array resided by calling delete[] :

delete[] buf;

Performance Tuning

The array placement new has a potential performance problem: it initializes every element in the array unconditionally. If your app deals with large arrays, this isn't the most efficient way. In some apps only a portion of the array is actually used, and in other apps the elements are assigned a different value immediately after their construction. In these cases, you want to postpone, or even completely avoid, the automatic initialization of array elements. To avoid the initialization of placement new arrays, follow the following steps:

As before, begin with an allocation of a raw buffer with the appropriate size. This time however, use the global operator new instead of the new operator:

Widget * warr=
static_cast
<Widget*> (::operator new ( sizeof(Widget)* ARRSIZE));

The global operator new , very much like C's malloc() , merely allocates raw bytes of memory from the free-store, without initializing them. It returns void * rather than Widget* which is why you need to cast the result explicitly.

At this stage, warr is a pointer to raw memory. You can't access its elements because they haven't been initialized yet. To initialize individual elements, call placement new once more, for each element you want initialized:

void assign(Widget arr[], size_t & sz,  const Widget& init)
{
    
new (&arr[sz++]) Widget (init); //invoke copy ctor
}

assign() passes the address of an individual element to placement new which in turn invokes Widget 's copy constructor. The copy-constructor initializes that element with init . Using this technique, you can initialize elements selectively, leaving the rest of the array uninitialized.

To destroy such an array, invoke the destructor of every initialized object. Then call the global operator delete to reclaim the raw storage:

void destroy(Widget arr[], size_t & sz)
{
    
while (sz)
    {
        arr[
--sz].~Widget();//destroy all initialized elements
    }
     ::
operator delete (arr); //reclaim raw storage
}

Summary

The techniques I've presented here are bug prone. Therefore, they should be encapsulated in higher-level classes that hide the implementation details from users. These techniques aren't rarely-used as they might seem. STL allocators use them under the hood to avoid object initialization and minimize reallocations.





Marcky 2009-08-13 00:48 鍙戣〃璇勮
]]>
涓涓狿ython鏂囨湰澶勭悊紼嬪簭http://www.shnenglu.com/Marcky/archive/2009/07/22/90839.htmlMarckyMarckyWed, 22 Jul 2009 08:33:00 GMThttp://www.shnenglu.com/Marcky/archive/2009/07/22/90839.htmlhttp://www.shnenglu.com/Marcky/comments/90839.htmlhttp://www.shnenglu.com/Marcky/archive/2009/07/22/90839.html#Feedback0http://www.shnenglu.com/Marcky/comments/commentRss/90839.htmlhttp://www.shnenglu.com/Marcky/services/trackbacks/90839.htmlsome
are
born
great  闃呰鍏ㄦ枃

Marcky 2009-07-22 16:33 鍙戣〃璇勮
]]>
热re99久久精品国99热| 亚洲а∨天堂久久精品9966| 99久久这里只精品国产免费| 久久久久一本毛久久久| 一本久道久久综合狠狠躁AV| 久久婷婷色综合一区二区| 精品久久久久久无码专区| 亚洲综合精品香蕉久久网97| 亚洲精品国精品久久99热| 亚洲色欲久久久综合网东京热| 69久久夜色精品国产69| 久久97久久97精品免视看秋霞| 无码伊人66久久大杳蕉网站谷歌| 久久久久AV综合网成人| 午夜精品久久久久久| 99久久免费国产特黄| 久久久亚洲AV波多野结衣| 曰曰摸天天摸人人看久久久| 国内精品久久久久影院薰衣草| 色综合合久久天天综合绕视看| 久久精品久久久久观看99水蜜桃| 99久久国产综合精品成人影院| 亚洲色欲久久久综合网东京热| 伊人久久大香线蕉综合5g| 亚洲国产精品久久久久婷婷软件 | 国产99久久久国产精品小说| 国产成人久久精品激情 | 国产国产成人久久精品| 亚洲国产另类久久久精品| 人妻中文久久久久| 久久丝袜精品中文字幕| 青青青青久久精品国产| 久久精品欧美日韩精品| 色欲综合久久中文字幕网| 亚洲国产精品嫩草影院久久 | 亚洲精品无码久久久| 国产精品免费久久| 久久AⅤ人妻少妇嫩草影院| 亚洲国产精品一区二区久久| 51久久夜色精品国产| 久久亚洲综合色一区二区三区|