Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Strange behaviour?

Reply
Thread Tools

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

Thanks in advance!
 
Reply With Quote
 
 
 
 
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!


 
Reply With Quote
 
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.
 
Reply With Quote
 
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"
 
Reply With Quote
 
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
 
Reply With Quote
 
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!
 
Reply With Quote
 
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
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
 
Reply With Quote
 
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.

 
Reply With Quote
 
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
AVG Email Scanner activating at strange times with strange IP addresses dennispublic@hotmail.com Computer Support 1 08-26-2006 04:27 AM
Strange Wireless Network problem Sarah Kingswell Wireless Networking 1 02-15-2005 06:24 PM
strange problem John Wireless Networking 4 02-14-2005 05:40 PM
Strange taskbar behaviour (notification area) Falcon Wireless Networking 0 08-17-2004 09:03 AM
Question About Strange 'C' Code Syntax ( Well strange to me anyway ) Harvey Twyman C Programming 8 10-25-2003 05:54 AM



Advertisments