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)?"