[comp.lang.c] malloc/stack conflicts

msf@amelia.nas.nasa.gov (Michael S. Fischbein) (02/22/89)

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@prandtl.nas.nasa.gov
                                  ...!seismo!decuac!csmunix!icase!msf
These are my opinions and not necessarily official views of any
organization.