Function and variable declarations

J

Joseph Wakeling

Hello all,

Here's a brief function from the aforementioned (and controversial) GNU
Scientific Library (GSL), or rather, a slightly rewritten version of it
that I've made. The function takes an integer and sets up certain
necessary values for a random number generator. x, n and shuffle in
this function are global variables.

/*************************************************************/
static void
ran1_set (unsigned long int s) {
int i;

if(s==0)
s = 1;

for(i=0;i<8;++i) {
long int h = s / q;
long int t = a * (s - h * q) - h * r;
if(t < 0)
t += m;
s = t;
}

for(i=N_SHUFFLE-1;i>=0;--i) {
long int h = s/q;
long int t = a * (s - h * q) - h * r;
if(t < 0)
t += m;
s = t;
shuffle = s;
}

x = s;
n = s;

return;
}
/*************************************************************/

Anyway, two questions:

(1) why declare the function a "static void" instead of just void? (My
personal use of "static" is just to preserve values between different
calls to modules or functions, but I know its use can be more complex
than this.)

(2) Any particular reason why the long int's h and t are declared twice
inside different loops, instead of just being declared at the beginning
of the function?

Many thanks,

-- Joe
 
R

rayw

Joseph Wakeling said:
Hello all,

Here's a brief function from the aforementioned (and controversial) GNU
Scientific Library (GSL), or rather, a slightly rewritten version of it
that I've made. The function takes an integer and sets up certain
necessary values for a random number generator. x, n and shuffle in
this function are global variables.

/*************************************************************/
static void
ran1_set (unsigned long int s) {
int i;

if(s==0)
s = 1;

for(i=0;i<8;++i) {
long int h = s / q;
long int t = a * (s - h * q) - h * r;
if(t < 0)
t += m;
s = t;
}

for(i=N_SHUFFLE-1;i>=0;--i) {
long int h = s/q;
long int t = a * (s - h * q) - h * r;
if(t < 0)
t += m;
s = t;
shuffle = s;
}

x = s;
n = s;

return;
}
/*************************************************************/

Anyway, two questions:

(1) why declare the function a "static void" instead of just void? (My
personal use of "static" is just to preserve values between different
calls to modules or functions, but I know its use can be more complex
than this.)

(2) Any particular reason why the long int's h and t are declared twice
inside different loops, instead of just being declared at the beginning
of the function?


1. The use of static for a function restricts the function's visibility - to
that of the translation unit (file) it's defined in.

2. As far as I know - Nope.
 
F

Flash Gordon

Joseph said:
Hello all,

Here's a brief function from the aforementioned (and controversial) GNU
Scientific Library (GSL), or rather, a slightly rewritten version of it
that I've made. The function takes an integer and sets up certain
necessary values for a random number generator. x, n and shuffle in
this function are global variables.

/*************************************************************/
static void
ran1_set (unsigned long int s) {
int i;

if(s==0)
s = 1;

for(i=0;i<8;++i) {
long int h = s / q;
long int t = a * (s - h * q) - h * r;
if(t < 0)
t += m;
s = t;
}

for(i=N_SHUFFLE-1;i>=0;--i) {
long int h = s/q;
long int t = a * (s - h * q) - h * r;
if(t < 0)
t += m;
s = t;
shuffle = s;
}

x = s;
n = s;


Global (or at least file scope) variable named x and n? Yuk.

Rather pointless having a return here IMHO.
}
/*************************************************************/

Anyway, two questions:

(1) why declare the function a "static void" instead of just void? (My
personal use of "static" is just to preserve values between different
calls to modules or functions, but I know its use can be more complex
than this.)

When used on a function or a variable at file scope it means
approximately "don't make the name available to other modules." Or, to
use standard terminology, it only has internal linkage.
(2) Any particular reason why the long int's h and t are declared twice
inside different loops, instead of just being declared at the beginning
of the function?

It's called minimising scope. Because they are declared in the loop you
know, without having to check, that they are not used outside.
Otherwise, you would have to read beyond the end of the loop to see if
the last value gets used outside the loop.
 
