??xml version="1.0" encoding="utf-8" standalone="yes"?>国内精品久久久久久久影视蜜臀 ,欧美一区二区国产,国内精品国语自产拍在线观看http://m.shnenglu.com/Marcky/archive/2009/12/06/102666.htmlMarckyMarckySun, 06 Dec 2009 14:01:00 GMThttp://m.shnenglu.com/Marcky/archive/2009/12/06/102666.htmlhttp://m.shnenglu.com/Marcky/comments/102666.htmlhttp://m.shnenglu.com/Marcky/archive/2009/12/06/102666.html#Feedback0http://m.shnenglu.com/Marcky/comments/commentRss/102666.htmlhttp://m.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;
~点Qn必须是已?/div>
优点Q调用直观,q箋储存Q程序简z?l过试Q析构函数能正确调用)

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;
~点Q非q箋储存Q程序烦琐,ga为A**cd
优点Q调用直观,n可以不是已知

3.  A* ga = new A[m*n];
    ...
    delete []ga;
~点Q调用不够直?/div>
优点Q连l储存,n可以不是已知

4.  vector<vector<A> > ga;
    ga.resize(m);                       //q三行可用可不用
    for(int i = 1; i < n; i++)          //
        ga[i].resize(n);                //
    ...

~点Q非q箋储存Q调试不够方便,~译速度下降Q程序膨胀(实际速度差别不大)
优点Q调用直观,自动析构与释攑ֆ存,可以调用stl相关函数Q动态增?/div>

5.  vector<A> ga;
    ga.resize(m*n);
Ҏ3,4的结?/div>


6. 2的改q版(Penrose提供Q在此感?
    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;
~点Q程序烦琐,ga为A**cd
优点Q连l储存,调用直观Qn可以不是已知



附:1,2,3,6q有对应的malloc-free版本
个h推荐1?,2可以?来代替,3,5调用太烦琐,毕竟源程序是拿来看的
不是拿来q行?/div>


下面是一些错误和没成功的版本

1. A* ga = new A[m][n];
  必然错误

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

   gcc 3.2下编译失败,不知道其它编译器效果如何
   也不知道标准是否允许

我知道的p些,Ƣ迎大家补充Q指?/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://m.shnenglu.com/Marcky/archive/2009/09/28/97474.htmlMarckyMarckyMon, 28 Sep 2009 09:49:00 GMThttp://m.shnenglu.com/Marcky/archive/2009/09/28/97474.htmlhttp://m.shnenglu.com/Marcky/comments/97474.htmlhttp://m.shnenglu.com/Marcky/archive/2009/09/28/97474.html#Feedback0http://m.shnenglu.com/Marcky/comments/commentRss/97474.htmlhttp://m.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;         /* 动态分配数l存储哈夫曼?nbsp;*/

typedef 
char **HuffmanCode;     /* 动态分配数l存储哈夫曼~码?nbsp;*/

/* 从ht?~n的节点中扑և权值最的两个节点Q分别存于s1, s2?nbsp;*/
void Select(HuffmanTree ht, int n, int *s1, int *s2)
{
    
int i;

    
*s1 = 0;
    
*s2 = 0;
    
/*讄s1, s2到开始两个parent{于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中parent{于0Q且权值最的两个节点位置Q分别存于s1, 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存储的n个权|来创Z颗哈夫曼? ht_ptr指向q颗哈夫曼树 */
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个字W,需?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) { /* 初始化数l中前n个单元存储的字符 */
        p
->weight = *w;
        p
->parent = 0;
        p
->lchild = 0;
        p
->rchild = 0;
    }
    
for ( ; i <= m; ++i, ++p) { /* 初始化数l中剩余的单?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的左孩子为s1, 叛_子ؓs2 */
        (
*ht_ptr + i)->lchild = s1;
        (
*ht_ptr + i)->rchild = s2;
        
/* 讄i的权gؓs1, s2之和 */
        (
*ht_ptr + i)->weight = (*ht_ptr + s1)->weight + (*ht_ptr + s2)->weight;
    }
}

