Path: utzoo!utgpu!jarvis.csri.toronto.edu!clyde.concordia.ca!uunet!snorkelwacker!tut.cis.ohio-state.edu!ucbvax!ucdavis!schell From: schell@llandru.ucdavis.edu (Stephan Schell) Newsgroups: comp.sys.hp Subject: Array indexing problem (bug?) Message-ID: Date: 22 Dec 89 02:27:12 GMT Sender: uucp@ucdavis.ucdavis.edu Distribution: comp Organization: University of California at Davis Lines: 40 While attempting to port the dviselect program in the TeX distribution over to a 9000/835 running hp-ux 3.1, I ran into a problem with array indexing. This problem is showcased in the code below: #include char junk[11] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' , 'j'}; main() { int i; printf("Enter a number between 20 and 29, inclusive: "); scanf("%d", &i); printf("Character selected is '%c'\n", (junk-20)[i]); exit(0); } For example, typing "25" in response to the prompt yields a segmentation fault, even though the final value of (junk-20+i) is (junk+5). The same behavior was observed when "(junk-20)[i]" was replaced by "(junk-20+i)". The program operated properly if "junk[i-20]" was used, or if a temporary pointer, "junk2 = junk-20+i" was computed and then used. The common thing among the usages that failed was that they all used LDBX where the base register contained a value outside the allowable range, even though the effective (or final) address was OK. Thus, it seems as if the segmentation fault is based on the base register contents rather than the effective address. Does this behavior seem bogus to anyone except me? Is there any way to fix this problem without modifying the source code of each and every application that does this sort of thing? Thanks. -- ------------------------------------------------------------------------------- Stephan Schell schell@llandru.ucdavis.edu Dept. of Electrical Engineering {ucbvax,lll-crg}!ucdavis!llandru!schell & Compter Science University of California, Davis (916) 752-1326