View unanswered posts | View active topics It is currently Fri Aug 01, 2014 2:59 am






Reply to topic  [ 5 posts ] 
two bytes of memory getting trashed 
Author Message
Rookie

Joined: Thu Dec 29, 2011 8:34 pm
Posts: 14
Post two bytes of memory getting trashed
I wrote some code to allow the user to select parameters using the NXT buttons. It uses an array of structs (TParam) inside one struct (TParams).

All was good except the first two bytes of the first string in the TParam struct was getting trashed ("Blue" became "|3ue"). I could shift the problem to a different variable by changing the order of the variables in the struct typedef, e.g. putting string name first instead of string values[].

I resorted to inserting a sacrificial variable as the first variable in TParam (short dummy). But I am not comfortable with this. Is it me or is it RobotC?

Here's the pair of typedefs that are in menu.c. Enclosed are menu.c and another file that can be used to demo the problem. Comment out "short dummy" to see the problem.

Code:
#define MAX_PARAMS (5)
#define MAX_PARAM_VALUES (8)

typedef struct
{
  short dummy;  // the first 2 bytes of the first param were getting trashed.  Sacrificial trashable variable.
  string values[MAX_PARAM_VALUES];
  int numValues;
  int idx;  // index into values[]
  string name;
} TParam;

typedef struct
{
  TParam params[MAX_PARAMS];
  int numParams;

} TParams;


Attachments:
File comment: task main() to demonstrate problem.
Autonomous_1.c [1.87 KiB]
Downloaded 274 times
File comment: menu.c gets #included in file with main().
menu.c [5.27 KiB]
Downloaded 275 times
Thu Jan 05, 2012 8:35 am
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: two bytes of memory getting trashed
What version of RobotC are you using? A bug that could explain what you saw was fixed in RobotC version 3.04.
Actually, looking at your code, you are using pointers all over. My understanding is that RobotC does not support pointers yet. I am surprise the compiler did not complain. I know that the RobotC folks have been working hard on supporting pointers in some future release but I am not sure if some partial support has made it out. Unless the RobotC folks can confirm this, I would avoid using pointers. You can pass the structure by reference.
We have implemented a similar menu module this way.
http://proj.titanrobotics.net/hg/Ftc/20 ... lib/menu.h


Thu Jan 05, 2012 3:45 pm
Profile
Rookie

Joined: Thu Dec 29, 2011 8:34 pm
Posts: 14
Post Re: two bytes of memory getting trashed
I'm using 3.04.

I think the only pointer I'm using is by passing the address of the TParams object into functions, e.g.
Code:
TParams parameters;
...
  addParam     ( "Color", &parameters );
  [etc]
And isn't "passing by reference" another way of saying "passing a pointer"?


Thu Jan 05, 2012 11:28 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: two bytes of memory getting trashed
gnormhurst wrote:
I'm using 3.04.

I think the only pointer I'm using is by passing the address of the TParams object into functions, e.g.
Code:
TParams parameters;
...
  addParam     ( "Color", &parameters );
  [etc]
And isn't "passing by reference" another way of saying "passing a pointer"?

There is a slight difference between passing by reference and pointers. It's mainly syntax. With pointers, you can do pointer arithmetics which will result in run away pointers that will cause fault in your code. With pass by reference, the syntax does not allow you to do pointer arithmetics. It's suppose to protect inexperience programmers from shooting their foot. If you search the forums, you will find a lot of discussions on RobotC did not support pointers but the desire of the user community on having pointer support. And the RobotC folks mentioning up coming pointer support in 3.x. That's why I am not sure if there is pointer support in 3.04. I did not see the RobotC folks announcing it. So I assume still no pointer support. But the fact that you can compile with pointer syntax gave me an impression that it may have partial support which usually means not everything is working and bugs free.


Fri Jan 06, 2012 2:19 am
Profile
Rookie

Joined: Thu Dec 29, 2011 8:34 pm
Posts: 14
Post Re: two bytes of memory getting trashed
I rewrote the code to avoid pointers, and the problem went away. Thanks!


Sat Jan 07, 2012 11:22 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 5 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.