问题
I currently try to learn how to use the CImg library but I get an error that I do not quite understand. The documentation of the constructors in the CImg library were also rather unhelpful. The basic idea is that I want to create an image so that I can write the pixels in a second step (not shown) manually. However, the access to the image created with the CImg constructor is denied for some reason.
Here is a minimal example:
#include <iostream>
#include <CImg.h>
#include "Header.h"
using namespace std;
int Main() {
cimg_library::CImg<float> img(200, 200, 0, 3, 0.0);
cout << "img is: " << img(0, 0, 0, 0) << endl; //<----------------- error occurs here
return 0;
}
The error reads: Exception thrown at 0x0067B693 in Test.exe: 0xC0000005: Access violation reading location 0x00000000
Any help in understanding this would be much appreciated!
Best
Blue
#Edit: I tried 2 more things but both don't work unfortunately.
1st: I tried the .data() function as well but to no avail. Changing data types (float, int, unsigned char) also did not solve the problem, apart from giving the error message that the whole thing would point to a NULL vector now (still access denied).
2nd: I switched to using pointers:
cimg_library::CImg<unsigned char>* img = new cimg_library::CImg<unsigned char>(200, 200, 0, 3, 1);
cout << "img is: " << *img->data(0, 0, 0, 0) << endl;
This still gives pretty much the same error message though: Exception thrown: read access violation. cimg_library::CImg::data(...) returned nullptr.
回答1:
Dont set 0 but 1 for the number of slices. Otherwise you get an empty image 0x0x0x0 that has no pixels, leading to a wrong memory access.
来源:https://stackoverflow.com/questions/60685142/c-cimg-access-violation-reading-location