Sun SPARC RISC compiler behavior

Rick Coates rickc at agora.hf.intel.com
Thu Sep 21 06:36:29 AEST 1989


Here is a small program that does some wrong things with structure
passing. It compiles fine on both the Sun 3 (68xxx) and on the Sun 4
(SPARC RISC).  What is interesting is that it also _runs_ fine on the
Sun 4. It core dumps (as expected) on the second subroutine call on
the Sun 3.  Any explanations or comments?

Rick Coates
Consulting H/W - S/W engineer
(Graphics - Sun - Unix - ASIC design - imbedded systems)

...!tektronix!tessi!agora!rickc

main.c:

    struct x { int x; int y; int z; };


    main()
    {
	struct x test;

	test.x = 111111;
	test.y = 222222;

	test.z = 333333;
	sub1(&test);			/* this routine expects reference */
	printf("z: %d\n",test.z);

	test.z = 333333;		/* WRONG */
	sub1(test);			/* this routine expects reference */
	printf("z: %d\n",test.z);

	test.z = 333333;		/* WRONG */
	sub2(&test);			/* this routine expects structure */
	printf("z: %d\n",test.z);

	test.z = 333333;
	sub2(test);			/* this routine expects strucuture */
	printf("z: %d\n",test.z);
    }


sub.c:


    struct x { int x; int y; int z; };


    sub1(st)
    struct x *st;
    {
	printf("x: %d y: %d\n",st->x,st->y);
	st->z = 666;
    }

    sub2(st)
    struct x st;
    {
	printf("x: %d y: %d\n",st.x,st.y);
	st.z = 666;
    }

results:
(comilation: cc -c main.c ; cc -c sub.c ; cc -o main main.o sub.o)
(compiled on Sun 4 under SunOS v. 4.01)

    x: 111111 y: 222222		(passed as reference - used as reference)
    z: 666

    x: 111111 y: 222222 	(passed as value     - used as reference)
    z: 333333

    x: 111111 y: 222222		(passed as reference - used as value)
    z: 666

    x: 111111 y: 222222 	(passed as value     - used as value)
    z: 333333



More information about the Comp.sys.sun mailing list