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

macro problem

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







macro problem
PostPosted: Fri Mar 14, 2003 3:10 am     Reply with quote

I have the following macro in code. It works fine when I use constants, but will not work with variables. Maybe this is how it suppose to be, but can anyone offer a solution to this problem without going over an int value (1 byte).

Here is the macro
#define SEC(x) x*200 // time in seconds

This works
static long timeout;
timeout=SEC(20);

This will not work
static long timeout;
ULTO[4] = {20,60,90,120); // user timeout levels
timeout=SEC(ULTO[0]);

Thanks in advance for any help

Ed Arnold
___________________________
This message was ported from CCS's old forum
Original Post ID: 12661
R.J.Hamlett
Guest







Re: macro problem
PostPosted: Fri Mar 14, 2003 3:44 am     Reply with quote

:=I have the following macro in code. It works fine when I use constants, but will not work with variables. Maybe this is how it suppose to be, but can anyone offer a solution to this problem without going over an int value (1 byte).
:=
:=Here is the macro
:=#define SEC(x) x*200 // time in seconds
:=
:=This works
:=static long timeout;
:=timeout=SEC(20);
:=
:=This will not work
:=static long timeout;
:=ULTO[4] = {20,60,90,120); // user timeout levels
:=timeout=SEC(ULTO[0]);
:=
:=Thanks in advance for any help
As a general comment (may not help in this case, but it is worth trying...), bracket macros!...
This doesn't look like it should be the problem in this case, but is illustrated by this example:

#define test(x) x+100

a=test(x)*10;

As written, instead of expanding as 'x+100', and then being multiplied by 10, the expansion results in x+ (100*10).
This is a 'standard' behaviour in 'C', and hence it is allways advised that you bracket the macro expansions to control this problem. Hence:

#define test(x) (x*100)

avoids this problem.

Similar problems can exist with 'simple' expansions (just things like numbers), where the behaviour is not 'intuitive' at times. Again bracketting, ensures that the expansion does behave as expected.

Your problem however is probably being caused by the default mathematical expansion. If you code:

timeout=100;

Then the value '100', gets assigned to timeout. However if you code:

timeout=SEC(ULTO[0]);

the code will take the value 'ULTO[0]', and multiply it by an _integer_ value of 200. Since both values are normal (8bit) integers, this will result in a massive overflow, and the result will be effectively meaningless. Hence your code should read:

#define SEC(x) (x*200l)

Which then forces expansion to a 16bit multiply, and should give the right answer.
All your values _are_ going to go over the 'int value', since the results will range from 4000 to 24000. If this is the size of value you need, there is no avoiding this. The storage used for each value will still be an int8, by using the macro expansion shown, but there will be a few bytes extra code to handle the 16bit multiplication.

Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 12664
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