Solved

Are there any issues with defining inside function body?

Posted on 2014-01-08
5
326 Views
Last Modified: 2014-01-08
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?
0
Comment
Question by:naseeam
5 Comments
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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.
0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 250 total points
Comment Utility
the only "issue" is that this will then be required to be put into the function code stack...
though not lots of data, still it could be optimized to be defined outside the function.
note: the compiler might do that anyhow, but I would do it in the code properly from the start. the more times this function is called, the worse the effect if all those constants have to be initiated so many times.
0
 

Author Comment

by:naseeam
Comment Utility
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 ?
0
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
Comment Utility
Anything that is declared inside a function goes on the stack. If you declare them as global variables however, they'll end up on the heap. Unless there is a reason to utilize local scope, I'd recommend globals for that.
0
 
LVL 32

Expert Comment

by:phoffric
Comment Utility
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.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now