This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Possible bug with initialization.


The initialization of aexp3 shown below does not work.
---------------------------------------------------------------------
expTreeTyp	aexp1 = { valNode , { 1 } };
expTreeTyp	aexp2 = { valNode , { 2 } };
expTreeTyp	aexp3 = { oprNode , { { IADD , & aexp1 , & aexp2 } } };
---------------------------------------------------------------------

The entire program is attaced as aet.c
The output of "cc -v aet.c" is attached next.

The correct evaluation of aexp3 should have been 3.
The output of the program is as follows:
---------------------------------------------------------------------
[chitta@csehcl56 test]$ ./a.out 
The value of aexp1 is: 1
The value of aexp2 is: 2
The value of aexp3 is: 1073955171
---------------------------------------------------------------------

The type definition of expTreeTyp is shown below.

typedef enum {
	IDIV, IMUL, IADD, ISUB
} iopsTyp;

typedef enum {
	valNode, oprNode
} expTNodeFlag;

typedef struct expTreeStu {
expTNodeFlag			flag;
union {
	int			ival;
	struct {
	  iopsTyp		opr;
	  struct expTreeStu	*lChild, *rChild;
	} opNod;
} aetNode;
} expTreeTyp, *expTreePtr;
----------------------------------------------------------------------

Your initial comments on this possible bugs would be very much
appreciated.


Best regards,            Dial-in: 77393 `*' <extn.> or 8 <extn.>
Chitta                   Tel: +91 3222 <dial-in> Extn: (off) 3498, (res) 3499
Dept. of CSE, IIT, Kgp.  Fax: +1 801 5159121, +44 870 2842932, +91 3222 55303
#include <stdio.h>

typedef enum {
	IDIV, IMUL, IADD, ISUB
} iopsTyp;

typedef enum {
	valNode, oprNode
} expTNodeFlag;

typedef struct expTreeStu {
expTNodeFlag			flag;
union {
	int			ival;
	struct {
	  iopsTyp		opr;
	  struct expTreeStu	*lChild, *rChild;
	} opNod;
} aetNode;
} expTreeTyp, *expTreePtr;

int	aetEval (expTreePtr tP) {
switch ( tP->flag ) {
   case valNode :
	return tP->aetNode.ival;
   case oprNode :
	switch ( tP->aetNode.opNod.opr ) {
	   case IDIV : return aetEval ( tP->aetNode.opNod.lChild ) /
			      aetEval ( tP->aetNode.opNod.rChild );
	   case IMUL : return aetEval ( tP->aetNode.opNod.lChild ) *
			      aetEval ( tP->aetNode.opNod.rChild );
	   case IADD : return aetEval ( tP->aetNode.opNod.lChild ) +
			      aetEval ( tP->aetNode.opNod.rChild );
	   case ISUB : return aetEval ( tP->aetNode.opNod.lChild ) -
			      aetEval ( tP->aetNode.opNod.rChild );
	}
}
}

main ( ) {
expTreeTyp	aexp1 = { valNode , { 1 } };
expTreeTyp	aexp2 = { valNode , { 2 } };
expTreeTyp	aexp3 = { oprNode , { { IADD , & aexp1 , & aexp2 } } };


printf ( "The value of aexp1 is: %d\n", aetEval ( & aexp1 ) );
printf ( "The value of aexp2 is: %d\n", aetEval ( & aexp2 ) );
printf ( "The value of aexp3 is: %d\n", aetEval ( & aexp3 ) );
}
[chitta@csehcl56 test]$ cc -v aet.c 
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/2.7.2.3/specs
gcc version 2.7.2.3
 /usr/lib/gcc-lib/i386-redhat-linux/2.7.2.3/cpp -lang-c -v -undef -D__GNUC__=2 -D__GNUC_MINOR__=7 -D__ELF__ -Dunix -Di386 -Dlinux -D__ELF__ -D__unix__ -D__i386__ -D__linux__ -D__unix -D__i386 -D__linux -Asystem(unix) -Asystem(posix) -Acpu(i386) -Amachine(i386) aet.c /tmp/cc2efnMc.i
GNU CPP version 2.7.2.3 (i386 Linux/ELF)
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /usr/i386-redhat-linux/include
 /usr/lib/gcc-lib/i386-redhat-linux/2.7.2.3/include
 /usr/include
End of search list.
 /usr/lib/gcc-lib/i386-redhat-linux/2.7.2.3/cc1 /tmp/cc2efnMc.i -quiet -dumpbase aet.c -version -o /tmp/cc2efnMc.s
GNU C version 2.7.2.3 (i386 Linux/ELF) compiled by GNU C version 2.7.2.3.
aet.c: In function `main':
aet.c:43: warning: braces around scalar initializer for `aexp3.aetNode.ival'
aet.c:43: warning: excess elements in scalar initializer after `aexp3.aetNode.ival'
aet.c:43: warning: excess elements in scalar initializer after `aexp3.aetNode.ival'
 as -V -Qy -o /tmp/cc2efnMc1.o /tmp/cc2efnMc.s
GNU assembler version 2.9.1 (i386-redhat-linux), using BFD version 2.9.1.0.23
 ld -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 /usr/lib/crt1.o /usr/lib/crti.o /usr/lib/gcc-lib/i386-redhat-linux/2.7.2.3/crtbegin.o -L/usr/lib/gcc-lib/i386-redhat-linux/2.7.2.3 -L/usr/i386-redhat-linux/lib /tmp/cc2efnMc1.o -lgcc -lc -lgcc /usr/lib/gcc-lib/i386-redhat-linux/2.7.2.3/crtend.o /usr/lib/crtn.o

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]