Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Thread

C Code weird behaviour help?

C Code weird behaviour help?

2007-01-27 by luthjej

Hi all,


 I'm hoping someone may be able to offer some assistance to a weird
"C" behavior problem I'm having (ICC v7).

The target platform is a Mega16.

The problem I've simplified below - basically the code below does not
work, the two while loops (outside of the main "infinite" loop) are
supposed to alternate depending on the value assigned to "val"
(greater than or less than 650), however when using the variable
comparison - the loop never finishes (i.e. (otherval < val) is always
TRUE, regardless of the value applied to the "otherval" variable).

Confusingly (for me, at least :) - If I substitute the actual integer
in place of "val" in the while loop test statement - it all works fine.

Thanks in advance for any help ... this one has me stumped!

Cheers,

Jon


Code that doesn't work:

main ()
{ 
  unsigned int val;
  unsigned int otherval;

val = 650;

while (1)
 {
   while (otherval < val)
    {
     otherval++;
    }
   while (otherval >= val)
    { 
     Do_other_stuff();
    }
  }

}

Code that DOES work:

main ()
{ 
  unsigned int otherval;

while (1)
 {
   while (otherval < 650)
    {
     otherval++;
    }

   while (otherval >= 650)
    { 
     Do_other_stuff();
    }

  }

}

RE: [AVR-Chat] C Code weird behaviour help?

2007-01-27 by Cat C

maybe you should be more specific about "int"... like "uint_8t", "uint_16t" 
etc.
I may have gotten the actual names wrong, but U get the idea (I hope).

Good luck.

Cat

----Original Message Follows----
Show quoted textHide quoted text
From: "luthjej" <luthjej@yahoo.com.au>
Reply-To: AVR-Chat@yahoogroups.com
To: AVR-Chat@yahoogroups.com
Subject: [AVR-Chat] C Code weird behaviour help?
Date: Sat, 27 Jan 2007 06:24:58 -0000

Hi all,


  I'm hoping someone may be able to offer some assistance to a weird
"C" behavior problem I'm having (ICC v7).

The target platform is a Mega16.

The problem I've simplified below - basically the code below does not
work, the two while loops (outside of the main "infinite" loop) are
supposed to alternate depending on the value assigned to "val"
(greater than or less than 650), however when using the variable
comparison - the loop never finishes (i.e. (otherval < val) is always
TRUE, regardless of the value applied to the "otherval" variable).

Confusingly (for me, at least :) - If I substitute the actual integer
in place of "val" in the while loop test statement - it all works fine.

Thanks in advance for any help ... this one has me stumped!

Cheers,

Jon


Code that doesn't work:

main ()
{
   unsigned int val;
   unsigned int otherval;

val = 650;

while (1)
  {
    while (otherval < val)
     {
      otherval++;
     }
    while (otherval >= val)
     {
      Do_other_stuff();
     }
   }

}

Code that DOES work:

main ()
{
   unsigned int otherval;

while (1)
  {
    while (otherval < 650)
     {
      otherval++;
     }

    while (otherval >= 650)
     {
      Do_other_stuff();
     }

   }

}




Yahoo! Groups Links



_________________________________________________________________
Buy, Load, Play. The new Sympatico / MSN Music Store works seamlessly with 
Windows Media Player. Just Click PLAY. 
http://musicstore.sympatico.msn.ca/content/viewer.aspx?cid=SMS_Sept192006

Re: [AVR-Chat] C Code weird behaviour help?

2007-01-27 by Dave Hylands

Hi,

