I allocate a matrix by dynamic allocation. But I find that some elements from the matrix point to the same memory address! How can I solve this problem, please?
Thanks a lot!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
rdata = newint *[x];
gdata = newint *[x];
bdata = newint *[x];
if(rdata==NULL||gdata==NULL||bdata == NULL){
cout<<"allocation in ImageRGB has problem!"<<endl;
exit(1);
}
for(int i =0; i< x; i++){
rdata[i] = newint[y];
gdata[i] = newint[y];
bdata[i] = newint[y];
if(rdata[i]==NULL||gdata[i]==NULL||bdata[i] == NULL){
cout<<"allocation in ImageRGB has problem!"<<endl;
exit(1);
}
}
When I seperate the allocation in iteration as below, it works! Could you explain this please?
1 2 3 4 5 6 7 8 9
for(int i =0; i< x; i++){
rdata[i] = newint[y];
}
for(int i =0; i< x; i++){
gdata[i] = newint[y];
}
for(int i =0; i< x; i++){
bdata[i] = newint[y];
}