[gnu.gcc.bug] A bug with gcc-1.34 on ultrix 2.0-1

JDEIFIK@ISI.EDU (Jeff Deifik) (04/18/89)

I have a program that works with ultrix cc, but doesn't with gcc.
The program bubble-sorts a linked list of structures, by taking in
a pointer to the first structure. The passed in pointer, points to
the sorted list when the function returns, but gcc returns the wrong pointer.
The bug is evident with and without the optimizer.
The code is below.

	Jeff Deifik	jdeifik@isi.edu

/* These functions sort linked lists. Written by Jeff Deifik 4/17/89.	*/

#include	<stdio.h>

struct	linke
{
    int			comp;		/* The comparison field.*/
    struct linke *	next;		/* Pointer to next element.*/
} ;

typedef	struct	linke	FLOU;
typedef	FLOU *		FLOUP;

#ifdef	__STDC__
extern unsigned long int VAXRAND(int);
extern int Length(FLOUP ptr);
extern void Bubblesort(FLOUP ptr);
extern void printptr(FLOUP foo);
#else
extern unsigned long int VAXRAND();
extern int Length();
extern void Bubblesort();
extern void printptr();
#endif

/* A function used to determine the length of a linked list.		*/
int
#ifdef	__STDC__
Length(FLOUP ptr)
#else
Length(ptr)
FLOUP	ptr;
#endif
{
int	i;
FLOUP	x;

    if (ptr == NULL) return(0);

    i = 1; x = ptr;
    while(x->next != NULL)
    {
	x = x->next;
	i++;
    }
    return(i);
}

/* A function used to bubble-sort linked lists.				*/
/* It is faster when the list is small enough.				*/
void
#ifdef	__STDC__
Bubblesort(FLOUP ptr)
#else
Bubblesort(ptr)
FLOUP	ptr;
#endif
{
int	upper,i,j;
FLOUP	a,b,c,d;

    if (ptr == NULL) return;
    upper = Length(ptr) - 1;
printf("DEBUG upper %d\n",upper);
    while (upper > 0)
    {
	j = 0;
	a = ptr;
	for (i = 0; i < upper; i++)
	{
printf("DEBUG i is %d\n",i);
	    b = a->next;
printf("DEBUG a->comp %d b.comp %d\n",a->comp,b->comp);
	    if (a->comp > b->comp)
	    {				/* Switch elements.*/
printf("DEBUG greater i is %d\n",i);
		d = b->next;
		if (i == 0) { ptr = b; ptr->next = a; }
		else { c->next = b; b->next = a; }
		a->next = d;
		c = a; a = b; b = c;	/* Switch pointers.*/
		j = i;			/* Save last switch.*/
	    }
	    c = a;
	    a = b;
printptr(ptr);
printf("Ptr.comp is %d\n",ptr->comp);
	}
	upper = j;		/* Array is totally sorted up to here.*/
    }
}

#define	SIZEA	5
FLOU	a[SIZEA];
void	main()
{
int	i;

    VAXRAND(1);
    for (i = 0; i < SIZEA; i++)
    {
	if (i == SIZEA - 1)
	    a[i].next = NULL;
	else
	    a[i].next = &(a[i+1]);
	a[i].comp = (int)VAXRAND(0) / 1000;
    }
    printptr(a);
    printf("Length is %d\n",Length(a));
    Bubblesort(a);
    printf("Bubble\n");
    printptr(a);
}

void
#ifdef	__STDC__
printptr(FLOUP foo)
#else
printptr(foo)
FLOUP	foo;
#endif
{
    if (foo != NULL)
    {
	printf("%d\n",foo->comp);
	printptr(foo->next);
    }
}

#include	<sys/time.h>

unsigned long int VAXRAND(set)
register int			set;
{
static	char			name[8] = "VAXRAND";
static unsigned long int	store;
struct	timeval			tp;		/* For timing.*/
struct	timezone		tzp;
int				gettimeofday();

    if (set)				/*Set store to system time.*/
    {
	if (gettimeofday(&tp,&tzp) != 0)
	    exit(1);
	store = tp.tv_usec * tp.tv_sec;	/* Set store sec * microsec.*/
    }
    store = 69069*store+1;		/*Generate the next random number.*/
    return(store);
}
-------