Doing some C++ for fun and have a problem where when I load an Image after doing some modifications to the image, it gives me segmentation fault. I feel like I'm missing something but I don't know where.
EDIT Here's the code for both the save and load function, (assume that all necessary header files are included):
int Image::save(const char* filename)
{
if(filename == NULL)
{
return 1;
}
///*
ofstream outFile(filename, ios::out | ios::binary);
if (!outFile)
{
return 1;
}
outFile.write(reinterpret_cast<char*>(&cols), sizeof(unsigned int));
outFile.write(reinterpret_cast<char*>(&rows), sizeof(unsigned int));
outFile.write(reinterpret_cast<char*>(pixels), sizeof(uint8_t) * cols * rows);
outFile.close();
return 0;
}
int Image::load(const char* filename)
{
if(filename == NULL)
{
return 1;
}
///*
ifstream inFile(filename, ios::in | ios::binary);
if (!inFile)
{
return 1;
}
**//feels like the segmentation fault is happening here**
inFile.read(reinterpret_cast<char*>(&cols), sizeof(unsigned int));
inFile.read(reinterpret_cast<char*>(&rows), sizeof(unsigned int));
inFile.read(reinterpret_cast<char*>(pixels), sizeof(uint8_t) * cols * rows);
inFile.close();
return 0;
}
EDIT Here's the header file that I am working with:
class Image {
public:
unsigned int cols;
unsigned int rows;
uint8_t* pixels;
...
/* Saves the image in the file filename. In a format that can be
loaded by load(). Returns 0 on success, else a non-zero error
code. */
int save( const char* filename );
/* Load an image from the file filename, replacing the current
image size and data. The file is in a format that was saved by
save(). Returns 0 success, else a non-zero error code . */
int load( const char* filename );
};