Hi. I'm trying to write a Windows program that draws a grid on the screen using Dev-C++ 4.9.9.2. So, I made this struct to represent each grid coordinate:
Code:
struct GridPoints
{
SHORT x;
SHORT y;
SHORT xAfterPhysics;
SHORT yAfterPhysics;
};
I wrote a couple functions to dynamically allocate enough memory to hold the amount of GridPoints structs needed to fill the screen with points for every 10 pixels(defined by GRID_DETAIL), and to assign coordinates to each of them. The program segfaults every time, for some reason. I tested my coordinate assignment function to see if it was jumping ahead out of the bounds of the allocated memory, but I'm almost certain that it works flawlessly. Here's my code for the memory allocation:
Code:
struct GridPoints* AllocatePoints(HWND MainhWnd)
{
WINDOWINFO MainhWndInfo;
LONG AllocationSize;
struct GridPoints *MemPointsStart = NULL;
MainhWndInfo.cbSize = sizeof(WINDOWINFO);
/*Calculate how many GridPoints structures are needed to hold
/*the information for the grid.*/
GetWindowInfo(MainhWnd, &MainhWndInfo);
pointsx = MainhWndInfo.rcClient.right / GRID_DETAIL;
pointsy = MainhWndInfo.rcClient.bottom / GRID_DETAIL;
total_points = pointsx * pointsy;
/*Allocate the memory for the GridPoints structures
/*to hold the grid points.*/
AllocationSize = total_points * GRID_POINT_SIZE;
MemPointsStart = (struct GridPoints*)malloc(AllocationSize);
if (MemPointsStart == NULL)
{
MessageBox(MainhWnd, "Unable to allocate points memory.", "Allocation Error",
MB_OK);
return NULL;
}
else
{
return MemPointsStart;
}
}
I doubt the value of GRID_DETAIL is the problem, because I've tried higher values in order to allocate less memory and I still got the segfault. When it's equal to 10, my coordinate assignment function segfaults on the 449th iteration, where there should be 3072 points in my 640x480 window.
If anyone has any idea what's going on here, please tell me.