Solved

Are there any issues with defining inside function body?

Posted on 2014-01-08
5
339 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 143

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
 
LVL 1

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ Language error 28 245
Specific format 21 207
Problem to event 3 102
Adding items to a C# list incrementally 5 30
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

856 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