I am writing a program where I need to take 2 text files and see where the smaller image is inside the bigger one. For this I need to use 2 dimensional arrays. This has all been going fine when I was just using one however now that I have populated the second array with the data from the smaller image I am getting an error saying:
Unhandled exception at 0x77338519 (ntdll.dll) in Wheres Wally.exe: 0xC0000374: A heap has been corrupted (parameters: 0x773758A0).
I have managed to narrow it down to one line in particular which is when the second array is actual given values
//Array Containing Initial Values Of The Base Image
double* baseImage = new double(largeImageRowSize * largeImageCollumnSize);
//Array Containing Values Of The Small Image
double* wallyImage = new double(smallImageRowSize * smallImageCollumnSize);
//Fill BaseImage with all values from the text file
baseImage = read_text("Cluttered_scene.txt", 1024, 768);
//Allocate 36 arrays for each row (so 49x36 arrays)
for (int i = 0; i < getLargeRowSize(); i++)
a2dArray[i] = new double[getLargeCollumnSize()];
//Put data of image into 2d array
int largeImageCounter = 0;
for (int y = 0; y < getLargeCollumnSize(); y++) {
for (int x = 0; x < getLargeRowSize(); x++) {
a2dArray[y][x] = baseImage[largeImageCounter];
largeImageCounter++;
//cout << a2dArray[x][y];
}
}
//Fill wallyImage array with all values of the small wally text file
wallyImage = read_text("Wally_grey.txt", 49, 36);
//Allocate 36 arrays for each row (so 49x36 arrays)
for (int i = 0; i < getSmallRowSize(); i++)
a2dArrayForWally[i] = new double[getSmallCollumnSize()];
//Put data of image into 2d array
int smallImageCounter = 0;
for (int y = 0; y < getSmallCollumnSize(); y++) {
for (int x = 0; x < getSmallRowSize(); x++) {
a2dArrayForWally[y][x] = wallyImage[smallImageCounter];
smallImageCounter++;
//cout << a2dArray[x][y];
}
}
The line giving the error is within the final for loop
a2dArrayForWally[y][x] = wallyImage[smallImageCounter];
So obviously this is something to do with where the memory is being stored, but I'm new to C++ and after googling I cant seem to find whats wrong with my code.
Any help would be greatly appreciated!
Edit:
Through trying to solve the error myself I have discovered that the issue arises when the smallImageCounter
reaches 430. Before that data is stored with no issues
You confused new double()
with new double[]
. The first one allocates a single double
and initializes it to the value from the parenthesis, where the second one allocates a dynamic array of double
s of a size from the square brackets.
Change:
double* baseImage = new double(largeImageRowSize * largeImageCollumnSize);
double* wallyImage = new double(smallImageRowSize * smallImageCollumnSize);
to:
double* baseImage = new double[largeImageRowSize * largeImageCollumnSize];
double* wallyImage = new double[smallImageRowSize * smallImageCollumnSize];
User contributions licensed under CC BY-SA 3.0