You have to declare that variable before using it...
Pixel * tempPixel = new Pixel[image.infoHeader.biWidth * image.infoHeader.biHeight];
Notice that you must deallocate the temporary array at the end of the function with delete[]
(otherwise you have a memory leak). To make this automatic and avoid issues with exception safety, you should be using a smart pointer, like scoped_array<Pixel>
from Boost or (if you have a compiler that supports the new C++ standard) unique_ptr<Pixel[]>
.
Even better: you could just use a std::vector<Pixel>
std::vector<Pixel> tempPixel(image.infoHeader.biWidth * image.infoHeader.biHeight);
and let it deal with allocation/deallocation.
Preemptive answer correction (due to your new question): if in the end you are going to assign tempPixel
to image.pixels
, then you must not delete[]
tempPixel
, otherwise image
will be replaced with a pointer to deallocated memory.
But you have bigger problems: when you replace image.pixels
you are not deallocating the memory it was pointing to previously. So you should deallocate that memory and then assign tempPixel
to it.
All this assuming that image.pixels
was allocated with new
and is going to be deallocated with delete[]
(otherwise you get a mismatch of allocation functions/operators).
By the way, if your image is just some kind of wrapper for a Windows DIB (BMP) as it seems from the header fields names you are not taking into account the fact that pixel lines are 4-byte aligned (so, if your image is not 32bpp, you must allocate more memory and perform the pixel copy accordingly).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…