Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #13407 > unrolled thread

Strange behaviour?

Started bydmjcunha <dmjcunha@gmail.com>
First post2011-11-15 11:31 -0800
Last post2011-11-16 22:18 +0000
Articles 13 — 8 participants

Back to article view | Back to comp.lang.c


Contents

  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

#13407 — Strange behaviour?

Fromdmjcunha <dmjcunha@gmail.com>
Date2011-11-15 11:31 -0800
SubjectStrange behaviour?
Message-ID<ae447417-7ce0-420a-84be-620f6cbedf7d@p2g2000vbj.googlegroups.com>
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;
     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!

[toc] | [next] | [standalone]


#13409

FromJames Kuyper <jameskuyper@verizon.net>
Date2011-11-15 14:43 -0500
Message-ID<4EC2C0F1.7050209@verizon.net>
In reply to#13407
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!

[toc] | [prev] | [next] | [standalone]


#13411

FromSeebs <usenet-nospam@seebs.net>
Date2011-11-15 19:26 +0000
Message-ID<slrnjc5gl6.1p3j.usenet-nospam@guild.seebs.net>
In reply to#13407
On 2011-11-15, dmjcunha <dmjcunha@gmail.com> wrote:
> $6 = 10    <-------------- here I don't understand. There was no
> computation with rmid and it changed strangely to 10.

Turn off optimization when trying to debug stuff like this.  The
generated code is not necessarily doing everything in the order it was
written in.  For the compiler you're probably using, this is spelled
"-O0".

-s
-- 
Copyright 2011, all wrongs reversed.  Peter Seebach / usenet-nospam@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.

[toc] | [prev] | [next] | [standalone]


#13423

Fromdmjcunha <dmjcunha@gmail.com>
Date2011-11-15 13:15 -0800
Message-ID<0c45cd5f-5093-4a67-8575-67d8bc85be55@p16g2000yqd.googlegroups.com>
In reply to#13411
On Nov 15, 4:26 pm, Seebs <usenet-nos...@seebs.net> wrote:
> On 2011-11-15, dmjcunha <dmjcu...@gmail.com> wrote:
>
> > $6 = 10    <-------------- here I don't understand. There was no
> > computation with rmid and it changed strangely to 10.
>
> Turn off optimization when trying to debug stuff like this.  The
> generated code is not necessarily doing everything in the order it was
> written in.  For the compiler you're probably using, this is spelled
> "-O0".
>
> -s
> --
> Copyright 2011, all wrongs reversed.  Peter Seebach / usenet-nos...@seebs.nethttp://www.seebs.net/log/<-- lawsuits, religion, and funny pictureshttp://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
> I am not speaking for my employer, although they do rent some of my opinions.

Strange, I tried "-O1" and the problem was solved.
James Kuyper, thank you for the explanation.

[toc] | [prev] | [next] | [standalone]


#13430

FromKeith Thompson <kst-u@mib.org>
Date2011-11-15 14:33 -0800
Message-ID<lnfwhp10ew.fsf@nuthaus.mib.org>
In reply to#13423
dmjcunha <dmjcunha@gmail.com> writes:
> On Nov 15, 4:26 pm, Seebs <usenet-nos...@seebs.net> wrote:
>> On 2011-11-15, dmjcunha <dmjcu...@gmail.com> wrote:
>>
>> > $6 = 10    <-------------- here I don't understand. There was no
>> > computation with rmid and it changed strangely to 10.
>>
>> Turn off optimization when trying to debug stuff like this.  The
>> generated code is not necessarily doing everything in the order it was
>> written in.  For the compiler you're probably using, this is spelled
>> "-O0".
[...]>
> Strange, I tried "-O1" and the problem was solved.
> James Kuyper, thank you for the explanation.

If changing the optimization level changes the behavior of your code, it
probably indicates a bug in your code.  Most likely your program's
behavior is undefined.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#13432

FromWillem <willem@toad.stack.nl>
Date2011-11-15 22:35 +0000
Message-ID<slrnjc5q9s.1tp6.willem@toad.stack.nl>
In reply to#13423
dmjcunha wrote:
) Strange, I tried "-O1" and the problem was solved.
) James Kuyper, thank you for the explanation.

No, it wasn't solved.  It was just hidden from view.


SaSW, Willem
-- 
Disclaimer: I am in no way responsible for any of the statements
            made in the above text. For all I know I might be
            drugged or something..
            No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT

[toc] | [prev] | [next] | [standalone]


#13472

Fromdmjcunha <dmjcunha@gmail.com>
Date2011-11-16 03:37 -0800
Message-ID<5d1a49fc-495a-4c29-8e42-c163aa5ceca4@h5g2000yqk.googlegroups.com>
In reply to#13432
On Nov 15, 7:35 pm, Willem <wil...@toad.stack.nl> wrote:
> dmjcunha wrote:
>
> ) Strange, I tried "-O1" and the problem was solved.
> ) James Kuyper, thank you for the explanation.
>
> No, it wasn't solved.  It was just hidden from view.
>
> SaSW, Willem
> --
> Disclaimer: I am in no way responsible for any of the statements
>             made in the above text. For all I know I might be
>             drugged or something..
>             No I'm not paranoid. You all think I'm paranoid, don't you !
> #EOT

Thank you all for the warnings. Anyway I think it is something a
little bit beyond my current level of knowledge. I'll keep studying.
Thanks!

[toc] | [prev] | [next] | [standalone]


#13476

