Solved

Are there any issues with defining inside function body?

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
word0 challenge 3 112
Bartender Integration Builder 3 73
Android development question 2 81
troubleshoot a python script 8 77
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…
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

733 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