Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
UPDATE:

This always seems to happen to me. Pointers are the devil. So i believe my issue is the fact that the return value that's being stored into my new array images[] is the same address.

I ended up printing the address for each index of the images[]. All addresses are different. However, when I print the next level images[i][0] all addresses are the same.

I'll put my thoughts in the what I've done section

I don't work in C/C++ often, so I always have to do a refresher of pointers.

ORIGINAL:
I have a program that locates a list of bmp files within a directory. From there I process all of them one at a time and store them into a array. Each image is made up of a struct associated with R,G,B. When I process a single image I insert it into my array of images at the next available index. However, when I've finished opening all bmp images and print out the data at each index they are the same.

here is the process

first the struct that is being used for the images is

C++
typedef struct pixel {
	uint8_t r;
	uint8_t g;
	uint8_t b;

} pixel;


C++
//image_names = name of all images which have been sorted
//num_images = total number of images
//IMG_Y = 360
//IMG_X = 640

pixel** img;
struct pixel** images[num_images];

// Allocate image structure which will be used to load images
img = (pixel**)malloc (IMG_Y * sizeof (pixel*));
for (i = 0; i < IMG_Y; i++) 
{
		img[i] = (pixel*)malloc (IMG_X * sizeof (pixel));
}

for (i = 0; i < num_images; i++)
{
	// Open image and ensure it's successful. 
	if (open_bmp (*(image_names + i), img) == EXIT_FAILURE) 
    {
		return EXIT_FAILURE;
	}

	images[i] = img;
	}
}


just for background here is the open_bmp()

C++
<pre>int open_bmp (char * file_name, pixel** img) {
	// Variable declarations
	long offset = 0;
	unsigned char buff[100];
	int x = 0, y = 0;
	FILE *f = fopen (file_name, "rb"); // open file in binary mode


	// Verify that the file opened is in the correct format
	if (verify_bmp (f, &offset, file_name) == EXIT_FAILURE) {
		printf ("Unable to proceed\n");
		return EXIT_FAILURE;
	}


	// Read pixel data
	fseek (f, offset, SEEK_SET); // go to beginning of pixel data
	while ((fread (&buff, 3, 1, f) >= 1) && y < IMG_Y) // while not EOF (or done with image), read next pixel
	{

		img[y][x].r = buff[2];
		img[y][x].g = buff[1];
		img[y][x].b = buff[0];

		x++; // read next column
		if (x >= IMG_X) { // finished reading row, go to next row
			y++;
			x = 0;
		}
	}

	if (PRINTBMPDATA) {
		for (y = 0; y < IMG_Y; y++) {
			for (x = 0; x < IMG_X; x++) {
				printf ("%02x%02x%02x ", img[y][x].b, img[y][x].g, img[y][x].r);
			}
			printf ("\n");
		}
		printf ("\n");
	}

	// Close image file now that we have copied it to the img structure
	if (fclose (f)) {
		printf ("Unable to close image file\n");
		return EXIT_FAILURE;
	}

	return EXIT_SUCCESS;
}


What I have tried:

I've done print statements in several locations to verify different parts work. For instance, "if (PRINTBMPDATA)" I changed to a value of 1 so that it would always print the results of open_bmp(). I verified that each image sent to open_bmp() is in fact different.

I also inserted a print loop immediately after the call to open_bmp() and printed the output variable "img" The return value is different each time.

However, when I loop "images[]", no matter what index I'm looking at it is exactly the same as the next.

I'm not sure what I'm missing. Any suggestions would be appreciated.

UPDATE:

So pointers. in this case I'm passing the variable img as a double pointer, yea thats new to me, so I'm assuming it works just like a point and really just passes the address of where I want data to be stored. So when I say the return value of open_bmp() I'm actually not thinking very straight. However, I'm assuming when I assign img to images[], I'm actually assigning the address of img, which never changes from one file to the next.

How do I pass the value of img and not the address when we are speaking of double pointers?
Posted
Updated 17-Oct-19 7:48am
v4
Comments
CPallini 17-Oct-19 13:19pm    
You have tagged the question as 'C++' but your code is C. Do you really need to use C? Couldn't you use C++, instead?
Moreover, the posted code is not complete (for instance it lacks the verify_bmp function definition), we cannot debug it, as it stands.
Nightpoison 17-Oct-19 13:30pm    
@CPallini yea sorry about that. I just changed it to C. habbit. I didn't want to load all the code as there is a lot more. verify_bmp can be ignored in regards to testing. The code in its original format works just fine, meaning if I want to open an image process it then open the next image and process it works great. I'm attempting to open all images first, then process each one in sequence.
Nightpoison 17-Oct-19 13:34pm    
CPallini I think I just figured it out. Its a pointer issue. The index's of my images array each have a unique address. I just printed them out. However, when I print the first address of the next level images[i][0] all addresses are the same.

1 solution

You are reusing the same block of data (img) to store the image you read from the file.
So each time you go round the loop reading a new file, you overwrite the bmp from the previous one, and all the image values end up pointing at the same memory, which contains the last image you loaded.
 
Share this answer
 
Comments
Nightpoison 17-Oct-19 13:48pm    
OriginalGriff haha you beat me to it. I just updated my original post and said the same thing, I think...more or less.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900