Link to home
Start Free TrialLog in
Avatar of naseeam
naseeamFlag for United States of America

asked on

Are there any issues with defining inside function body?

In a typical embedded application, assume following constants are defined inside a function body.

const unsigned short M1500ProductID = 23;
const unsigned short M1500dmProductID = 67;
const unsigned short M1200ProductID = 27;
const unsigned short M1200dmProductID = 66;
const unsigned short M750ProductID = 26;
const unsigned short M750dmProductID = 65;

Open in new window



This function is called every loop.  Can someone see any issues with defining constants inside function body?
Avatar of jkr
jkr
Flag of Germany image

The only issue one could argue about is stack space, even though these few bytes should not matter on a desktop system (they might on small embedded systems, though). Actually, I'd rather use a #define for that like

#define M1500ProductID  23
#define M1500dmProductID  67
#define M1200ProductID  27
#define M1200dmProductID  66
#define M750ProductID  26
#define M750dmProductID  65

Open in new window


since I'd rather not want to waste any storage space for variables that never change at all.
SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of naseeam

ASKER

I wasn't using #defind because then I don't know whay data type they are.

If constants are defined inside a function, do you think they go in stack space or in Read Only memory space or both ?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of phoffric
phoffric

Since they are const, there is no point in assigning them every time you enter the function; so you should make them static const so they get initialized only once. And BTW, as static, they would not be on the stack.