• <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>

            chenglong7997

            C/C++ 2D數組的動態分配方法比較

            方法1:
             1 #include <iostream>
             2 using namespace std;
             3 
             4 //by snape 2012-3-25
             5 //method 1: drawback is we need calculate the one-dimensional index to access the 2D array
             6 int main()
             7 {
             8     int rowSize, colSize, totalSize;
             9     int index, i,j;
            10 
            11     cout<<"Enter the row and column size for your 2D array!"<<endl;
            12     cin>>rowSize>>colSize;
            13 
            14     totalSize=rowSize*colSize;
            15     int *pArray;
            16     pArray=new int[totalSize];
            17 
            18     //file  the array with integers from 0 to totalsize
            19     //file across the rows, moving down the colums
            20 
            21     int arrayValue=0;
            22     for(i=0; i<rowSize; ++i)    //outer-loop traverse down the "rows"
            23     {
            24         for(j=0; j<colSize; ++j)
            25         {
            26             //caculate array index
            27             //index=rowSize*j+i;        //both index=rowSize*j+i; and index=colSize*i+j; are ok
            28             index=colSize*i+j;            //but if index=rowsize*i+j; or index=colSize*j+i; then there will be a bug.
            29             pArray[index]=arrayValue;    // i like index=colSize*i+j; since the arrange of 2D is according to rows
            30             cout<<"index="<<index<<endl;
            31             cout<<"i(row)="<<i<<"j(col)="<<j<<"array value"<<pArray[index]<<endl;
            32             ++arrayValue;
            33         }
            34     }
            35 
            36     //output the array
            37     for(int k=0; k<totalSize; ++k)
            38     {
            39         cout<<pArray[k]<<endl;
            40     }
            41     cout<<"The End"<<endl;
            42     delete [] pArray;
            43     return 0;
            44 }
            45 
            46 

            方法2:
             1 //by snape
             2 //method 2: better than method 1, but call new twice
             3 int main()
             4 {
             5     int rowSize, colSize, totalSize;
             6     int i, j;
             7     cout<<"Enter the row and column size for your 2D array"<<endl;
             8     cin>>rowSize>>colSize;
             9 
            10     totalSize=rowSize*colSize;
            11 
            12     int *pArray; //pointer to an integer
            13     int **pPointerArray; //pointer to an integer pointer
            14 
            15     pArray=new int[totalSize];    //memory for totalSize integers
            16     pPointerArray=new int*[rowSize];    //memory for rowSize # of int pointers
            17 
            18     //fill the pointer array with the pArray[i][0] address
            19     for(i=0; i<rowSize; ++i)
            20         pPointerArray[i]=pArray+i*colSize;    //place the address into the pointer
            21 
            22     //now fill the pArray by using the pPointerArray to access elements
            23     int arrayValue=0;
            24     for(i=0; i<rowSize; ++i)
            25     {
            26         for(j=0; j<colSize; ++j)
            27         {
            28             pPointerArray[i][j]=arrayValue;    //cool
            29             cout<<"i(row)="<<i<<"j(col)="<<j<<"array value="<<pPointerArray[i][j]<<endl;
            30             ++arrayValue;
            31         }
            32     }
            33 
            34         //output the array
            35     for(int k=0; k<totalSize; ++k)
            36     {
            37         cout<<pArray[k]<<endl;
            38     }
            39     cout<<"The End!"<<endl;
            40     delete [] pArray;
            41     delete [] pPointerArray;
            42     return 0;
            43 }

            方法3:
             1 // by snape
             2 //method 3: better than method 2. just malloc once and the memory is contiguous block. the best
             3 int **my2DAlloc(int rowSize, int colSize)
             4 {
             5     int i;
             6     int header=rowSize * sizeof(int *);
             7     int data=rowSize * colSize * sizeof(int);
             8     int **rowptr=(int **)malloc(header+data);    //malloc memory for both data and pointerArray(the header)
             9 
            10     if(rowptr==NULL)
            11         return NULL;
            12     int *buf=(int *)(rowptr+rowSize);    //buf: the pointer to the first data
            13     for(i=0; i<rowSize; ++i)    //assign the address of each row to pointerArray(the header)
            14         rowptr[i]=buf+i*colSize;
            15 
            16     return rowptr;
            17 }
            18 
            19 int main()
            20 {
            21     cout<<"Enter the row and column size for your 2D array"<<endl;
            22     int rowSize, colSize;
            23     cin>>rowSize>>colSize;
            24     int **p=my2DAlloc(rowSize, colSize);
            25 
            26     //assign values
            27     int i, j, arrayValue=0;
            28     for(i=0; i<rowSize; ++i)
            29         for(j=0; j<colSize; ++j)
            30             p[i][j]=arrayValue++;
            31     
            32     //output values
            33     for(i=0; i<rowSize; ++i)
            34         for(j=0; j<colSize; ++j)
            35             cout<<p[i][j]<<endl;
            36 
            37     free((void *)p);
            38 }

            方法3,我感覺最好,只調用一次malloc, 空間連續,釋放內存也比較方便。

            大家有什么想法的歡迎交流

            posted on 2012-03-27 01:20 Snape 閱讀(1653) 評論(6)  編輯 收藏 引用 所屬分類: CTCI_C++

            評論

            # re: C/C++ 2D數組的動態分配方法比較 2012-03-27 09:40 tb

            很不錯的幾個例子  回復  更多評論   

            # re: C/C++ 2D數組的動態分配方法比較 2012-03-27 10:49 gbb21

            Wo cao ....

            int (*a)[10] = new int[10][10];

            a[2][4] = 4;

            .....  回復  更多評論   

            # re: C/C++ 2D數組的動態分配方法比較 2012-03-27 13:24 Snape

            謝謝啊~~@tb
              回復  更多評論   

            # re: C/C++ 2D數組的動態分配方法比較 2012-03-27 13:29 Snape

            你給的例子是二位維數都已知的情況下。有人總結可以分為知道一維維數,二位維數和變形的不同情況。我轉載了http://www.shnenglu.com/chenglong7997/articles/168955.html。我這里說的是維數都未知的情況,但轉載中的二維分配方法,思路是一種。這里舉出其他方法,感覺更好。你如果有覺得更好的,可以討論@gbb21  回復  更多評論   

            # re: C/C++ 2D數組的動態分配方法比較 2012-03-27 19:40 放屁阿狗

            可以看一下linux kernel代碼關于虛擬內存的申請和管理代碼,那個brk很有趣,相信對以后內存管理有很大幫助  回復  更多評論   

            # re: C/C++ 2D數組的動態分配方法比較 2012-03-28 01:40 Snape

            恩,有空的話看看,之前看過一點,很吃力。呵呵@放屁阿狗
              回復  更多評論   

            導航

            <2012年8月>
            2930311234
            567891011
            12131415161718
            19202122232425
            2627282930311
            2345678

            統計

            常用鏈接

            留言簿

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            my

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久青青草原亚洲av无码app| 久久se精品一区精品二区国产| 四虎影视久久久免费观看| 久久99热这里只有精品国产 | 色欲综合久久躁天天躁| 久久天天躁狠狠躁夜夜不卡| 狠狠色丁香婷婷久久综合五月| 欧洲成人午夜精品无码区久久| 99久久精品国产一区二区| 久久久午夜精品福利内容| 国内精品人妻无码久久久影院 | 久久亚洲国产最新网站| 国产精品久久久久AV福利动漫| 26uuu久久五月天| 精品熟女少妇a∨免费久久| 久久国产综合精品五月天| 欧美一区二区三区久久综合| 久久精品国产WWW456C0M| 久久久久AV综合网成人| 亚洲国产天堂久久综合| 久久婷婷久久一区二区三区| 欧美精品丝袜久久久中文字幕 | 狠狠色噜噜色狠狠狠综合久久| 精品一区二区久久| 久久强奷乱码老熟女网站| 久久露脸国产精品| 久久精品国产69国产精品亚洲| 亚洲va国产va天堂va久久| 色老头网站久久网| 欧美与黑人午夜性猛交久久久| 色综合久久综精品| 国产精品久久永久免费| …久久精品99久久香蕉国产| 久久久无码精品亚洲日韩蜜臀浪潮| 国产亚州精品女人久久久久久| 夜夜亚洲天天久久| 亚洲天堂久久精品| 久久国产成人午夜AV影院| 国产高清美女一级a毛片久久w | 亚洲第一永久AV网站久久精品男人的天堂AV | 久久伊人中文无码|