|
|
View previous topic :: View next topic |
Author |
Message |
kda406
Joined: 17 Sep 2003 Posts: 97 Location: Atlanta, GA, USA
|
Math/Pointer Issue (a+b != a+b) |
Posted: Tue Jun 19, 2007 2:41 pm |
|
|
Can someone tell me why one method works and the other doesn't? This is on an 18F8722 compiled on 4.038. Here is what I want to do, but it hoses memory:
Code: | memcpy(&gsSetting+i,payload+3,c); |
Rewritten, this works, but requires extra (unneeded) steps and RAM:
Code: | m = &gsSetting;
memcpy(m+i,payload+3,c); |
I set up a test program to print out the values. gsSetting is a global structure that is 74 bytes in length. Other variables are int16. Here is the code that illustrates the problem:
Code: | i=43;
m = &gsSetting;
fprintf(TERM,"&gsSetting=%li m=%li m+i=%li &gsSetting+i=%li &i=%li\n\r",&gsSetting,m,m+i,&gsSetting+i,&i); |
Which yields the following startling results:
Quote: | &gsSetting=393 m=393 m+i=436 &gsSetting+i=503 &i=1426 |
I can't figure out any combination of addresses or values that equals 503. This seems to be a math error. Am I doing something wrong here, or is this a shortcoming of the CCS compiler?
Thanks,
Kyle |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jun 19, 2007 2:44 pm |
|
|
Post all variable declarations. |
|
|
kda406
Joined: 17 Sep 2003 Posts: 97 Location: Atlanta, GA, USA
|
|
Posted: Tue Jun 19, 2007 2:53 pm |
|
|
As I already said in the post, they are int16s and the structure is 74 bytes long.
In the processing routine:
Globally defined:
Code: | typedef struct {
unsigned int32 lCode;
char cPacket[68];
unsigned int16 mFilter;
} settingstruct;
settingstruct gsSetting; |
Thanks,
Kyle |
|
|
Ttelmah Guest
|
|
Posted: Tue Jun 19, 2007 2:57 pm |
|
|
The problem is that:
&gsSetting+x
Will _not_ return the address of the xth byte in gsSetting, but the address of the xth 'gsSetting' structure _beyond_ the one you have.
This is pointer arithmetic in C. A pointer pointing to a memory structure of a particular type, if incremented, moves forward by the size of the type...
The value you get then comes about, because the size is so large, that the maths wraps. The result is slightly 'odd' (should be up at about 3500, but is the problem.
You need to use:
(int8 *)(&gsSetting)+i
This takes the address of 'gsSetting', and tells the compiler this is the address of an 8bit value, then increments this by i, which increments it in bytes, instead of in lumps the size of the structure.
Best Wishes |
|
|
kda406
Joined: 17 Sep 2003 Posts: 97 Location: Atlanta, GA, USA
|
Solved |
Posted: Wed Jun 20, 2007 7:37 am |
|
|
Ttelmah,
Thanks for the reply. Indeed that fixes the problem. I now understand the incrementing of the structure mistake I made. To tell the truth, I think I knew that years ago, but so infrequently work on the address level of bytes within a structure, that I just forgot how C implements it. I appreciate your insight into the solution.
Many Thanks,
Kyle |
|
|
|
|
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 vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|