S

Skarmander

Joseph said:
static void
ran1_set (unsigned long int s) {
for(i=0;i<8;++i) {
long int h = s / q;
long int t = a * (s - h * q) - h * r;
if(t < 0)
t += m;
s = t;
}

for(i=N_SHUFFLE-1;i>=0;--i) {
long int h = s/q;
long int t = a * (s - h * q) - h * r;
if(t < 0)
t += m;
s = t;
shuffle = s;
}

(1) why declare the function a "static void" instead of just void? (My
personal use of "static" is just to preserve values between different
calls to modules or functions, but I know its use can be more complex
than this.)
"static" in declarations has two different meanings; one is to give an
identifier static storage (the use you describe), the other is to give
it internal linkage (this use). Here "static" just means "not extern",
that is, the function symbol is internal to the unit and inaccessible
from the outside. This improves modularity.

There's actually a third meaning in C99, used in passing array
parameters, but if you're interested in this you can look it up. You're
not likely to encounter it in the wild yet. And we'll ignore C++, which
assigns yet another meaning to it.
(2) Any particular reason why the long int's h and t are declared twice
inside different loops, instead of just being declared at the beginning
of the function?
Declaring variables in the innermost block they are used in is good for
readability and may have positive effects on register usage too. In this
case it doesn't buy you much, but it's not hurting anyone either.

S.
 
R

rayw

There's actually a third meaning in C99, used in passing array parameters,
but if you're interested in this you can look it up. You're not likely to
encounter it in the wild yet. And we'll ignore C++, which assigns yet
another meaning to it.

void f(double a[static 3]);

Specifies that the argument corresponding to 'a' in any call to f must be a
non-null pointer to the first element????
 
M

Michael Mair

rayw said:
There's actually a third meaning in C99, used in passing array parameters,
but if you're interested in this you can look it up. You're not likely to
encounter it in the wild yet. And we'll ignore C++, which assigns yet
another meaning to it.

void f(double a[static 3]);

Specifies that the argument corresponding to 'a' in any call to f must be a
non-null pointer to the first element????

Among other things also this.
The above tells you that the array a points to has at least
three elements, i.e. that you can safely access a[0], a[1], a[2].
This can give rise to optimisations and (conversely) the
compiler may give you a diagnostic when you pass a pointer
to an array which is either NULL or does not point to an
array with sufficiently many elements.


Cheers
Michael
 
J

jacob navia

Flash Gordon a écrit :
Global (or at least file scope) variable named x and n? Yuk.

I would like to emphasize that.

It is ALWAYS an error to name a GLOBAL variable (i.e. at file or even
process scope) with a name like "n" or "x".

This is bound to provoke name clashes. Who hasn't a local variable
named "x" in a math package somewhere???

Now suppose you mistype the declaration of your x, and istead of writing
int x;
you type
int xc;

The compiler SILENTLY will use the global variable x, that you will
destroy!!!

Today, names can be quite long in C, some compilers accept 256 or even
more characters in a name.

There is a measure in everything, and extremely long names could
be a bore to type, but a global variable should always have a
name that conveys something about its usage, and avoids name clashes.

This is even more necessary in the case of a library like in this example.

If you are writing a LIBRARY, you should do your best to avoid
polluting your user's name space, prefixing all your globals
(if you need globals) with the prefix of your library. Note that
the gsl library always uses gsl_something for its names. I would
ve very surprised if they would leave a variable like 'x'
floatng around... Nobody could use such a library.

jacob
 
M

Malcolm

Skarmander said:
Declaring variables in the innermost block they are used in is good for
readability and may have positive effects on register usage too. In this
case it doesn't buy you much, but it's not hurting anyone either.
I believe in the rule of three.

A human being can cope with three layers of nested parentheses, three levels
of indirection, three dimensions, and three levels of scope.
In C these are global, file scope, and function scope. By adding more levels
you render your program non-human understandable.
 
F

Flash Gordon

Malcolm said:
I believe in the rule of three.

A human being can cope with three layers of nested parentheses, three levels
of indirection, three dimensions, and three levels of scope.
In C these are global, file scope, and function scope. By adding more levels
you render your program non-human understandable.

I don't have a problem reading other peoples code that uses block scope
variables, but perhaps I am non-human. Sometimes they make it easier to
understand, because you don't have to worry about whether they are used
outside that scope. Having said that, I won't have multiple blocks using
the same name for separate variables, I would prefer either distinct
names or to define than at function scope.
 
S

Skarmander

Malcolm said:
I believe in the rule of three.

A human being can cope with three layers of nested parentheses, three levels
of indirection, three dimensions, and three levels of scope.
In C these are global, file scope, and function scope. By adding more levels
you render your program non-human understandable.
I believe in the rule of seven.

A human being can cope with at most seven items held in active memory
before you have to start referring back to refresh your memory. If, by
putting your variables in subblocks, you can reduce the number of
outstanding variables that have to be kept in active memory at any given
point, you've achieved something.

That said, I've found that very often when people start declaring
variables in subblocks (especially when it's more than one variable)
it's usually time to split the block off to a new function, rather than
trying to keep it all squeezed into one.

S.
 
C

Christopher Benson-Manica

jacob navia said:
There is a measure in everything, and extremely long names could
be a bore to type, but a global variable should always have a
name that conveys something about its usage, and avoids name clashes.

Indeed. We, until recently, had global variables argc and argv (you
read that right) visible to all portions of all modules. It was only
after a number of predictable problems had been caused that this
design faux pas was undone.
 
J

Joseph Wakeling

Thanks to all for helpful answers about this!


Flash said:
Global (or at least file scope) variable named x and n? Yuk.

Normally I would agree. Originally these variable came as part of a
structure called "state", so the system was referring to state->x,
state->n and state->shuffle. Given the content of the program and how
it's going to be used (as an individually compiled module) it's not a
big deal.

Rather pointless having a return here IMHO.

That came with the GSL and I left it in. return; at the end of a void
function is a matter of preference anyway, right? It doesn't
functionally affect things but some people prefer it for readability.

It's called minimising scope. Because they are declared in the loop you
know, without having to check, that they are not used outside.
Otherwise, you would have to read beyond the end of the loop to see if
the last value gets used outside the loop.

OK. Does it make any difference to the speed at which the code runs?
Let's assume I'm going to be calling this function a LOT so small
differences add up. :)

Many thanks again for this & all other useful advice.

-- Joe
 
J

Joseph Wakeling

Christopher said:
We, until recently, had global variables argc and argv (you
read that right) visible to all portions of all modules. It was only
after a number of predictable problems had been caused that this
design faux pas was undone.

How do you go about making variables global with respect to several
modules? I know about using the extern declaration for variables in a
module, but I thought that was generally pretty bad coding practice
because it limits the ability to make the modules stand effectively on
their own.

-- Joe
 
P

pemo

Joseph Wakeling said:
How do you go about making variables global with respect to several
modules? I know about using the extern declaration for variables in a
module, but I thought that was generally pretty bad coding practice
because it limits the ability to make the modules stand effectively on
their own.

There are basically only two ways, i.e., 1. have a defining declaration in
one .c module (i.e., a variable/const placed outside of any function body),
and then access that via extern in other modules - then the linker will
resolve the situation. 2. Use pointers, i.e., have a function in the same
module as the 'static' global that returns the address of variable/const to
a caller - although that rather defeats the purpose I think.
 
P

pemo

Flash Gordon said:
I don't have a problem reading other peoples code that uses block scope
variables, but perhaps I am non-human. Sometimes they make it easier to
understand, because you don't have to worry about whether they are used
outside that scope. Having said that, I won't have multiple blocks using
the same name for separate variables, I would prefer either distinct names
or to define than at function scope.

I'm still a bit 'old school' I'm afraid, and typically declare my variables
at the top of a function - no matter where they're used in that function.

Many years ago, I would have loved having some mechanism that allowed me to
declare them 'nearer' to where they were used - but, then, C wouldn't allow
that. Nowadays, [being an 'old dog' that can't learn 'new tricks'] I stick
with my old habit. However, one thing has improved so much that it's made
things easier, namely, decent IDEs. Years ago, when one only had dumb
editors, it was sometimes really hard to find where things had been
declared/defined. but now, well, I just ask the IDE, and it shows me where a
'thing' is. That's allowed me to learn one new trick however - I no longer
find that hungarian notation is terribly useful.
 
F

Flash Gordon

pemo wrote:

I'm still a bit 'old school' I'm afraid, and typically declare my variables
at the top of a function - no matter where they're used in that function.

Many years ago, I would have loved having some mechanism that allowed me to
declare them 'nearer' to where they were used - but, then, C wouldn't allow
that. Nowadays, [being an 'old dog' that can't learn 'new tricks'] I stick

Must be very old school, since it is allowed by C89.
with my old habit. However, one thing has improved so much that it's made
things easier, namely, decent IDEs. Years ago, when one only had dumb
editors, it was sometimes really hard to find where things had been
declared/defined. but now, well, I just ask the IDE, and it shows me where a
'thing' is. That's allowed me to learn one new trick however - I no longer
find that hungarian notation is terribly useful.

I have never found hungarian notation useful.
 
F

Flash Gordon

Joseph said:
Thanks to all for helpful answers about this!



Normally I would agree. Originally these variable came as part of a
structure called "state", so the system was referring to state->x,
state->n and state->shuffle. Given the content of the program and how
it's going to be used (as an individually compiled module) it's not a
big deal.

I stand by my comment. If I had to review that code I would tell you to
go and fix it. After all, some other poor chap may have to maintain that
code.
That came with the GSL and I left it in. return; at the end of a void
function is a matter of preference anyway, right? It doesn't
functionally affect things but some people prefer it for readability.
Agreed.


OK. Does it make any difference to the speed at which the code runs?
Let's assume I'm going to be calling this function a LOT so small
differences add up. :)

It depends. The only way to find out it to measure on your specific
system. However, my opinion would be that with modern compilers it will
generally make absolutely no difference one way or the other.

If you are concerned with speed the first thing to do is forget it
unless you have a very good reason to think it won't be fast enough.

If you actually do find yourself with a real performance issue, don't
start by looking at the code, start by looking at the algorithm to see
if there is a more efficient algorithm.

My experience is that trying to optimise one function only helps if that
function is very badly written, the compiler is very bad, or you are
doing embedded work and there is a time constraint on that specific
function (e.g. it has to complete during the video blanking period).
Other than those rare cases there is more to be had by improving the
algorithm.
 
J

Joseph Wakeling

Flash said:
I stand by my comment. If I had to review that code I would tell you to
go and fix it. After all, some other poor chap may have to maintain that
code.

I'm being lazy right now because the chance of anyone else even *using*
that code, let alone maintaining it, are about zero. But you're right.
:)

My experience is that trying to optimise one function only helps if that
function is very badly written, the compiler is very bad, or you are
doing embedded work and there is a time constraint on that specific
function (e.g. it has to complete during the video blanking period).
Other than those rare cases there is more to be had by improving the
algorithm.

My experience too. But if one is running the same function millions of
times, every little helps... ;-)
 
P

pemo

Flash Gordon said:
pemo wrote:

I'm still a bit 'old school' I'm afraid, and typically declare my
variables at the top of a function - no matter where they're used in that
function.

Many years ago, I would have loved having some mechanism that allowed me
to declare them 'nearer' to where they were used - but, then, C wouldn't
allow that. Nowadays, [being an 'old dog' that can't learn 'new tricks']
I stick

Must be very old school, since it is allowed by C89.

Um, let's see - can't remember exactly when I started, but it was around
1980 [and I used to be quite good at it then I believe]
I have never found hungarian notation useful.

I had to use it - guess where I used to work!
 
C

Christopher Benson-Manica

Joseph Wakeling said:
How do you go about making variables global with respect to several
modules?

I spoke poorly there. What I should have said is that our "main"
header file that everything includes used to define these variables as
global. It made for more than its share of headaches.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top