Hey all,

I need to initialize an array to hold an image to all zeroes. The dimensions are that of an image [numcols][numrows][3], where 3 is for rgb values. I know this isn't necessary when I'm reading an image into the array, of course, but I'm going to be using the function to create an additional zeroed array of the same size as the picture, such that I can record locations of certain anomalies of the picture (that is, I want to be able to later iterate through my mirror array and skip over everything that is still zero).

The problem is [j][2], after I attempt to initialize the array to zero, when I output the contents of the array I get the following output for every value of i and j:

0 0 6BA9CACC0

Here is the current code I'm using to make the array:


void bmp_make_array(bmp_data& bmp_struct){


	bmp_struct.rgb_array = new unsigned char**[bmp_struct.numRows];

	for (unsigned int i = 0; i < bmp_struct.numRows; ++i){
		bmp_struct.rgb_array[i] = new unsigned char*[bmp_struct.numCols];

		for (unsigned int j = 0; j < bmp_struct.numCols; ++j){
			bmp_struct.rgb_array[i][j] = (unsigned char*) calloc(3, 1);
		}
	}

	for (unsigned int i = 0; i < bmp_struct.numRows; ++i){
		for (unsigned int j = 0; j < bmp_struct.numCols; ++j){
			std::cout << (int)bmp_struct.rgb_array[i][j][0] << " " << (int)bmp_struct.rgb_array[i][j][1] << " " << std::cout << (int)bmp_struct.rgb_array[i][j][2] << std::endl;
		}
	}

}

Before using calloc, my original solution was just to manually set the values:

bmp_struct.rgb_array = new unsigned char**[bmp_struct.numRows];

	for (unsigned int i = 0; i < bmp_struct.numRows; ++i){
		bmp_struct.rgb_array[i] = new unsigned char*[bmp_struct.numCols];

		for (unsigned int j = 0; j < bmp_struct.numCols; ++j){
			bmp_struct.rgb_array[i][j] = new unsigned char[3];
			bmp_struct.rgb_array[i][j][0] = 0;
			bmp_struct.rgb_array[i][j][1] = 0;
			bmp_struct.rgb_array[i][j][2] = 0;

		}
	}

But both had the same result.

What am I missing?

oops! n/m :) I accidentally cut and pasted a 2nd std::cout in the middle of my output line. I better take a break... thanks for looking it over.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.