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

Problem with #locate and sizeof()

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



Joined: 22 Aug 2008
Posts: 21

View user's profile Send private message

Problem with #locate and sizeof()
PostPosted: Wed Feb 18, 2015 10:51 am     Reply with quote

Hi,

I'm having difficulties with the use of #locate and sizeof() to get the dimension of a structure
I have a receive buffer that share the same space as 2 structures, one for the header part and the 2nd for the payload. The 1st structure can be located with
Code:
#locate Rx_Hdr= RxBuff

but I can't use
Code:
 #locate Payload= RxBuff+ sizeof(Rx_Hdr)

to place the payload after the header structure using sizeof()

error messages say: Expression must be a constant or simple variable
but it works with
Code:
#locate Payload= RxBuff+ 3

Why can't I use sizeof() with preprocessor #locate?
What am I missing?
Or is there a better way to do it?

Thanks

>>>>>>> SAMPLE code
Code:
#include "18F26K80.h"
...

// structure definition for the header
typedef struct _RESP_HDR{
    unsigned int SUCC_FAIL;
    unsigned int LOGIC_ID;   
   unsigned int DATA_LEN;
} RESP_HDR;

// structure definition for the payload
typedef struct _PAYL_RX{
   int32 VAR1;
   int32 VAR2;
   int32 VAR3;
}PAYL_DATA;

char RxBuff[AHRS_RXBUFFLEN];               //!<
/// receive header(Rx_Hdr) share same address space as the receive buffer RxBuff[]
RESP_HDR Rx_Hdr;                  // ram structure holding the message header
#locate Rx_Hdr= RxBuff
   
/// receive Payload(Payload)  share same address space as the receive buffer RxBuff[]
PAYL_DATA Payload;                  // ram structure holding the payload information
#locate Payload= RxBuff+ sizeof(Rx_Hdr) // LINE A: Problem with this line


if LINE A: #locate Payload= RxBuff+ sizeof(Rx_Hdr)
results:
*** Error 51 "file.h" Line 338(31,37): A numeric expression must appear here
*** Error 48 "file.h" Line 338(38,44): Expecting a (

if LINE A: #locate Payload= (RxBuff+ (sizeof(Rx_Hdr)))
results:
*** Error 91 "file.h" Line 338(18,19): Expression must be a constant or simple variable
*** Error 43 "file.h" Line 338(30,31): Expecting a declaration
*** Error 43 "file.h" Line 338(32,33): Expecting a declaration
*** Error 43 "file.h" Line 338(33,39): Expecting a declaration
*** Error 43 "file.h" Line 338(39,40): Expecting a declaration
*** Error 48 "file.h" Line 338(40,46): Expecting a (
*** Error 43 "file.h" Line 338(47,48): Expecting a declaration
*** Error 43 "file.h" Line 338(48,49): Expecting a declaration

if line A:#locate Payload= AHRS_RxBuff+ sizeof(RESP_HDR)
results
*** Error 51 "file.h" Line 338(31,37): A numeric expression must appear here

if line A:#locate Payload= AHRS_RxBuff+ sizeof(_RESP_HDR)
results
*** Error 51 "file.h" Line 340(31,37): A numeric expression must appear here
*** Error 48 "file.h" Line 340(38,47): Expecting a (
Ttelmah



Joined: 11 Mar 2010
Posts: 19436

View user's profile Send private message

PostPosted: Wed Feb 18, 2015 11:47 am     Reply with quote

The better way, is a union. This is what this is for....
JacquesB



Joined: 22 Aug 2008
Posts: 21

View user's profile Send private message

PostPosted: Wed Feb 18, 2015 12:31 pm     Reply with quote

Yes but with a union all members start at the same memory location.
I want the payload structure to be at the end of the Rx_Hdr structure.

right now if I use #locate Payload= RxBuff+ 3, the program compile and I can access the different elements:
ex:
Code:

RxBuff[ RxBuffidx]= c;      // fill the rx buffer
x= Rx_Hdr.LOGIC_ID;     // get the logic ID of the header
y= Payload.VAR1;           //  get the payload variable VAR1

my main concern is about the difference between:
Code:

#locate Payload= RxBuff+ 3

and
Code:

#locate Payload= RxBuff+ sizeof(Rx_Hdr)

I may redefine the header structure and I want the payload to be located properly
Unless there's a way with unions that I don't see...
jeremiah



Joined: 20 Jul 2010
Posts: 1334

View user's profile Send private message

PostPosted: Thu Feb 19, 2015 11:18 am     Reply with quote

JacquesB wrote:

my main concern is about the difference between:
Code:

#locate Payload= RxBuff+ 3

and
Code:

#locate Payload= RxBuff+ sizeof(Rx_Hdr)



The main difference is that sizeof() is not a preprocessor environment component, so the preprocessor cannot parse what the value is. You can use it as a replacement in a macro since the preprocessor doesn't have to know the value of sizeof for replacement. However, if you try to force the preprocessor to evaluate the value of sizeof()...say in an #if or #locate...then it can balk. Remember that the Preprocessor has no concept of types (int, char, Rx_Hdr) nor how structs and unions are packed, so it cannot figure out the size of the type you put in. The preprocessor is very limited.

That's not to say that theoretically it cannot be parsed at the preprocessor (I am sure there are some compiler vendors who have), but it would involve bringing a lot of the compiler environment into the preprocessor, which is overkill.
Ttelmah



Joined: 11 Mar 2010
Posts: 19436

View user's profile Send private message

PostPosted: Thu Feb 19, 2015 12:02 pm     Reply with quote

and, as a comment, on locating things later in the union, just use a dummy.

The key point is that you are offsetting by the size of a variable. If you create a union, using a structure which contains a declared dummy variable of the same type, and then the area you want to be shared, the offset is automatic, and will adjust for the variable size....

The compiler doesn't actually have a 'preprocessor' as such.
What it does is an initial pass in which it does things as if they were pragmas. These include #build, #org, #locate. After this is allocates the variable sizes and locations. Then it starts on the actual code. Just the way that CCS works
JacquesB



Joined: 22 Aug 2008
Posts: 21

View user's profile Send private message

PostPosted: Thu Feb 19, 2015 12:42 pm     Reply with quote

Thanks Jeremiah for your good explanations. I got it.

Thanks also to Ttelmah, I'll take your suggestion
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