Solved

[q]memory problem

Posted on 1998-04-07
2
261 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
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

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…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 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