Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!rutgers!mailrus!ames!ncar!umigw!steve From: steve@umigw.MIAMI.EDU (steve emmerson) Newsgroups: comp.lang.c Subject: Re: Question about VMS C Summary: apparently arr[i][j] may break Message-ID: <157@umigw.MIAMI.EDU> Date: 30 Aug 88 13:30:57 GMT References: <32725@cca.CCA.COM> Reply-To: steve@umigw.miami.edu (steve emmerson) Followup-To: comp.lang.c Distribution: na Organization: University of Miami Lines: 51 In article <32725@cca.CCA.COM> g-rh@CCA.CCA.COM (Richard Harter) writes: > >char p[256]; >foo () { > int i,j, *s[128]; > for (i=0;i<128;i++) s[i] = (int *)malloc(128*sizeof(int)); > for (i=0;i<128;i++) { > p[i] = ' '; > p[i+128] = ' '; > for (j=0;j<128;j++) s[i][j] = 0; > } > } > >When this code was executed sundry things were zeroed outside of s and >the vectors pointed to by the elements of s. When the double subscripting >was changed to *(s[i]+j) = 0 the code executed correctly. The double >subscripting executes correctly on a host of UNIX machines and on PRIMOS. I ran accross the same problem apparently. Here's the relevant fragment: /* The following two print statements should be identical. They aren't!. */ int i, j; MESH_PT **mp = transform->mesh; /* vector of pointers */ for (j = -1; j <= transform->nj; ++j) { for (i = -1; i <= transform->ni; ++i) { printf("&mp[%d][%d] = %ld", j, i, (long)&mp[j][i]); printf("(mp[%d]+%d) = %ld", j, i, (long)(mp[j]+i)); } } (I appologize for the weird "-1" indexing -- it's for pointing into a 2D array that has 1-point "margins" where mp[0][0] is the first, "true" data point.) The results of the print statements were different (i.e. incorrect) under the VMS C compiler. They were identical (i.e. correct) however, under both the GNU C and EUNICE C compilers. The apparently incorrect behavior, however, was context sensitive. When I extracted the above fragment into its own program, I started getting correct results. Anyone else seen this? -- Steve Emmerson Inet: steve@umigw.miami.edu [128.116.10.1] SPAN: miami::emmerson (host 3074::) emmerson%miami.span@star.stanford.edu UUCP: ...!ncar!umigw!steve emmerson%miami.span@vlsi.jpl.nasa.gov "Computers are like God in the Old Testament: lots of rules and no mercy"