Solved

Are there any issues with defining inside function body?

Posted on 2014-01-08
5
331 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
ID: 39766323
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
ID: 39766327
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
ID: 39766336
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
ID: 39766352
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
ID: 39766863
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

911 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

24 Experts available now in Live!

Get 1:1 Help Now