static struct and char arrays

A

A

Hi,

I want to use the advantage of having single allocation of a certain struct
or array but keep it "local".
For example - not to pollute my namespace I like to use a lot of "local"
functions like this:

void Function1()
{
struct TLocalFunction1
{
void operator()(int i)
{
char abc[] = {1,2,3,4,5};
}
} LocalFunction1

// call "local" function
LocalFunction(1);
LocalFunction(2);
// etc...
}

Now the problem is that I think this might be slowing down Function1 because
TLocalFunction1 and char abc are constantly initializing from scratch.

So, might I speed up if I prefix "struct TLocalFunction1" and "char abc"
with the keyword "static" would that speed up the Function1 in terms of no
more multiple allocations of TLocalFunction1 and char abc. Or is that
compiler-dependent?
 
I

Ian Collins

Hi,

I want to use the advantage of having single allocation of a certain struct
or array but keep it "local".
For example - not to pollute my namespace I like to use a lot of "local"
functions like this:

void Function1()
{
struct TLocalFunction1
{
void operator()(int i)
{
char abc[] = {1,2,3,4,5};
}
} LocalFunction1

// call "local" function
LocalFunction(1);
LocalFunction(2);
// etc...
}

Now the problem is that I think this might be slowing down Function1 because
TLocalFunction1 and char abc are constantly initializing from scratch.

So, might I speed up if I prefix "struct TLocalFunction1" and "char abc"
with the keyword "static" would that speed up the Function1 in terms of no
more multiple allocations of TLocalFunction1 and char abc. Or is that
compiler-dependent?

Probably. In this case there's nothing to initialise in the struct its
self, but the char array may as well be declared static.
 
K

Kai-Uwe Bux

A said:
Hi,

I want to use the advantage of having single allocation of a certain
struct or array but keep it "local".
For example - not to pollute my namespace I like to use a lot of "local"
functions like this:

void Function1()
{
struct TLocalFunction1
{
void operator()(int i)
{
char abc[] = {1,2,3,4,5};
}
} LocalFunction1

// call "local" function
LocalFunction(1);
LocalFunction(2);
// etc...
}

Now the problem is that I think this might be slowing down Function1
because TLocalFunction1 and char abc are constantly initializing from
scratch.

So, might I speed up if I prefix "struct TLocalFunction1" and "char abc"
with the keyword "static" would that speed up the Function1 in terms of no
more multiple allocations of TLocalFunction1 and char abc. Or is that
compiler-dependent?

Some cases:

a) If the array does not need initialization, the compiler can very likely
see that and optimize the initialization away.

b) If the array is non-const, needs initialization, and the contents changes
during the evaluation of LocalFunction, it has to be reinitialized anyway.
Otherwise the second call to LocalFunction() would have the garbage contents
from the previous run.

c) If the array is const, declare it as such and let the compiler take care
of the possible optimizations.


Best,

Kai-Uwe Bux
 
A

A

i didn't though of const.... but i think you may be right it might be used
probably for some optimizations by the compiler.
maybe also declaring "static const struct" or just "const struct" as well
then?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top