Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Strange behaviour? |
| Date | 2011-11-15 14:43 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <4EC2C0F1.7050209@verizon.net> (permalink) |
| References | <ae447417-7ce0-420a-84be-620f6cbedf7d@p2g2000vbj.googlegroups.com> |
On 11/15/2011 02:31 PM, dmjcunha wrote:
> In the code below something odd happens when it is called with:
> merge (a[], lledge=13, lredge=15, lcedge=16, rcedge=17, rledge=18,
> rredge=19)
>
> I will post the gdb output to show the behaviour:
>
> Breakpoint 2, merge (a=0xbffff24c, lledge=13, lredge=15, lcedge=16,
> rcedge=17, rledge=18, rredge=19) at ex8.11.c:91
> 91 for(lri = rmid + 1; lri > rledge; lri--)
> (gdb) p rmid
> $1 = 18
> (gdb) s
> 92 aux[lri - 1] = a[lri - 1];
> (gdb) p rmid
> $2 = 18
> (gdb) s
> 91 for(lri = rmid + 1; lri > rledge; lri--)
> (gdb) p rmid
> $3 = 18
> (gdb) s
> 93 for(rrj = rmid; rrj < rredge; rrj++)
> (gdb) p rmid
> $4 = 18
> (gdb) s
> 94 aux[rredge + rmid - rrj] = a[rrj + 1];
> (gdb) p rmid
> $5 = 18
> (gdb) s
> 93 for(rrj = rmid; rrj < rredge; rrj++)
> (gdb) p rmid
> $6 = 10 <-------------- here I don't understand. There was no
> computation with rmid and it changed strangely to 10.
>
> A piece of the function I think you could help me:
>
> void merge(int a[], int lledge, int lredge, int lcedge, int rcedge,
> int rledge, int rredge)
> {
> int aux[N - 1], lmid, cmid, rmid, k;
I'd recommend printing out the values that you subscript aux[] with. I
think you'll find that one or more of them is negative or exceeds N-2.
That would render the behavior undefined. On many implementations, the
undefined behavior takes the form of having aux[N-1] be an alias for
lmid, aux[N] being an alias for cmid, and aux[N+1] be an alias for rmid,
etc, which would explain the symptoms you're seeing.
What this implies is that this function should validate the values of
the parameters that determine what range of values is used to access
aux[]. You should give it a return type, and have it return an error
code if one of those values is out of range. The calling routine should
pay attention to this error code, and deal with it in whatever manner
seems appropriate.
> int lli, lrj, lci, rcj, lri, rrj;
> int minor;
> char letter;
> struct attempt {
> int min;
> char let; };
> struct attempt lattempt1, cattempt2, rattempt3;
> struct attempt *lattempt, *cattempt, *rattempt;
>
> lattempt = &lattempt1;
> cattempt = &cattempt2;
> rattempt = &rattempt3;
>
> lmid = (lledge + lredge) / 2;
> cmid = (lcedge + rcedge) / 2;
> rmid = (rledge + rredge) / 2;
>
> for(lli = lmid + 1; lli > lledge; lli--)
> aux[lli - 1] = a[lli - 1];
> for(lrj = lmid; lrj < lredge; lrj++)
> aux[lredge + lmid - lrj] = a[lrj + 1];
>
> for(lci = cmid + 1; lci > lcedge; lci--)
> aux[lci - 1] = a[lci - 1];
> for(rcj = cmid; rcj < rcedge; rcj++)
> aux[rcedge + cmid - rcj] = a[rcj + 1];
>
> for(lri = rmid + 1; lri > rledge; lri--)
> aux[lri - 1] = a[lri - 1];
> for(rrj = rmid; rrj < rredge; rrj++)
> aux[rredge + rmid - rrj] = a[rrj + 1];
>
> for(k = lledge; k <= rredge; k++) {
> ...
>
> Thanks in advance!
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Strange behaviour? dmjcunha <dmjcunha@gmail.com> - 2011-11-15 11:31 -0800
Re: Strange behaviour? James Kuyper <jameskuyper@verizon.net> - 2011-11-15 14:43 -0500
Re: Strange behaviour? Seebs <usenet-nospam@seebs.net> - 2011-11-15 19:26 +0000
Re: Strange behaviour? dmjcunha <dmjcunha@gmail.com> - 2011-11-15 13:15 -0800
Re: Strange behaviour? Keith Thompson <kst-u@mib.org> - 2011-11-15 14:33 -0800
Re: Strange behaviour? Willem <willem@toad.stack.nl> - 2011-11-15 22:35 +0000
Re: Strange behaviour? dmjcunha <dmjcunha@gmail.com> - 2011-11-16 03:37 -0800
Re: Strange behaviour? Willem <willem@toad.stack.nl> - 2011-11-16 13:34 +0000
Re: Strange behaviour? Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-11-16 06:07 -0800
Re: Strange behaviour? Kenneth Brody <kenbrody@spamcop.net> - 2011-11-16 13:13 -0500
Re: Strange behaviour? Kaz Kylheku <kaz@kylheku.com> - 2011-11-16 19:12 +0000
Re: Strange behaviour? dmjcunha <dmjcunha@gmail.com> - 2011-11-16 13:14 -0800
Re: Strange behaviour? Seebs <usenet-nospam@seebs.net> - 2011-11-16 22:18 +0000
csiph-web