0

我必须为最初都指向空的行和列创建一个指针数组。
新创建的行/列标题的地址将被插入到数组中。
在结果中,我看到地址在行和列数组中重复。

  • 问题:地址在行和列数组中重复。

我不得不提到我没有使用 delete (用于释放内存,因为我很困惑在函数内部还是外部将它包含在哪里)。

我还看到没有为令牌 2x3y3 创建行标题。

First I created an array of pointers  

//allocating row and column pointers, m is number of rows and n is number of columns
- node **rArr = new node*[m+1];
- node **cArr = new node*[n+1];

    void create_array_with_nullp(){


        for(int i=0; i<=m;i++){

            rArr[i]=NULL;
            std::cout<<"row array contents"<<rArr[i]<<'\n';
        }

        for(int i=0; i<=n;i++){

            cArr[i]=NULL;
            std::cout<<"col array contents"<<cArr[i]<<'\n';
        }
    }

然后,如果尚未通过以下函数创建令牌,我将为令牌创建行头或列头:

    void create_n_link_new_node(int a, int b){


        if(a >m || b>n || a<0 || b<0){
            return;
        }

        node * colptr = cArr[b];

        node * rowptr = rArr[a];

        if (rowptr==NULL){

            node * new_rowheader = new node;

            new_rowheader->coefficient = NULL;
            new_rowheader->row = a;
            new_rowheader->column = -1;
            new_rowheader->rowLink = new_rowheader;
            new_rowheader->colLink = new_rowheader;


            rArr[a] = new_rowheader;

            std::cout<<"new row header created"<<'\n';
            std::cout<< "coefficient = "<<new_rowheader->coefficient<<'\n';
            std::cout<< "row = "<<new_rowheader->row<<'\n';
            std::cout<< "column = "<<new_rowheader->column<<'\n';
            std::cout<< "rowLink = "<<new_rowheader->rowLink<<'\n';
            std::cout<< "colLink = "<<new_rowheader->colLink<<'\n';



        }


        if(colptr == NULL){
            node * new_colheader = new node;

            new_colheader->coefficient = NULL;
            new_colheader->row = -1;
            new_colheader->column = b;
            new_colheader->rowLink = new_colheader;
            new_colheader->colLink = new_colheader;



            cArr[b] = new_colheader;

            std::cout<<"new column header created"<<'\n';
            std::cout<< "coefficient = "<<new_colheader->coefficient<<'\n';
            std::cout<< "row = "<<new_colheader->row<<'\n';
            std::cout<< "column = "<<new_colheader->column<<'\n';
            std::cout<< "rowLink = "<<new_colheader->rowLink<<'\n';
            std::cout<< "colLink = "<<new_colheader->colLink<<'\n';



        }
    }

结果是:

THE RESULT:
token==5x4y2
coefficient=5
row= 4
col = 2
new row header created
coefficient = 0
row = 4
column = -1
rowLink = 0x100103c10
colLink = 0x100103c10
new column header created
coefficient = 0
row = -1
column = 2
rowLink = 0x100103c30
colLink = 0x100103c30

token==8x4y
coefficient=8
row= 4
col = 1
new column header created
coefficient = 0
row = -1
column = 1
rowLink = 0x100103c50
colLink = 0x100103c50

token==2x3y3
coefficient=2
row= 3
col = 3
new column header created
coefficient = 0
row = -1
column = 3
rowLink = 0x100103c70
colLink = 0x100103c70

token==4xy2
coefficient=4
row= 1
col = 2
new row header created
coefficient = 0
row = 1
column = -1
rowLink = 0x100103c90
colLink = 0x100103c90

token==y3
coefficient=-1
row= 0
col = 3
new row header created
coefficient = 0
row = 0
column = -1
rowLink = 0x100103cb0
colLink = 0x100103cb0

token==5y
coefficient=5
row= 0
col = 1

token==5
coefficient=5
row= 0
col = 0
new column header created
coefficient = 0
row = -1
column = 0
rowLink = 0x100103cd0
colLink = 0x100103cd0

为什么没有为 token=2x3y3 创建行头?

最后存储在指针数组中的地址是:

#Array of row pointers#
- row 0 = 0x100103cb0
- row 1 = 0x100103c90
- row 2 = 0x100103cd0
- row 3 = 0x100103c50
- row 4 = 0x100103c30
#Array of column pointers#
- column 0 = 0x100103cd0
- column 1 = 0x100103c50
- column 2 = 0x100103c30
- column 3 = 0x100103c70

* row 2 is having the same address of column 0,
* row 3 is having the same address of column 1,
* row 4 is having the same address of column 2
4

1 回答 1

0

很难弄清楚你在这里想要做什么。一些突出的事情:

请记住,对于长度为 m 的数组,您只能索引从 0 到 m-1(而不是 m)。您的循环create_array_with_nullp进入每个数组的末尾,从而破坏了内存。

node **rArr = new node*[m];
//...
        for(int i=0; i<=m;i++){  // ouch! should only go to i**<**m

create_n_link_new_node中,您是不是要像这样设置列条目:

cArr[b] = new_colheader->rowLink;

或者你的意思是:

cArr[b] = new_colheader;

您是否打算让每个 colheader 和 rowheader 交叉引用?如果是这样,您将需要:

new_colheader->rowLink = new_rowheader;
new_rowheader->colLink = new_colheader;

在结束时create_n_link_new_node

同样,不确定要完成什么,但至少这些事情似乎值得怀疑。

于 2013-11-10T17:21:55.297 回答