Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

[q]memory problem

Posted on 1998-04-07
2
Medium Priority
?
270 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 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 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

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

609 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