dynamic allocation

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 = new int *[x];
	gdata = new int *[x];
	bdata = new int *[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] = new int[y];
		gdata[i] = new int[y];
		bdata[i] = new int[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] = new int[y];
	}
	for(int i =0; i< x; i++){
		gdata[i] = new int[y];
	}
	for(int i =0; i< x; i++){
		bdata[i] = new int[y];
	}
Last edited on
What did you do to come to that conclusion? Looking at your code I can't see how that could happen.
(gdb) print &gdatat[154][43]
$3 = (int *) 0x100deccac
(gdb) print &rdatat[154][555]
$4 = (int *) 0x100deccac

I also check the address of two pixels. They have same address
Topic archived. No new replies allowed.