Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

[q]memory problem

Posted on 1998-04-07
2
Medium Priority
?
271 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
  • 2
2 Comments
 
LVL 11

Accepted Solution

by:
alexo earned 200 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 learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

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.  …
In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
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…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

810 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