Mysterious memory allocation problem

This is a discussion on Mysterious memory allocation problem within the C Programming forums, part of the General Programming Boards category; 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 ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    4

    Mysterious memory allocation problem

    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.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    And what is GRID_DETAIL and GRID_POINT_SIZE constants?
    What about rounding while dividing
    MainhWndInfo.rcClient.right / GRID_DETAIL
    ?

    You should not cast malloc in C (see FAQ)
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    4
    GRID_DETAIL is 10, and GRID_POINT_SIZE is sizeof(struct GridPoints)

    I get the segfault even if I don't cast malloc().
    I doubt that
    MainhWndInfo.rcClient.right / GRID_DETAIL
    would have a rounding error because there probably wouldn't be a difference like memory for 448 points instead of for 3072 points if that were the case. I also ran it through the debugger, and the pointsx and pointsy variables have the expected values: 64 for pointsx, and 48 for pointsy.

    I coded my program to execute the grid code with the WM_CREATE message. Is this the problem?
    Last edited by TomServo1; 07-07-2007 at 10:42 PM.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Shaw the code that uses the allocated array
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    > GetWindowInfo(MainhWnd, &MainhWndInfo);
    Check the return result of this function.

    > my coordinate assignment function segfaults on the 449th iteration
    Show us this code.

    > I coded my program to execute the grid code with the WM_CREATE message. Is this the problem?
    So long as all your pointers to allocated memory are stored in persistent memory (not a local variable in a message handler say), then it should be ok.

    How many other malloc /realloc / free calls are there before this one?
    All of those need to be checked as well. Often, memory corruption has a delayed effect so that the problem you're seeing could have nothing to do with the immediate code, but is actually caused by some earlier mistake.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    4
    There aren't any other malloc calls than the one I showed. It's supposed to execute only once, with WM_CREATE. The value that's returned by AllocatePoints() goes into a global pointer, so it should be fine.
    Here's the code for the coordinate assignment.

    Code:
    void AssignPointsCoor(struct GridPoints *MemPoints)
    {
         int i;
         int j;
         SHORT xcoor = 0;
         SHORT ycoor = 0;
         
         /*segfaults when i=10 and j=16...the 449th loop iteration*/
         for(i=1; i<=pointsx; i++)
         {
              for(j=1; j<=pointsy; j++)
              {
                   xcoor = (GRID_DETAIL * i);
                   ycoor = (GRID_DETAIL * j);
                   
                   MemPoints->x = xcoor;
                   MemPoints->y = ycoor;
                   
                   if(i * j != total_points)
                   {
                        MemPoints += GRID_POINT_SIZE; /*get the pointer to point to the next grid point*/
                   }
              }
         }
    }
    For the record, pointsx, pointsy, and total_points are all static LONG variables that are the same ones used in the memory allocation function. GRID_DETAIL is a static unsigned because I guess you can't do any operations on a #define.
    Last edited by TomServo1; 07-08-2007 at 10:50 AM.

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Code:
    MemPoints += GRID_POINT_SIZE; /*get the pointer to point to the next grid point*/
    Uh.....

    MemPoints is a pointer to a block of memory of a bunch of struct GridPoints, right?

    Then do this:

    Code:
    MemPoints++;
    Review pointer arithmetic. Adding x to a pointer actually increases it by (x * sizeof(type)), where type is the data type that the pointer points to.

    Also memory and arrays should start with index 0, not index 1.

    In addition, you really should be treating MemPoints as an array... Something like:

    Code:
    for(i=0;i<total_points;i++)
    {
    	/* use MemPoints[i] as your current point. */
    }

  8. #8
    Registered User
    Join Date
    Jul 2007
    Posts
    4
    Thanks, that got rid of the segfault. I'll try to look into some pointer arithmetic, too.
    I coded that function with 2 for loops because I thought it was the easiest way to show exactly what was going on in the code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with linked list and shared memory
    By Sirfabius in forum C Programming
    Replies: 10
    Last Post: 11-10-2008, 03:45 PM
  2. Memory leaks problem in C -- Help please
    By Amely in forum C Programming
    Replies: 14
    Last Post: 05-21-2008, 11:16 AM
  3. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  4. Understanding Memory Allocation
    By Ragsdale85 in forum C Programming
    Replies: 7
    Last Post: 10-31-2005, 07:36 AM
  5. memory allocation problem with 2 dimensional array
    By nano_nasa in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2002, 11:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21