CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug Reports on this forum. Send them to support@ccsinfo.com

Math/Pointer Issue (a+b != a+b)

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
kda406



Joined: 17 Sep 2003
Posts: 69
Location: Atlanta, GA, USA

View user's profile Send private message

Math/Pointer Issue (a+b != a+b)
PostPosted: Tue Jun 19, 2007 2:41 pm     Reply with quote

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: 20759

View user's profile Send private message

PostPosted: Tue Jun 19, 2007 2:44 pm     Reply with quote

Post all variable declarations.
kda406



Joined: 17 Sep 2003
Posts: 69
Location: Atlanta, GA, USA

View user's profile Send private message

PostPosted: Tue Jun 19, 2007 2:53 pm     Reply with quote

As I already said in the post, they are int16s and the structure is 74 bytes long.

In the processing routine:
Code:
int16 i,m;

Globally defined:
Code:
typedef struct {
unsigned int32    lCode;
char   cPacket[68];
unsigned int16   mFilter;
} settingstruct;

settingstruct    gsSetting;


Thanks,
Kyle
Ttelmah
Guest







PostPosted: Tue Jun 19, 2007 2:57 pm     Reply with quote

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: 69
Location: Atlanta, GA, USA

View user's profile Send private message

Solved
PostPosted: Wed Jun 20, 2007 7:37 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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