/* 对ht_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; /* 从叶子节点开始,q取得父节点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 发表评论
]]>二叉树的创徏及遍历(递归代码Q?/title><link>http://m.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://m.shnenglu.com/Marcky/archive/2009/09/24/97173.html</guid><wfw:comment>http://m.shnenglu.com/Marcky/comments/97173.html</wfw:comment><comments>http://m.shnenglu.com/Marcky/archive/2009/09/24/97173.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.shnenglu.com/Marcky/comments/commentRss/97173.html</wfw:commentRss><trackback:ping>http://m.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;">按先序次序输入二叉树节点的|I格表示I树?/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://m.shnenglu.com/Marcky/aggbug/97173.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.shnenglu.com/Marcky/" target="_blank">Marcky</a> 2009-09-24 22:33 <a href="http://m.shnenglu.com/Marcky/archive/2009/09/24/97173.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Python的多元赋值方式实C换两个变量的?/title><link>http://m.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://m.shnenglu.com/Marcky/archive/2009/08/26/94473.html</guid><wfw:comment>http://m.shnenglu.com/Marcky/comments/94473.html</wfw:comment><comments>http://m.shnenglu.com/Marcky/archive/2009/08/26/94473.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.shnenglu.com/Marcky/comments/commentRss/94473.html</wfw:commentRss><trackback:ping>http://m.shnenglu.com/Marcky/services/trackbacks/94473.html</trackback:ping><description><![CDATA[利用Python的多元赋值方式可以无M时中间变量实C个变量值的交换?br>代码Q?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://m.shnenglu.com/Marcky/aggbug/94473.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.shnenglu.com/Marcky/" target="_blank">Marcky</a> 2009-08-26 16:44 <a href="http://m.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 —?׃n内存(?http://m.shnenglu.com/Marcky/archive/2009/08/20/93911.htmlMarckyMarckyThu, 20 Aug 2009 06:57:00 GMThttp://m.shnenglu.com/Marcky/archive/2009/08/20/93911.htmlhttp://m.shnenglu.com/Marcky/comments/93911.htmlhttp://m.shnenglu.com/Marcky/archive/2009/08/20/93911.html#Feedback0http://m.shnenglu.com/Marcky/comments/commentRss/93911.htmlhttp://m.shnenglu.com/Marcky/services/trackbacks/93911.html
代码如下Q?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;
}
/* 在文件fd上加锁或解锁*/
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"字符串和l束W?时就退出@?nbsp;*/
            
