Solved

[q]memory problem

Posted on 1998-04-07
2
267 Views
Last Modified: 2013-12-03
What is a problem of below code..?
When I executed(or only allocate the memory block) by using for loop about 20 times, I met a General Protection Error..( in about 15 times loop, GPE is not shown.)
(x,ypts is an integer about 128-512)
void AllocateMemory (const int id)
{
      int       i;

      if (Matrix[id] != NULL) return;
      if (( id >= 0 ) && ( id < 128 ))
                 {
          Matrix[id] = ( int far * far * ) GlobalAllocPtr ( GHND, ActiveChildInfo.ypts * sizeof(int far *) );

         for ( i = 0; i < ActiveChildInfo.ypts; i++ )
            Matrix[id][i] = ( int far * ) GlobalAllocPtr ( GHND, ActiveChildInfo.xpts * sizeof(int) );
      }
0
Comment
Question by:lsjun
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 11

Accepted Solution

by:
alexo earned 100 total points
ID: 1412584
I assume you're programming for 16-bit Win3.x, otherwise there's no need to use "far", and you should use other memory allocation functions instead of GlobalAllocPtr().

You could be running out of memory.  If x and y are 512 you're allocating about 10MB (excluding the OS overhead).  Increase the size of your swap file.  Check that you're unlocking and freeing the memory.  Use GetFreeSpace() to check remaining memory.  Check the return values of GlobalAllocPtr() calls.  Or better yet, use separate GlobalAlloc() and GlobalLock() calls and check the return values vs. NULL.

Also, free tips:

The check for matrix bounds:
        if (( id >= 0 ) && ( id < 128 ))
should come BEFORE the dereferencing:
        if (Matrix[id] != NULL)

"const int id" is redundant.  Simple built-in types (int, etc.) are always const.

0
 
LVL 11

Expert Comment

by:alexo
ID: 1412585
If you're writing for Win32, use HeapAlloc() or even malloc() or new.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question