malloc/stack conflicts

Michael S. Fischbein msf at amelia.nas.nasa.gov
Wed Feb 22 07:39:55 AEST 1989


I have a program I am working on that passes an argv-like list of arguments
to itself recursively.  It mallocs memory to deal with the intermediate
structures.  I am at a point where the pointer returned by malloc with
the correct size requested overlaps the parameter for the current
invocation.  Does anyone have any suggestions on where I can look to
fix this?

More succinctly, I have a routine

void foo(i, list, j)
	int	i;
	char	*list[];
	int	j;
{
	struct bar	*abar;

	abar = (struct bar *) malloc((unsigned) (j-i)*sizeof(struct bar));

/* stuff that gets each string from list and constructs the correct
 * struct bar */
/* etc */
}
where the abar returned by malloc is such that the list parameter is
trashed after the first couple of calls (it is fine on invocation, both
by printfs and dbx).  If I duplicate the malloc line, that is, allocate
double the memory I need and start in the second half, the routine
works fine.

This is on a Sun 3/60 under SunOS 3.4.  Does anyone know where I should
look?
		mike

-- 
Michael Fischbein                 msf at prandtl.nas.nasa.gov
                                  ...!seismo!decuac!csmunix!icase!msf
These are my opinions and not necessarily official views of any
organization.



More information about the Comp.unix.wizards mailing list