> Code that doesn't work:
>
> main ()
> {
>  unsigned int val;
>  unsigned int otherval;
>
> val = 650;
>
> while (1)
>  {
>   while (otherval < val)

otherval is currently uninitialized and will take on some random value
from the stack.

> Code that DOES work:
>
> main ()
> {
>  unsigned int otherval;
>
> while (1)
>  {
>   while (otherval < 650)

otherval is still uninitialized. The fact that it works is pure luck

-- 
Dave Hylands
Vancouver, BC, Canada
http://www.DaveHylands.com/

Re: C Code weird behaviour help?

2007-01-28 by luthjej

Thanks for the reply, Dave

Unfortunately the code snippet below is not the "real" code, but a 
quick throw-together to illustrate the problem I'm having.

In the real code, the variables are initialized with real data. 
Either statically (for testing) or dynamically from a pre-defined 
location in EEPROM.

ICC actually initializes the value to 0 automatically as part of the 
compile process anyhow ...

I'm still having no luck trying to solve this problem - might try and 
use a series of if-then-else handlers instead of the alternating 
while loops ... hmmm ...

Thanks again for the reply.


Cheers,


Jon



--- In AVR-Chat@yahoogroups.com, "Dave Hylands" <dhylands@...> wrote:
>
> Hi,
> 
> > Code that doesn't work:
> >
> > main ()
> > {
> >  unsigned int val;
> >  unsigned int otherval;
> >
> > val = 650;
> >
> > while (1)
> >  {
> >   while (otherval < val)
> 
> otherval is currently uninitialized and will take on some random 
value
Show quoted textHide quoted text
> from the stack.
> 
> > Code that DOES work:
> >
> > main ()
> > {
> >  unsigned int otherval;
> >
> > while (1)
> >  {
> >   while (otherval < 650)
> 
> otherval is still uninitialized. The fact that it works is pure luck
> 
> -- 
> Dave Hylands
> Vancouver, BC, Canada
> http://www.DaveHylands.com/
>

RE: [AVR-Chat] Re: C Code weird behaviour help?

2007-01-28 by larry barello

Which compiler are you using?  AVR-GCC compiles and runs your code
correctly.

I'll state this again just for the benefit of all those who assume they
can't use GCC because it is too difficult: Studio 4 with the GCC plug in
makes is brain dead simple:  Just create a project, cut the code from the
email and paste into the initial file, and press the compile button.  Start
debugging, and see that it works.

If you set the compile options for anything other than -oO (default, no
optimization) the compiler correctly identifies the problems and warns you.

GCC is the best and only free compiler!
Show quoted textHide quoted text
-----Original Message-----
From: AVR-Chat@yahoogroups.com [mailto:AVR-Chat@yahoogroups.com] On Behalf
Of luthjej
Sent: Sunday, January 28, 2007 5:00 AM
To: AVR-Chat@yahoogroups.com
Subject: [AVR-Chat] Re: C Code weird behaviour help?

Thanks for the reply, Dave

Unfortunately the code snippet below is not the "real" code, but a 
quick throw-together to illustrate the problem I'm having.

In the real code, the variables are initialized with real data. 
Either statically (for testing) or dynamically from a pre-defined 
location in EEPROM.

ICC actually initializes the value to 0 automatically as part of the 
compile process anyhow ...

I'm still having no luck trying to solve this problem - might try and 
use a series of if-then-else handlers instead of the alternating 
while loops ... hmmm ...

Thanks again for the reply.


Cheers,


Jon



--- In AVR-Chat@yahoogroups.com, "Dave Hylands" <dhylands@...> wrote:
>
> Hi,
> 
> > Code that doesn't work:
> >
> > main ()
> > {
> >  unsigned int val;
> >  unsigned int otherval;
> >
> > val = 650;
> >
> > while (1)
> >  {
> >   while (otherval < val)
> 
> otherval is currently uninitialized and will take on some random 
value
> from the stack.
> 
> > Code that DOES work:
> >
> > main ()
> > {
> >  unsigned int otherval;
> >
> > while (1)
> >  {
> >   while (otherval < 650)
> 
> otherval is still uninitialized. The fact that it works is pure luck
> 
> -- 
> Dave Hylands
> Vancouver, BC, Canada
> http://www.DaveHylands.com/
>




 
Yahoo! Groups Links

RE: [AVR-Chat] Re: C Code weird behaviour help?

2007-01-28 by John Samperi

At 02:18 AM 29/01/2007, you wrote:
>Studio 4 with the GCC plug in
>makes is brain dead simple:

I must agree with that even though I don't use C, no
make file to write up or other confusing stuff. The only
confusing bit for me is C itself... :) Perhaps the new
Studio 4.13 will make it even easier to use just like
the assembler.


Regards

John Samperi

********************************************************
Ampertronics Pty. Ltd.
11 Brokenwood Place Baulkham Hills, NSW 2153 AUSTRALIA
Tel. (02) 9674-6495       Fax (02) 9674-8745
Email: john@ampertronics.com.au
Website  http://www.ampertronics.com.au
*Electronic Design * Custom Products * Contract Assembly
********************************************************

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.