Static variables

D

David Buck

If I have the following setup

void Vdlog_SetVport(int reason)
{
static double lx,ly,hx,hy;

Vmath_GetBox(&lx,&ly,&hx,&hy);
....
}

void Vmath_GetBox(double *lx,double *ly,double *hx,double *hy)
{
.....
}

Will the pointers passed to Vmath_GetBox be out of scope (I have tried and
it causes a crash) ?

Ie are static variables only visible within the routine to which they are
declared ?
 
K

Keith Thompson

David Buck said:
If I have the following setup

void Vdlog_SetVport(int reason)
{
static double lx,ly,hx,hy;

Vmath_GetBox(&lx,&ly,&hx,&hy);
...
}

void Vmath_GetBox(double *lx,double *ly,double *hx,double *hy)
{
....
}

Will the pointers passed to Vmath_GetBox be out of scope (I have tried and
it causes a crash) ?

Ie are static variables only visible within the routine to which they are
declared ?

If you declare a static variable within a block, its name is visible
only within that block, but it has static storage duration, meaning
that it exists for the entire execution of the program. There should
be no problem with the code you posted. Even if the variables weren't
static, accessing them indirectly from a called function should be ok;
their lifetime doesn't end until Vdlog_SetVport() terminates, which
won't happen until *after* Vmath_GetBox() terminates. (In the latter
case, you could have problems if Vmath_GetBox() stashes away the
addresses of its parameters for use after Vdlog_SetVport() has
terminated, but even that's not a problem if the variables are static
-- though it would be horrible style.)

Is there a prototype for Vmath_GetBox() visible at the point of the
call? Even that probably shouldn't cause any visible problems, since
the types of the arguments match the expected types of the parameters,
unless your actual code differs from what you posted.

Can you trim your program to a small but complete program that
exhibits the problem?
 
E

Emmanuel Delahaye

David Buck a écrit :
If I have the following setup

void Vdlog_SetVport(int reason)
{
static double lx,ly,hx,hy;

Vmath_GetBox(&lx,&ly,&hx,&hy);
...
}

void Vmath_GetBox(double *lx,double *ly,double *hx,double *hy)
{
....
}

Will the pointers passed to Vmath_GetBox be out of scope

No. And additionally, the variables in Vdlog_SetVport() don't need to be
static (at least for what we know about them)
(I have tried and
it causes a crash) ?

Sounds to be for another reason.
Ie are static variables only visible within the routine to which they are
declared ?

Yes, but their durations are program life. Hence you can pass their
addresses to anyone.

Design issue:

When you have more than one pointer to 'similary' data as parameters of
a function, it probably means that you'd better use some structure and a
pointer to a structure. It makes interfaces and coding less clumsy.

struct my_data
{
double lx;
double ly;
double hx;
double hy;
};

void Vdlog_SetVport(int reason)
{
/* you probably don't need this 'static' */
static my_data data;

Vmath_GetBox(&data);
...
}

void Vmath_GetBox(my_data *p_data)

see my point ?
 
O

Old Wolf

David said:
void Vdlog_SetVport(int reason)
{
static double lx,ly,hx,hy;
Vmath_GetBox(&lx,&ly,&hx,&hy);

Will the pointers passed to Vmath_GetBox be out of scope

Pointers don't have scope. Identifiers have scope, and variables have
lifetime.

The scope of the identifiers "lx", "ly", "hx", and "hy" is local to
that function. Writing "lx" somewhere else in your program will
say "undefined identifier" or some similar error.

But the variables identified by those identifiers, have a permanent
lifetime (ie. they always exist). You can access those variables at
any point in your program -- as long as you have some way of
knowing whereabouts in memory they are, if you are accessing
them from somewhere where their names are not in scope.
(I have tried and it causes a crash) ?

Your crash is for some other reason. Try posting a complete
compilable program that demonstrates the crash.

(NB. Google ran out of hard disk space when i was trying to
post this. Apologies if it ends up as a double-post.)
 
D

David Buck

Emmanuel said:
David Buck a écrit :

No. And additionally, the variables in Vdlog_SetVport() don't need to
be static (at least for what we know about them)


Sounds to be for another reason.


Yes, but their durations are program life. Hence you can pass their
addresses to anyone.

Design issue:

When you have more than one pointer to 'similary' data as parameters
of a function, it probably means that you'd better use some structure
and a pointer to a structure. It makes interfaces and coding less
clumsy.
struct my_data
{
double lx;
double ly;
double hx;
double hy;
};

void Vdlog_SetVport(int reason)
{
/* you probably don't need this 'static' */
static my_data data;

Vmath_GetBox(&data);
...
}

void Vmath_GetBox(my_data *p_data)

see my point ?

Thanks everyone, I have re-coded with a structure as suggested.

The problem is that the program operates on a system whereby the routines
are re-entered with various reason codes, in a multitasking WIMP
environment, so Vdlog_SetVport does terminate after setting up the
Vmath_GetBox function (which waits for the user to enter co-ordinates using
the mouse). Vmath_GetBox then fills in the entered co-ordinates in the *lx
pointer, which, I presume, as Vdlog_SetVport is not current, cannot be
accessed.
 

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
474,172
Messages
2,570,934
Members
47,477
Latest member
ColumbusMa

Latest Threads

Top