473,508 Members | 2,007 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Static Variable Must Be Constant?

I want to initialize a static variable to a "random" value like:

static void *
get_key(struct dnsreq *req)
{
static uint16_t next_txnid = (uint32_t)req & 0xFFFF;

But gcc gives me an error:

src/dns.c: In function `get_key':
src/dns.c:173: initializer element is not constant

So a static initializer must be a constant?

Must I do:

static uint16_t next_txnid = 0;

if (!next_txnid) {
(uint32_t)req & 0xFFFF;
}

?

Thanks,
Mike
Nov 14 '05 #1
6 3873
Michael B Allen wrote:
I want to initialize a static variable to a "random" value like:

static void *
get_key(struct dnsreq *req)
{
static uint16_t next_txnid = (uint32_t)req & 0xFFFF;


You can't INITIALIZE a static variable with an expression that isn't a
constant expression. But you are free to ASSIGN to a static variable
with a variable or constant expression.

do this:

static uint16_t next_txnid; /* compiler will initialize to zero */
next_txnid = (uint32_t)req & 0xFFFF; /* you assign something to it */
INITALIZATIONS are for STARTING VALUES. Since static variables START
before a program begins, the initial values for static variables must
not depend on variables (like req) whose values can't be determined
before program start. Thus, your compiler needs to know what your
static variable's value will be before program start, which obviously
can't be a variable expression. In your case, does it matter what the
value of next_txnid is at program start? Most likely no.

Nov 14 '05 #2
Michael B Allen wrote:
I want to initialize a static variable to a "random" value like:

static void *
get_key(struct dnsreq *req)
{
static uint16_t next_txnid = (uint32_t)req & 0xFFFF;

But gcc gives me an error:

src/dns.c: In function `get_key':
src/dns.c:173: initializer element is not constant

So a static initializer must be a constant?


Yes.

But the question is, since you overwrite the value immediately
on each function call entry, why do you need the variable to
have static duration in the first place?

If you're just trying to save stack allocation overhead for
a specific implementation, then just do...

static uint16_t next_txnid;
next_txnid = req & 0xFFFFu;

--
Peter

Nov 14 '05 #3
Michael B Allen wrote:

So a static initializer must be a constant?

Of course, initializers are computed during compile time
Must I do:

static uint16_t next_txnid = 0;

if (!next_txnid) {
(uint32_t)req & 0xFFFF;
}


Just separate declaration and assignment:

static uint16_t next_txnid;
next_txnid = (uint32_t)req & 0xFFFF;

Nov 14 '05 #4
On Tue, 08 Feb 2005 17:40:59 -0500, Kenneth Bull wrote:
INITALIZATIONS are for STARTING VALUES. Since static variables START
before a program begins, the initial values for static variables must
not depend on variables


Good explaination. Thanks.

Mike
Nov 14 '05 #5
On Tue, 08 Feb 2005 17:22:58 -0500, Michael B Allen wrote:
I want to initialize a static variable to a "random" value like:

static void *
get_key(struct dnsreq *req)
{
static uint16_t next_txnid = (uint32_t)req & 0xFFFF;

But gcc gives me an error:

src/dns.c: In function `get_key':
src/dns.c:173: initializer element is not constant

So a static initializer must be a constant?
Yes, static variables are initialised once at program startup. So in your
example code next_txnid would *not* be initialised when the function is
called, only at program startup when the variable req doesn't exist.
Must I do:

static uint16_t next_txnid = 0;

if (!next_txnid) {
(uint32_t)req & 0xFFFF;
I assume you mean

next_txnid = (uint32_t)req & 0xFFFF;
}
}


So it appears that you want the one-off initialisation behaviour but at
the first time get_key() is called. If so then, yes, this is a reasonable
approach assuming of course that (uint32_t)req & 0xFFFF can't be zero. A
more exact approach would use a separate flag variable indicating whether
next_txnid had been initialised or not.

Lawrence
Nov 14 '05 #6
Michael B Allen wrote:
...
So a static initializer must be a constant?
Yes.
Must I do:

static uint16_t next_txnid = 0;

if (!next_txnid) {
(uint32_t)req & 0xFFFF;
Did you mean
next_txnid = (uint32_t)req & 0xFFFF;
}

?
...


Yes, that's one way to do it, provided '0' is not one of the possible
results of '(uint32_t)req & 0xFFFF'. If it is, you might end up with
"initializations" that don't register a such, i.e. the variable will be
"re-initialized" again every time you go through this code.

Otherwise, if there's no definitive reserved "impossible" value among
the results of '(uint32_t)req & 0xFFFF' expression, you'll have no other
choice but to create another variable just to serve as a boolean
"already initialized" flag.

--
Best regards,
Andrey Tarasevich
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
28173
by: Marcin Vorbrodt | last post by:
So I have a class Math that looks like this: Math { public: static Real PI(void); }; Real Math::PI(void) { return 4.0 * atan(1.0); }
4
5406
by: cppsks | last post by:
"Defining static const variables inside the class is not universally supported yet, so for now I guess you'll have to move the definition out of the body of the class. No, static const inside...
9
3223
by: cppsks | last post by:
Taking the address of a static const resulted in a unresolved symbol. Why is that? Is the address assigned at load time? Thanks.
2
2617
by: Rafe Culpin | last post by:
Does anyone please know of a way to access static methods of a class, when the name of that class is held in a variable? I have several classes (PHP5) which all have identically named methods and...
10
4175
by: n.torrey.pines | last post by:
Are global variables (and const's) guaranteed to be initialized before static class members (and methods) ? const int x = 19907; int get_x() { return x; } // another compilation unit: ...
3
5830
by: Steve Folly | last post by:
Hi, I had a problem in my code recently which turned out to be the 'the "static initialization order fiasco"' problem (<http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12>) The FAQ...
9
8863
by: Jess | last post by:
Hello, I was told that if I declare a static class constant like this: class A{ static const int x = 10; }; then the above statement is a declaration rather than a definition. As I've...
15
7839
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
8
5689
by: aarklon | last post by:
Hi all, see:- http://linuxgazette.net/issue51/pramode.html
0
7226
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7125
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7328
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7049
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7499
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5631
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5055
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3199
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1561
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.