Technical article 100837

Why is malloc(S) causing a memory fault in SCO OpenServer 5.x?

OpenServer 5.0.0OpenServer 5.0.2

Written · Last revised

Problem

I am getting a memory fault in my C program. When I use a debugger on this I see the instruction that appears to cause the problem is malloc(S). Why would malloc cause a memory fault in SCO OpenServer 5.0?

Cause

One common cause of this problem is a previous use of free(S) without a corresponding malloc call. In other words, free() has been called on some unallocated memory. When debugging this on SCO OpenServer 5.0, this will be seen as a memory fault in _real_malloc; that is, _real_malloc is the last instruction on the stack.

NOTE: The act of freeing up unallocated memory causes different
behavior on different releases of the development system.
For example, in SCO XENIX 2.3.4 the program may execute completely
with no errors. In SCO UNIX Version 4.2, the memory fault will
appear in the free() call.  In all cases this can be the result
of the above cause.

The following sample program illustrates the problem:

#include <stdio.h>
main(){
struct anyolstruct {
        int num;
        float fl;
};
struct anyolstruct * sp;
printf("Start\n");
free();
printf("After bogus free\n");
sp=(struct anyolstruct*)malloc(sizeof(struct anyolstruct));
printf("Done\n");
}

Solution

Make sure that each call to free() corresponds to a malloc() call before it.

See also

TA100247, "Process keeps growing because free(S) is not de-allocating memory."

TA100754, "Why does process size not decrease when using malloc(S) and free(S)?"

From the Xinuos technical article database, record 100837. Reproduced with permission. The original is at www.sco.com/ta/100837, which is reachable but blocked from search engines. Xinuos marks this article against a retired product line. That refers to the product’s lifecycle, not to whether the advice still works — plenty of it still applies to running systems.

Next step

Need this fixed rather than explained?

Tell us the release, what the system runs and what it is doing. We will reply by email with what we would do next.

  • You deal with the people who do the work
  • No obligation, and no upgrade required before we will help
  • We tell you plainly if something is not worth doing

Send an enquiry

We reply by email. If a server is down, say so in the first line. You can also write to help@scoexpert.com.