對於之前”指標特輯”的前三篇文,在程式中讓人感覺實用意義並不大,接下來要說的全都是重點,如果前三篇文沒有看懂的人,請先回去看懂吧。

1. 為什麼要用動態記憶體:一般我們使用陣列時,你不可以隨時改變陣列的大小,陣列的大小在宣告時就已經確定。所以不可能會有像以下的程式碼出現

int N = 10;
int a[N]; // complier error
N = 20;
a[N]; // complier

但如果一開始我們就宣告陣列的大小,如果我們沒用那麼多的話就浪費,如果用太多、超過的話又會造成 runtime error,怎麼辦呢?這就是為什麼要使用動態記憶體的原因了,因為動態配置陣列,可以隨心所欲的控制陣列的元素個數。這篇暫以指標動態配置一維陣列為例。

2. 配置動態記憶體注意事項:

假設我要配置的陣列大小為 cnt 個

(2.1) C語言使用 malloc進行配置,語法為

        資料型態 *ptr;
        ptr = (資料型態*)malloc(sizeof(資料型態)*cnt);

如:
        double *ptr;
        ptr = (double*)malloc(sizeof(double)*cnt);

 

C++使用 new 進行配置其語法為

資料型態 *ptr;
       ptr = new 資料型態[cnt];

如:
        double *ptr;
        ptr = new double[cnt];

 

(2.2) 配置完後,和一般陣列的存取方式都一樣,你可以用 *(ptr+i); 也可以用 ptr[i] 方式進行存取。

 

(2.3) 如果你已經用該指標配置了一塊記憶體空間(在這裡指的是陣列),那麼當你還要再進行一次新的配置時,記得把原本的記憶體空間給釋放出去,否則你的程式記憶體會愈吃愈大,最後將因記憶體空間不足而跳出。

C語言使用  free(ptr) 進行釋放
C++ 使用 delete [] ptr 進行釋放

最後,不論如何,只要有配置,就一定要釋放,一個 malloc 就配一個 free,一個new就配一個delete。

3. 程式範例-C

// ====================================
// FileName: Ptr1Dim_C.cpp
// Author  : Edison.Shih.
// Complier: VC 2008

#include <stdio.h>
#include <stdlib.h>

// ====================================
// main function
int main(int argc, char**argv)
{
        double *ptr = NULL;
        int Dim = 0, i=0;

        printf("please input Dim:");
        scanf_s("%d", &Dim);
       
        // malloc the memory address
        ptr = (double*)malloc(sizeof(double)*Dim);
        // set the value - method 1
        for(i=0; i<Dim; i++) *(ptr+i) = (double)(i*i);
        // show the value - method 2
        for(i=0; i<Dim; i++) printf("ptr[%d] = %lf\n", i, ptr[i]);
       

        printf("please input Dim again:");
        scanf_s("%d", &Dim);
        // before you malloc the new memory addree.
        // free the old memory address first.
        free(ptr);
        ptr = (double*)malloc(sizeof(double)*Dim);
        // set the value - method 1
        for(i=0; i<Dim; i++) *(ptr+i) = (double)(i*i);
        // show the value - method 2
        for(i=0; i<Dim; i++) printf("ptr[%d] = %lf\n", i, ptr[i]);

        free(ptr); // last, free the memory.

        return 0;
}

4. 程式範例-C++

// ====================================
// FileName: Ptr1Dim_Plus.cpp
// Author  : Edison.Shih.
// Complier: VC 2008

#include <iostream>
using namespace std;
// ====================================
// main function
int main(int argc, char**argv)
{
        double *ptr = NULL;
        int Dim = 0, i=0;

        cout << "please input Dim:" ;
        cin >> Dim;
       
        // malloc the memory address
        ptr = new double[Dim];
        // set the value - method 1
        for(i=0; i<Dim; i++) *(ptr+i) = (double)(i*i);
        // show the value - method 2
        for(i=0; i<Dim; i++) cout << "ptr[" << i << "]=" << ptr[i] << endl;

        cout << "please input Dim again:" ;
        cin >> Dim;
        // before you malloc the new memory addree.
        // free the old memory address first.
        delete [] ptr;
       
        // malloc the memory address
        ptr = new double[Dim];
        // set the value - method 1
        for(i=0; i<Dim; i++) *(ptr+i) = (double)(i*i);
        // show the value - method 2
        for(i=0; i<Dim; i++) cout << "ptr[" << i << "]=" << ptr[i] << endl;


        // free the memory address
        delete [] ptr;

         return 0;
}

5. 執行結果

please input Dim:3
ptr[0] = 0.000000
ptr[1] = 1.000000
ptr[2] = 4.000000
please input Dim again:5
ptr[0] = 0.000000
ptr[1] = 1.000000
ptr[2] = 4.000000
ptr[3] = 9.000000
ptr[4] = 16.000000

arrow
arrow
    全站熱搜

    Edison 發表在 痞客邦 留言(3) 人氣()