Solved

[q]memory problem

Posted on 1998-04-07
2
260 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 100 total points
Comment Utility
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
Comment Utility
If you're writing for Win32, use HeapAlloc() or even malloc() or new.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

728 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now