malloc and segmentation errors (in funiq) - help!!!

Bob Peirce rbp at investor.UUCP
Thu Jul 25 05:00:00 AEST 1985


I need help on segmentation errors I get when trying to run a
program called funiq which was submitted to the net a month
ago as:

Message-ID: <197 at ur-cvsvax.UUCP>
>From bill at ur-cvsvax.UUCP (Bill Vaughn) Thu Jun 27 14:00:54 1985

I am running Altos' port of Sys III to the M68000.  This seems
fairly clean except the C compiler treats integers as 16 bits
so we have to be very careful about software that assumes pointers
are integers.  Therefore, the first thing I did was to run lint.
This checked out perfectly except for the usual comments about
possible pointer problems with malloc and anything=NULL.

The code which seems to cause the problem is the following:

 * Returns pointer to 'n' bytes for character string storage.
char *allocstr(n)
	register char *x;

	x = malloc((unsigned)n);
	if (x == NULL) {
		fprintf(stderr,"funiq: not enough memory for strings\n");

 * Returns a pointer to the next available string space.
 * Gets more space if necessary.
char *nextstr(s)
register char *s;
	register char *x;
	register int i;

#ifdef STATS
	charsused += (i = strlen(s) + 1);
	i = strlen(s) + 1;

	if ((x = s + i) >= cend) {
		x = allocstr(ADDSTR);
		cend = x + ADDSTR;

The program runs for a while, and will find a number of unique words
before crashing.  (Interestingly, sort -u also crashes on our system at
about the 750th word).  By changing the definitions for  initial tree
nodes and string space along with additional tree nodes and string
space, I discovered the problem seems to occur when additional string
space is requested.

The test file I used was 6463 words, one word/line, obtained from
running deroff -w on the netnews manual pages.  The statistics I
obtained were as follows:

				char	nodes	total	com-	max
NODES	STR	ADDNOD	ADDST	used	used	words	pares	depth
-----	---	------	-----	----	-----	-----	-----	-----
1024	4096	128	512	4091	 576	1940	19927	  24
 256	1024	128	512	1014	 153	 333	 2822	  17
 128	1024	 64	512	1014	 153	 333	 2822	  17
 Same with long instead of int	2044	 297	 766	 7272	  21
Clearly all the initial node and string space gets allocated, and
additional nodes get allocated, but additional string space doesn't
get allocated.  For the last test I changed most integers to longs,
since my integers are 16 bit.  This got me one additional string
allocation, but it died on the next call to allocstr.

As a further test, I set STR to 32768.  It crashed on the initial call
to allocstr.  I tried STR = 16384 and it ran to completion!  Obviously,
I will leave STR at 16384 for now, but can anyone tell me what is
going on here?

		 	Bob Peirce, Pittsburgh, PA
		uucp: ...!{allegra, bellcore, cadre, idis}

		NOTE:  Mail must be < 30,000 bytes/message

More information about the Comp.lang.c mailing list