if (SHM_SIZE - strlen(shmptr) < strlen("Child"+ 1break
            strcat(shmptr, 
"C"); /* ׃加了锁,每个Child会q箋出现 */
            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"字符串和l束W?时就退出@?nbsp;*/
        
if (SHM_SIZE - strlen(shmptr) < strlen("Parent"+ 1break;
        strcat(shmptr, 
"P");/* ׃加了锁,每个Parent会q箋出现 */
        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 —?׃n内存(?http://m.shnenglu.com/Marcky/archive/2009/08/20/93909.htmlMarckyMarckyThu, 20 Aug 2009 06:48:00 GMThttp://m.shnenglu.com/Marcky/archive/2009/08/20/93909.htmlhttp://m.shnenglu.com/Marcky/comments/93909.htmlhttp://m.shnenglu.com/Marcky/archive/2009/08/20/93909.html#Feedback0http://m.shnenglu.com/Marcky/comments/commentRss/93909.htmlhttp://m.shnenglu.com/Marcky/services/trackbacks/93909.html
linux内核定义的shared memoryl构shmid_ds如下Q?br>
struct shmid_ds {
    
struct ipc_perm  shm_perm;  /*权限*/
    size_t           shm_segsz; 
/*大小*/
    pid_t            shm_lpid;  
    pid_t            shm_cpid;  
/*创徏者pid*/
    shmatt_t         shm_nattch;
/*q接到此D内存的q程?/span>*/
    time_t           shm_atime;
    time_t           shm_dtime;
    time_t           shm_ctime;
    
};

1、创建或使用一D共享内存用shmget函数Q此函数返回共享内存标C符?br>
#include <sys/shm.h>
int shmget(key_t key, size_t size, int flag);
如果key取gؓIPC_PRIVATE或者key当前为和特定cd的IPCl构相结合,q且flag指定了IPC_CREAT位,则创Z个新的share memoryl构?br>size为共享内存段的长度(字节Q?br>
2、对一个共享内存段q行操作使用shmctl?br>
#include <sys/shm.h>
int shmctl(int shmid, int cmd, struct shmid_ds *buf);
shmid指定需要操作的shared memory
cmd指定需要进行的操作
       IPC_STAT取得此段的shmid_dsl构攑օbuf中?br>       IPC_SET用buf的D|此D中的:shm_perm.uidQshm_perm.gidQshm_perm.mode?br>       IPC_RMID从系l中删除此共享内存段?br>       SHM_LOCK共享内存锁定到内存中?br>       SHM_UNLOCK解锁׃n内存Dc?br>
3、将一个共享内存段q接到自q地址I间使用shmatQ?br>
#include <sys/shm.h>
void *shmat(int shmid, const void *addr, int flag);
推荐addr?|此D连接到内核选择的第一个可用的地址上。增加程序的可移植性?br>
4、对׃n内存操作l束后,要脱该D는shmdtQ?br>
#include <sys/shm.h>
int shmdt(void *addr);
addr是shmat的返回倹{?br>


Marcky 2009-08-20 14:48 发表评论
]]>
安全讉K数组的指针类模板http://m.shnenglu.com/Marcky/archive/2009/08/13/93233.htmlMarckyMarckyThu, 13 Aug 2009 10:29:00 GMThttp://m.shnenglu.com/Marcky/archive/2009/08/13/93233.htmlhttp://m.shnenglu.com/Marcky/comments/93233.htmlhttp://m.shnenglu.com/Marcky/archive/2009/08/13/93233.html#Feedback0http://m.shnenglu.com/Marcky/comments/commentRss/93233.htmlhttp://m.shnenglu.com/Marcky/services/trackbacks/93233.html
#include <iostream>
#include 
<stdexcept>

using namespace std;

template
<typename T>
class Ptr2T {
public:
//构造函敎ͼ形参为数lv始地址和大?/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);
//安全的数l元素访问操?/span>
    T& operator*() const;  
private:
    T 
*m_p;           //讉K数组的指?/span>
    T *m_array;       //保存数组的v始地址
    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 发表评论
]]>
昄构造函C转换q算W的合作http://m.shnenglu.com/Marcky/archive/2009/08/13/93193.htmlMarckyMarckyThu, 13 Aug 2009 06:39:00 GMThttp://m.shnenglu.com/Marcky/archive/2009/08/13/93193.htmlhttp://m.shnenglu.com/Marcky/comments/93193.htmlhttp://m.shnenglu.com/Marcky/archive/2009/08/13/93193.html#Feedback0http://m.shnenglu.com/Marcky/comments/commentRss/93193.htmlhttp://m.shnenglu.com/Marcky/services/trackbacks/93193.html
class Year {
    
int m_y;
public:
//explicit限制int到Year的隐式{?/span>
    explicit Year(int y)
        : y(m_y) { }
//Year到int的类型{?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不能隐式转换为Year
Date d2(21, feb, Year(1987)); //ok



在这里Year只是包裹住了intQ对int提供一层保护而已。由于operator int()的存在,只要需要,Year可以隐式的{化ؓint出现q算表达式中参加q算。而通过l构造函数声明ؓexplicitQ就能够保证Qint到Year的{化只能在明确无误的情况进行,避免了意外的赋倹{?br>
昄构造函数和转换q算W的合作Q让Year可以当int使用Q同时又对Yearq行一定的保护。。?br>



Marcky 2009-08-13 14:39 发表评论
]]>
Allocating Arrays Using Placement new (zz)http://m.shnenglu.com/Marcky/archive/2009/08/13/93110.htmlMarckyMarckyWed, 12 Aug 2009 16:48:00 GMThttp://m.shnenglu.com/Marcky/archive/2009/08/13/93110.htmlhttp://m.shnenglu.com/Marcky/comments/93110.htmlhttp://m.shnenglu.com/Marcky/archive/2009/08/13/93110.html#Feedback0http://m.shnenglu.com/Marcky/comments/commentRss/93110.htmlhttp://m.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 发表评论
]]>
一个Python文本处理E序http://m.shnenglu.com/Marcky/archive/2009/07/22/90839.htmlMarckyMarckyWed, 22 Jul 2009 08:33:00 GMThttp://m.shnenglu.com/Marcky/archive/2009/07/22/90839.htmlhttp://m.shnenglu.com/Marcky/comments/90839.htmlhttp://m.shnenglu.com/Marcky/archive/2009/07/22/90839.html#Feedback0http://m.shnenglu.com/Marcky/comments/commentRss/90839.htmlhttp://m.shnenglu.com/Marcky/services/trackbacks/90839.htmlsome
are
born
great  阅读全文

Marcky 2009-07-22 16:33 发表评论
]]>
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
      <noscript id="pjuwb"></noscript>
            <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
              <dd id="pjuwb"></dd>
              <abbr id="pjuwb"></abbr>
              9ƷƵ| | һɫav| ձ| ϵĻ| ŷһ߹ۿ| ŷƵһ| þۺϸ| ޻ɫ߿| þۺϾɫŷۺϺݺ| þˬˬˬ˾þþ| þƵ| ޹| 뾫Ʒ98ҹ | 鶹þþƷ| ŷֱjizzŷ20| ŷƬվ| ŷһƷɫ| ޸Ƶ| ѹۿ| ŷƵһ| һƷavѹۿ| þþƷۺ| ŷƵһ| ޹һ| ҹƷ99þ| ŷƵ| ҹƷƵ| ŷҹƷ| ŷ͵ľƷ| þþƷ޾Ʒ| ¹߹ۿ| þaһ| ŷɫ͵͵| ޾ƷƷapp| ŷ˿| 99߹ۿƵƷۿ| ŷ߿Ƭaѹۿ| һ| ޵ӰƵվ| ŮëƬڵ| ѳav| ҹƷƵ| Ʒһ| Ƶŷպ| ŷһ| ŷvavaպaۺɫ| ˾޴Ʒŷһ| ŷһ߹ۿ| Ʒ˿༤ŷ| ŷձĻ| ޾Ʒ| ŷ߿ҰŷƷ| þۺһ| ˾þŮͼƬ| þۺϹƷ| þþƷۺ| ޺ձĻ| ŷvava㽶| պƷ| ޾ѡ| պƵ߹ۿ| ŷػһ| 㽶þҹɫ| þðwww| ޵һɫ| 91þþ޾Ʒ| ŷպɫվһ| ޾ƷԴŮ¾Ƶ| 91þ| ƷɫۺϾþ| þùƷþþƷ| һ| Ʒ޳| һ| ŷrƬ| ŷaƵ| ߹ۿƵ| ҹƷӰԺ| ywҹƷƵ| ŷƬ߹ۿ| ŷxxxxx| լ߹ۿ޲| av| һƵۿ| ˳| ƷձƷ| ŷƵ| Ʒvvv| վŷ| ҹƷӰԺ| 鶹ۺ| һպƷ| þ| ŷƵѹۿ| ޹һ| ޹˾ƷŮ˾þþ | ҹƷþþþþþþþþþþ| ƷպƷŷƷ| av߲һ| ͬŷ| ŷһ13˾| þñۺɫݺ| av| ҹƷ| Ʒ߳þav| ŷһ| ŷwww| ѲƵ| Ʒ| ŷһ| ŷղƵ| ŷһƵ| ŷvavaƵva| ŷһӰԺ| ŷɫþ88ۺ޾Ʒ| 鶹ѾƷƵ| Ʒѹۿ| Ůٸžž| ݺɫۺɫ| ۺƵһ| Ƶ߹ۿվ| ŷƷ123| ŷ龫Ʒþþþþþñ̬| ˾޴ƷŷһСƵ| һۺ| ŷһ| Ʒҹɫ7777| Ʒ99þþþþþŮ| ޾Ʒŷ| ŷƷһһƵ | ۺպŷ| ޾Ʒһ| һվ| ŷպۺ| ƷˬҵĿɰ| ҹav| ŷһ| ŷպһ| ҹþõӰ| þþƷƵ߿| ҹƷһŷ | Ʒһ| һպ| ҹƷƵ| 91þۺ| Ʒþþþþó| ޼| һõþۺþ| ŷպŷ| ҹƷ| ŷջƵ| ɫպ| ŷŷ| ޾ѡһ| þ9ȾƷƵ| ߹ۿר| ŷ༫Ʒvideosbest°汾 | ˳վƷƬ߹ۿ| Ʒþþþþþþһ| Ʒþþþþþþþþþ| ݺ88ۺϾþþþۺ| ޻ҳһ| Ƶպ| ŷ123| ŷ1| ŷƵѹۿ| ҹƷһ| ͵þþþƷר| ŷƵ| ҹһ| þþۺƵ| һƷƵ | ߹ۿպwwwƵ| Ƶ߹ۿŷ| ŷ͹һƵ| ŷȷ| ŷһ| ŷƵ| ͼƬ㶮| þ| 91þҹɫƷվ| ҹƵƷ| ޹Ƶһ| ƷþþþþӰ| þ鶹| ͼƬŷղ| ŷӰ| ҹһ߹ۿ| Ӱȷ޾Ʒ| Ʒ| ŷa99þúר| ۺɫ| һɫþ88Ʒۺ| þӰԺ| þðwwwþ| ɫavһӰ| ѳavӰ| ޹ɫ| þŮ|