?
Solved

Are there any issues with defining inside function body?

Posted on 2014-01-08
5
Medium Priority
?
369 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 1000 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 1000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Suggested Courses

765 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