# Strange behaviour?

Discussion in 'C Programming' started by Seebs, Nov 15, 2011.

1. ### SeebsGuest

On 2011-11-15, dmjcunha <> 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.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.

Seebs, Nov 15, 2011

2. ### dmjcunhaGuest

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

dmjcunha, Nov 15, 2011

3. ### James KuyperGuest

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

James Kuyper, Nov 15, 2011
4. ### dmjcunhaGuest

On Nov 15, 4:26 pm, Seebs <> wrote:
> On 2011-11-15, dmjcunha <> 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 / ://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.

dmjcunha, Nov 15, 2011
5. ### Keith ThompsonGuest

dmjcunha <> writes:
> On Nov 15, 4:26Â pm, Seebs <> wrote:
>> On 2011-11-15, dmjcunha <> 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
behavior is undefined.

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

Keith Thompson, Nov 15, 2011
6. ### WillemGuest

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

Willem, Nov 15, 2011
7. ### dmjcunhaGuest

On Nov 15, 7:35 pm, Willem <> 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!

dmjcunha, Nov 16, 2011
8. ### WillemGuest

dmjcunha wrote:
) On Nov 15, 7:35?pm, Willem <> 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

Willem, Nov 16, 2011
9. ### Nick KeighleyGuest

On Nov 16, 11:37 am, dmjcunha <> wrote:
> On Nov 15, 7:35 pm, Willem <> 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.

Nick Keighley, Nov 16, 2011
10. ### Kaz KylhekuGuest

On 2011-11-16, Kenneth Brody <> 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.

Kaz Kylheku, Nov 16, 2011
11. ### dmjcunhaGuest

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"

dmjcunha, Nov 16, 2011
12. ### SeebsGuest

On 2011-11-16, dmjcunha <> 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...)

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 /
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.

Seebs, Nov 16, 2011