Velocity Reviews > Strange behaviour?

# Strange behaviour?

Seebs
Guest
Posts: n/a

 11-15-2011
On 2011-11-15, dmjcunha <(E-Mail Removed)> 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 / http://www.velocityreviews.com/forums/(E-Mail Removed)
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.

dmjcunha
Guest
Posts: n/a

 11-15-2011
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++) {
...

James Kuyper
Guest
Posts: n/a

 11-15-2011
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!

dmjcunha
Guest
Posts: n/a

 11-15-2011
On Nov 15, 4:26*pm, Seebs <(E-Mail Removed)> wrote:
> On 2011-11-15, dmjcunha <(E-Mail Removed)> 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 / (E-Mail Removed)://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.

Keith Thompson
Guest
Posts: n/a

 11-15-2011
dmjcunha <(E-Mail Removed)> writes:
> On Nov 15, 4:26Â*pm, Seebs <(E-Mail Removed)> wrote:
>> On 2011-11-15, dmjcunha <(E-Mail Removed)> 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) (E-Mail Removed) <http://www.ghoti.net/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Willem
Guest
Posts: n/a

 11-15-2011
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

dmjcunha
Guest
Posts: n/a

 11-16-2011
On Nov 15, 7:35*pm, Willem <(E-Mail Removed)> 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!

Willem
Guest
Posts: n/a

 11-16-2011
dmjcunha wrote:
) On Nov 15, 7:35?pm, Willem <(E-Mail Removed)> 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

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

Nick Keighley
Guest
Posts: n/a

 11-16-2011
On Nov 16, 11:37*am, dmjcunha <(E-Mail Removed)> wrote:
> On Nov 15, 7:35*pm, Willem <(E-Mail Removed)> 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.

Kaz Kylheku
Guest
Posts: n/a

 11-16-2011
On 2011-11-16, Kenneth Brody <(E-Mail Removed)> 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.