Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Using static to init vars?

Reply
Thread Tools

Using static to init vars?

 
 
Ronald Bruck
Guest
Posts: n/a
 
      06-12-2006
I have several routines which are used millions of times in my
programs, using the Gnu multi-precision software's floating-point reals
(see <http://www.swox.se/gmp>). These are of type mpf_t, and I must
explicitly initialize (and later free) all temporary variables of this
type.

So I hit on the idea of something like this:

static int firstcall = 1;
mpf_t temp;

if (firstcall) {
firstcall = 0;
mpf_init(temp);
}
else blahblah

First time through, firstcall is 1, so temp gets initialized. Second
(and later) times through, temp is already initialized (with what
values I don't care, because i'll reset them) and doesn't get
REinitialized (which would be a fatal error).

It passes -Wall, and it seems to work. Is it legal? Is there anything
I need to look out for?

Initializing, and then freeing, an mpf_t millions of times is not
something I want to do. Especially since it may be thousands of bytes
long.

I should add, I don't care that I never free temp. The program exits
before this would become a problem.

--Ron Bruck

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      06-12-2006
In article <120620061031319493%>,
Ronald Bruck <> wrote:

>So I hit on the idea of something like this:


> static int firstcall = 1;
> mpf_t temp;


> if (firstcall) {
> firstcall = 0;
> mpf_init(temp);
> }
> else blahblah


>First time through, firstcall is 1, so temp gets initialized. Second
>(and later) times through, temp is already initialized (with what
>values I don't care, because i'll reset them) and doesn't get
>REinitialized (which would be a fatal error).


>It passes -Wall, and it seems to work. Is it legal? Is there anything
>I need to look out for?


Yes, that is legal.

You could also save an assignment by reversing the sense of the flag
(statics are automatically initialized to 0). If you did that, though,
your if would have to be against !flagname and whether that generated
more instructions than just testing flagname without the logical
negation would be dependant on the optimizer.
--
Prototypes are supertypes of their clones. -- maplesoft
 
Reply With Quote
 
 
 
 
Al Balmer
Guest
Posts: n/a
 
      06-12-2006
On Mon, 12 Jun 2006 10:31:31 -0700, Ronald Bruck <>
wrote:

>I have several routines which are used millions of times in my
>programs, using the Gnu multi-precision software's floating-point reals
>(see <http://www.swox.se/gmp>). These are of type mpf_t, and I must
>explicitly initialize (and later free) all temporary variables of this
>type.
>
>So I hit on the idea of something like this:
>
> static int firstcall = 1;
> mpf_t temp;
>
> if (firstcall) {
> firstcall = 0;
> mpf_init(temp);
> }
> else blahblah
>
>First time through, firstcall is 1, so temp gets initialized. Second
>(and later) times through, temp is already initialized (with what
>values I don't care, because i'll reset them) and doesn't get
>REinitialized (which would be a fatal error).
>
>It passes -Wall, and it seems to work. Is it legal? Is there anything
>I need to look out for?


Sure. I use this technique often, because a caller can just use the
function without needing to know whether it's been initialized or not,
or whether some other function has already done the job.

This is analogous to data hiding techniques - consider that the
calling function has no need to know the state of the called function,
only that it will do the job requested.
>
>Initializing, and then freeing, an mpf_t millions of times is not
>something I want to do. Especially since it may be thousands of bytes
>long.
>
>I should add, I don't care that I never free temp. The program exits
>before this would become a problem.
>
>--Ron Bruck
>
> Posted Via Usenet.com Premium Usenet Newsgroup Services
>----------------------------------------------------------
> ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
>----------------------------------------------------------
> http://www.usenet.com


--
Al Balmer
Sun City, AZ
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      06-12-2006


Ronald Bruck wrote On 06/12/06 13:31,:
> I have several routines which are used millions of times in my
> programs, using the Gnu multi-precision software's floating-point reals
> (see <http://www.swox.se/gmp>). These are of type mpf_t, and I must
> explicitly initialize (and later free) all temporary variables of this
> type.
>
> So I hit on the idea of something like this:
>
> static int firstcall = 1;
> mpf_t temp;
>
> if (firstcall) {
> firstcall = 0;
> mpf_init(temp);
> }
> else blahblah
>
> First time through, firstcall is 1, so temp gets initialized. Second
> (and later) times through, temp is already initialized (with what
> values I don't care, because i'll reset them) and doesn't get
> REinitialized (which would be a fatal error).
>
> It passes -Wall, and it seems to work. Is it legal? Is there anything
> I need to look out for?


It's fine *if* temp has static storage duration,
which it will have if it's declared `static' or if it's
defined at file scope, outside any function.

If temp is an `auto' variable, it gets created when
control enters its block (often a function) and destroyed
when the block exits. Such a variable will *not* retain
its value -- including its initialized or non-initialized
status -- across different invocations of the function.

--


 
Reply With Quote
 
Al Balmer
Guest
Posts: n/a
 
      06-12-2006
On Mon, 12 Jun 2006 17:59:16 +0000 (UTC),
(Walter Roberson) wrote:

>In article <120620061031319493%>,
>Ronald Bruck <> wrote:
>
>>So I hit on the idea of something like this:

>
>> static int firstcall = 1;
>> mpf_t temp;

>
>> if (firstcall) {
>> firstcall = 0;
>> mpf_init(temp);
>> }
>> else blahblah

>
>>First time through, firstcall is 1, so temp gets initialized. Second
>>(and later) times through, temp is already initialized (with what
>>values I don't care, because i'll reset them) and doesn't get
>>REinitialized (which would be a fatal error).

>
>>It passes -Wall, and it seems to work. Is it legal? Is there anything
>>I need to look out for?

>
>Yes, that is legal.
>
>You could also save an assignment by reversing the sense of the flag
>(statics are automatically initialized to 0). If you did that, though,
>your if would have to be against !flagname and whether that generated
>more instructions than just testing flagname without the logical
>negation would be dependant on the optimizer.


In fact, I usually write

static int initialized = 0;
.
.

if (!initialized)
...

not because it might save a cycle one time, but because it expresses
my intention more clearly. IMO.

--
Al Balmer
Sun City, AZ
 
Reply With Quote
 
Mark Odell
Guest
Posts: n/a
 
      06-12-2006

Al Balmer wrote:
> On Mon, 12 Jun 2006 17:59:16 +0000 (UTC),
> (Walter Roberson) wrote:
>
> >In article <120620061031319493%>,
> >Ronald Bruck <> wrote:
> >
> >>So I hit on the idea of something like this:

> >
> >> static int firstcall = 1;
> >> mpf_t temp;

> >
> >> if (firstcall) {
> >> firstcall = 0;
> >> mpf_init(temp);
> >> }
> >> else blahblah

> >
> >>First time through, firstcall is 1, so temp gets initialized. Second
> >>(and later) times through, temp is already initialized (with what
> >>values I don't care, because i'll reset them) and doesn't get
> >>REinitialized (which would be a fatal error).

> >
> >>It passes -Wall, and it seems to work. Is it legal? Is there anything
> >>I need to look out for?

> >
> >Yes, that is legal.
> >
> >You could also save an assignment by reversing the sense of the flag
> >(statics are automatically initialized to 0). If you did that, though,
> >your if would have to be against !flagname and whether that generated
> >more instructions than just testing flagname without the logical
> >negation would be dependant on the optimizer.

>
> In fact, I usually write
>
> static int initialized = 0;
> .
> .
>
> if (!initialized)
> ...
>
> not because it might save a cycle one time, but because it expresses
> my intention more clearly. IMO.


However, in embedded land, we often discourage this assignment because:
1) it's redundant and 2) usually adds to the size of the executable
image that we are trying to cram into Flash (e.g. it gets moved from
..bss into the .data section).
--
- Mark

 
Reply With Quote
 
Michael Mair
Guest
Posts: n/a
 
      06-12-2006
Ronald Bruck schrieb:
> I have several routines which are used millions of times in my
> programs, using the Gnu multi-precision software's floating-point reals
> (see <http://www.swox.se/gmp>). These are of type mpf_t, and I must
> explicitly initialize (and later free) all temporary variables of this
> type.
>
> So I hit on the idea of something like this:
>
> static int firstcall = 1;
> mpf_t temp;
>
> if (firstcall) {
> firstcall = 0;
> mpf_init(temp);
> }
> else blahblah
>
> First time through, firstcall is 1, so temp gets initialized. Second
> (and later) times through, temp is already initialized (with what
> values I don't care, because i'll reset them) and doesn't get
> REinitialized (which would be a fatal error).
>
> It passes -Wall, and it seems to work. Is it legal? Is there anything
> I need to look out for?


Yes: For this to work as intended,
1) temp must have static storage duration
2) temp is not read from before it is assigned some kind of value
3) the function containing the above (say foo) is allowed to be
non-reentrant

The latter may be the one which gives you a heavy headache:
Essentially, this means that foo() must not recursively call itself
and you have to be careful in an environment where there may be
more than one "instance" of the function at work at the same time
(i.e. using foo() in a shared library of some kind is not possible,
working on several processors is not possible, ...)


Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      06-12-2006
"Mark Odell" <> writes:
> Al Balmer wrote:

[...]
>> In fact, I usually write
>>
>> static int initialized = 0;
>> .
>> .
>>
>> if (!initialized)
>> ...
>>
>> not because it might save a cycle one time, but because it expresses
>> my intention more clearly. IMO.

>
> However, in embedded land, we often discourage this assignment because:
> 1) it's redundant and 2) usually adds to the size of the executable
> image that we are trying to cram into Flash (e.g. it gets moved from
> .bss into the .data section).


I'd expect a decent optimiziing compiler to generate the same code for
static int initialized = 0;
and
static int initialized;
Do compilers for embedded systems commonly fail to do this?

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      06-12-2006
In article <>,
Keith Thompson <kst-> wrote:
>I'd expect a decent optimiziing compiler to generate the same code for
> static int initialized = 0;
>and
> static int initialized;
>Do compilers for embedded systems commonly fail to do this?


I believe that some of the [decent] hosted compilers I have used within
the last decade have generated different code for the two cases.
It wasn't important to me at the time, so I did not pay attention
to whether the behaviour changed in later versions.
--
Prototypes are supertypes of their clones. -- maplesoft
 
Reply With Quote
 
William Ahern
Guest
Posts: n/a
 
      06-12-2006
On Mon, 12 Jun 2006 10:31:31 -0700, Ronald Bruck wrote:

> I have several routines which are used millions of times in my programs,
> using the Gnu multi-precision software's floating-point reals (see
> <http://www.swox.se/gmp>). These are of type mpf_t, and I must explicitly
> initialize (and later free) all temporary variables of this type.
>
> So I hit on the idea of something like this:
>
> static int firstcall = 1;
> mpf_t temp;
>
> if (firstcall) {
> firstcall = 0;
> mpf_init(temp);
> }
> else blahblah
>
> First time through, firstcall is 1, so temp gets initialized. Second (and
> later) times through, temp is already initialized (with what values I
> don't care, because i'll reset them) and doesn't get REinitialized (which
> would be a fatal error).
>
> It passes -Wall, and it seems to work. Is it legal? Is there anything I
> need to look out for?


No, because temp doesn't have static duration. When this function exists
and you call it again, you can't assume temp is initialized. You'll need
to make it static as well.

> Initializing, and then freeing, an mpf_t millions of times is not
> something I want to do. Especially since it may be thousands of bytes
> long.
>
> I should add, I don't care that I never free temp. The program exits
> before this would become a problem.
>


You should take this up in a GMP newsgroup or mailing list. As far as I
know, a default compile of GMP will attempt to use alloca() or an
equivalent allocation device which uses automatic storage. This can be
significantly faster than malloc(), but also means that even if you
declare temp as static it still won't be initialized next time you enter
the function. But I'm no expert, and you should follow up on this issue.

- Bill

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
init of class members : mem(0) vs. mem() vs. not-init at all news.aon.at C++ 11 01-29-2011 07:30 PM
questions about object initialization, default-init and value-init Jess C++ 4 05-04-2007 02:47 AM
Sequence Order between Page Init and User Control Init Tony Cheng ASP .Net 1 02-24-2006 01:56 PM
Compiler/Linker Error undefined reference to 'std::ios_base::Init::Init[in-charge]() clusardi2k@aol.com C++ 1 08-18-2005 07:11 PM
using of "static const" in class with no imediately init heinquoi C++ 2 05-10-2004 11:39 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57