Memory 相同的地址分配给不同的节点
我必须为最初都指向null的行和列创建指针数组。Memory 相同的地址分配给不同的节点,memory,Memory,我必须为最初都指向null的行和列创建指针数组。 新创建的行/列标题的地址将插入到数组中。 在结果中,我看到地址在行和列数组中重复 问题:地址在行和列数组中重复。 我必须提到的是,我没有使用delete(用于释放内存,因为我不知道应该在函数内部还是外部包含它) 我还看到没有为令牌2x3y3创建行标头 First I created an array of pointers //allocating row and column pointers, m is number of rows
新创建的行/列标题的地址将插入到数组中。
在结果中,我看到地址在行和列数组中重复
- 问题:地址在行和列数组中重复。
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';
}
}
为什么没有为令牌=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
真的很难弄明白你想在这里做什么。突出的两件事: 请记住,对于长度为m的数组,只能从0索引到m-1(而不是m)。
create\u array\u with_nullp
中的循环将超过每个数组的末尾,从而损坏内存
node **rArr = new node*[m];
//...
for(int i=0; i<=m;i++){ // ouch! should only go to i**<**m
或者你的意思是:
cArr[b] = new_colheader;
您是否打算交叉引用colheader和rowheader?如果是,您需要:
new_colheader->rowLink = new_rowheader;
new_rowheader->colLink = new_colheader;
在创建链接新节点的末尾
同样,我也不确定要完成什么,但至少这些事情似乎是可疑的。第2行的内存地址与第0列的地址相同,*第3行与第1列的地址相同,*第4行与第2列的地址相同。谢谢Okuma Scott的编辑我编辑了代码“cArr[b]=new_colheader”而不是“cArr[b]=new\u colheader->rowLink”。新_头的地址将保存在相应头的数组中。没有为令牌2x3y3创建行标头。最后,当我检查row和column的数组时,给一些索引值提供了相同的地址。
cArr[b] = new_colheader->rowLink;
cArr[b] = new_colheader;
new_colheader->rowLink = new_rowheader;
new_rowheader->colLink = new_colheader;