FromWillem <willem@toad.stack.nl>
Date2011-11-16 13:34 +0000
Message-ID<slrnjc7ev3.k9b.willem@toad.stack.nl>
In reply to#13472
dmjcunha wrote:
) On Nov 15, 7:35?pm, Willem <wil...@toad.stack.nl> wrote:
)> dmjcunha wrote:
)>
)> ) Strange, I tried "-O1" and the problem was solved.
)> ) James Kuyper, thank you for the explanation.
)>
)> No, it wasn't solved. ?It was just hidden from view.
)
) Thank you all for the warnings. Anyway I think it is something a
) little bit beyond my current level of knowledge. I'll keep studying.
) Thanks!

The best way to learn is to tackle something that is just a little bit
beyond your current level of knowledge.  In any case, there have been
several pointers as to the actual problem in your code, which should
give a good starting point to solve it.  And I'm sure that if you get
stuck in that process, the people on thie NG are more than willing to
help you.


SaSW, Willem
-- 
Disclaimer: I am in no way responsible for any of the statements
            made in the above text. For all I know I might be
            drugged or something..
            No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT

[toc] | [prev] | [next] | [standalone]


#13479

FromNick Keighley <nick_keighley_nospam@hotmail.com>
Date2011-11-16 06:07 -0800
Message-ID<87e8c698-142e-4368-b995-1d8240db63e0@v5g2000yqn.googlegroups.com>
In reply to#13472
On Nov 16, 11:37 am, dmjcunha <dmjcu...@gmail.com> wrote:
> On Nov 15, 7:35 pm, Willem <wil...@toad.stack.nl> wrote:
> > dmjcunha wrote:

> > ) Strange, I tried "-O1" and the problem was solved.
> > ) James Kuyper, thank you for the explanation.
>
> > No, it wasn't solved.  It was just hidden from view.
>
> > SaSW, Willem
> > --
> > Disclaimer: I am in no way responsible for any of the statements
> >             made in the above text. For all I know I might be
> >             drugged or something..
> >             No I'm not paranoid. You all think I'm paranoid, don't you !
> > #EOT

it susual to trim out (snip) any signatures (usually any text after a
"--" of "-- " on a line of their own is a signature).

> Thank you all for the warnings. Anyway I think it is something a
> little bit beyond my current level of knowledge. I'll keep studying.
> Thanks!

well if you say an array has 3 items in it and then you try and access
the 4th element then Bad Things happen. If you're *lucky* your
computer crashes. If you're unlucky it just does something bizzare and
hard to debug.

[toc] | [prev] | [next] | [standalone]


#13494

FromKenneth Brody <kenbrody@spamcop.net>
Date2011-11-16 13:13 -0500
Message-ID<haGdnexi_tbTYF7TnZ2dnUVZ_sqdnZ2d@bestweb.net>
In reply to#13407
On 11/15/2011 2: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:
[...]
> 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;
[...]
>       for(rrj = rmid; rrj<  rredge; rrj++)
>            aux[rredge + rmid - rrj] = a[rrj + 1];
[...]

Although elsethread you said that changing to "-O1" "solved" the problem, 
all it did was hide it.

I would recommend going back to the compile flags in which the bug was 
visible.  Run that in the debugger again, and at the point that rmid is 
about to "magically" change, check the values of N, rredge, rmid, and rrj. 
You will probably see that redge+rmid-rj exceeds N-2, or is less than zero.



-- 
Kenneth Brody

[toc] | [prev] | [next] | [standalone]


#13500

FromKaz Kylheku <kaz@kylheku.com>
Date2011-11-16 19:12 +0000
Message-ID<20111116111038.130@kylheku.com>
In reply to#13494
On 2011-11-16, Kenneth Brody <kenbrody@spamcop.net> wrote:
> I would recommend going back to the compile flags in which the bug was 
> visible.  Run that in the debugger again, and at the point that rmid is 
> about to "magically" change, check the values of N, rredge, rmid, and rrj. 
> You will probably see that redge+rmid-rj exceeds N-2, or is less than zero.

With optimization, looking at variables is not reliable under the GNU toolchain
(gcc + gdb). GDB does not know, in general, where a variable lives at any
point in the code: is its current home in the backing memory location or
in a register?  You have to disassemble the code, or add some printf calls and
hope that it still reproduces the same way.

[toc] | [prev] | [next] | [standalone]


#13521

Fromdmjcunha <dmjcunha@gmail.com>
Date2011-11-16 13:14 -0800
Message-ID<fa8bdbd2-4d6a-45d3-978c-cd87fe14421f@n6g2000vbg.googlegroups.com>
In reply to#13500
I'm sorry to all of you. Sorry to have spent your time. The argument a
is a[N] and aux should also be aux[N]. I don't know what happened that
I typed aux[N-1].
I hope you experienced programmers forgive me for this typical
beginner mistake. Now everything is fine without "-O1" :)

[toc] | [prev] | [next] | [standalone]


#13527

FromSeebs <usenet-nospam@seebs.net>
Date2011-11-16 22:18 +0000
Message-ID<slrnjc8f1p.38n.usenet-nospam@guild.seebs.net>
In reply to#13521
On 2011-11-16, dmjcunha <dmjcunha@gmail.com> wrote:
> I'm sorry to all of you. Sorry to have spent your time. The argument a
> is a[N] and aux should also be aux[N]. I don't know what happened that
> I typed aux[N-1].

Ah-hah!

> I hope you experienced programmers forgive me for this typical
> beginner mistake. Now everything is fine without "-O1" :)

You say "typical beginner mistake".  I say "last-minute emergency panic
about a kernel release", because that same mistake was made in a hunk of
kernel network driver code.  (Actually, in that case, it was a[N] when
it should have been a[N+1], and I blame the general API design for getting
the sense of FOO_MAX wrong, but...)

Don't feel bad about "wasting" our time by asking an actual question about
C and then taking the time to follow up on it and learn what was happening.
That's pretty much why we hang out here...

-s
-- 
Copyright 2011, all wrongs reversed.  Peter Seebach / usenet-nospam@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web