Strange behaviour?

S

Seebs

$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
 
D

dmjcunha

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!
 
J

James Kuyper

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!
 
D

dmjcunha

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

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

Keith Thompson

dmjcunha said:
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.
 
W

Willem

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
 
D

dmjcunha

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!
 
W

Willem

dmjcunha 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
 
N

Nick Keighley

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

Kaz Kylheku

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

dmjcunha

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" :)
 
S

Seebs

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top