Go to Post I'm glad to be on the same team as smart people. - Andy Baker [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
 
 
Thread Tools Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #5   Spotlight this post!  
Unread 10-02-2006, 11:25
heydowns's Avatar
heydowns heydowns is offline
Registered User
AKA: Jeff Downs
FRC #1511 (Rolling Thunder)
Team Role: Mentor
 
Join Date: Jan 2006
Rookie Year: 2005
Location: Ra-Cha-Cha
Posts: 142
heydowns has a reputation beyond reputeheydowns has a reputation beyond reputeheydowns has a reputation beyond reputeheydowns has a reputation beyond reputeheydowns has a reputation beyond reputeheydowns has a reputation beyond reputeheydowns has a reputation beyond reputeheydowns has a reputation beyond reputeheydowns has a reputation beyond reputeheydowns has a reputation beyond reputeheydowns has a reputation beyond repute
Re: Modified default code gives "code violation"

You probably get code errors because of your printf statements:

Code:
unsigned char joystick_input = 127;
 // ...
printf("Slow_Drive for pwm01 = %d",joystick_input); 
// ...
printf("Slow_Drive for pwm02 = %d",joystick_input);
You are asking printf to print an integer type (by using %d). But you are only passing it an "unsigned char" data type. In brief, this is in error -- details are below.
To fix, try passing it an unsigned integer explicitly by doing the following:

printf("Slow_Drive for pwm01 = %u", (unsigned int)joystick_input);

The %u says "print as unsigned". The cast to unsigned int will ensure that the arguments are decoded correctly within the printf function.

For those interested, the long explanation:
printf takes a variable number and type of arguments. This is contrary to most functions, which have a very specified number and type of arguments (void f(int x, int y) takes two ints, for example).

Printf relies on you supplying the correct format characters in the initial string argument to be able to decode the subsequent arguments. This is so that it can grab the right number of bytes off the block of memory representing the passed arguments. If you put a format character for int (%d), it will grab sizeof(int) bytes. If you put a format character for char (%c), it will grab sizeof(char) bytes. And so on.

The original poster's code specified %d as the format string, but passed a char. This means the printf code went to grab 2 bytes (sizeof int), but, in reality, the function was only passed 1 byte (sizeof unsigned char). This resulted in printf accessing memory outside the space allocated to the function's arguments.

In this case, it resulted in red light of doom..... But that isn't necessarily always the case. The results here really are undefined and unpredictable. There is a chance it would just run, probably printing out incorrect results for the passed unsigned char.

Another common mistake with printf is when printing long variables. You need to use %ld (for long) or %lu (for unsigned long). If you accidentally use %d for long, you'll likely just get wrong output, but not red light of death.
 


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Out of the Box Camera Code russell Programming 9 21-10-2009 05:28
default camera code edit Windward Programming 8 25-01-2006 18:33
Best Way To Insert Autonomous Code Into Default then load help.? :-)? gemccnp Programming 2 05-02-2005 18:58
Team THRUST - Kevin's Code and Camera Code Combine Chris_Elston Programming 3 31-01-2005 22:28
heres the code. y this not working omega Programming 16 31-03-2004 15:18


All times are GMT -5. The time now is 19:08.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi