2009-09-28

可惡的程式

我已經想了兩天了,卻遲遲無法決定要用哪個方法。也許並沒有我想的那麼嚴重,畢竟現在電腦中的記憶體都這麼大,管他用哪一個方法宣告一塊記憶體區域,不會影響到整體的速度。

struct counterpart {
    int ncp;    /* number of elements in cp */
    int *cp;    /* array with ncp elements */
};

struct counterpart_array {
    size_t nobj;    /* number of objects looking for cp(s) */
    int *ncp;         /* array of number of counterparts */
    int **cp;         /* array of counterpart pointer */
};

The major difference between this two structs is that the block of the memory is segment or not.  For the yellow case, we have to allocate an array of the structs with nobj (number of objects).  Each cp needs allocating with one element as default.  Therefore, the locations are separated in the memory.  It might reduce the efficiency of computing.

The green case might work only if we create *cp in an order, i.e. element(s) follows the previous element.

Actually, I thought another way to do this

struct counterpart_arr {
    size_t nobj;        /* number of objects looking for cp(s) */
    int *ncp;            /* number of cp(s) found with the order of obj */
    int *cp_index;    /* array of cp index */
};

No comments: