-
December 8th, 2003, 02:01 PM
#1
Quick constant question
I have a very quick question. If I declare a constant the standard way, with the const keyword, does it go into the data segment or the code segment of the executable?
-
December 8th, 2003, 03:22 PM
#2
Depends on the compiler as well as the context and some other qualifiers.
Code:
#include <math.h>
// Probably data
static const int n = 3;
const int m = 5;
int main(int argc, char* argv[])
{
// Probably a local register.
const double d = ::sin(0.1);
return 0;
}
Sincerely, Chris.
You're gonna go blind staring into that box all day.
-
December 8th, 2003, 04:10 PM
#3
-
December 8th, 2003, 04:46 PM
#4
There is a keyword "register" which will cause a variable to be
stored in a machine register if possible. Note: MS says the
32-bit compiler does not honor user requests for register
variables.
If you want to force the constant to be in the code segment, then
you can use template meteprogramming to achieve this.
refer to this papar and the section on compile time functions
http://osl.iu.edu/~tveldhui/papers/T.../meta-art.html
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
-
December 9th, 2003, 03:00 AM
#5
You know,
Both last nicht as well as this morning, as soon as souldog mentioned that interesting comment about template programming, my respect for this topic was reawakened.
In fact, it is really a very rich topic. The samples below show some of the interesting cases which can arise. Take a look.
Sincerely, Chris.
Code:
#include <math.h>
// Probably data
static const int n = 3;
const int m = 5;
// I do not know?
static const double dc = ::sqrt(0.3);
// Template metaprogram
template <int N> struct factorial
{
enum
{
value = factorial<N-1>::value * N
};
};
template <> struct factorial<0>
{
enum
{
value = 1
};
};
// Use of the template metaprogram.
static volatile int four_factorial = static_cast<int>(factorial<4>::value);
int main(int argc, char* argv[])
{
// Probably a local register.
const double d = ::sin(0.1);
// Maybe a register, maybe not.
// Maybe considered bad style?
register const int rn = 12;
volatile const int vc = 34;
return 0;
}
You're gonna go blind staring into that box all day.
-
December 9th, 2003, 05:41 AM
#6
The only difference between the const with static and the one without is a scoping one, and that is something the compiler can sort out. For your integral ones it may well optimise and treat them like enums.
Even for one like
Code:
const long double pi = 3.1415926535897932384626433832795;
the compiler may well substitute a literal everywhere pi is used.
We already know that using string literals will lead to undefined behaviour if you try modifying them.
For scoping purposes it is probably best to use namespaces to avoid naming clashes (better than using static with your const).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|