473,505 Members | 14,136 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Static variables

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 ?

--
David Buck
2D CAD for RISC OS at www.risccad.freeuk.com
Jan 8 '06 #1
4 1734
"David Buck" <da********@freeuk.com> writes:
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?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
Jan 8 '06 #2
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 ?

--
A+

Emmanuel Delahaye
Jan 8 '06 #3
Emmanuel Delahaye wrote:
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 ?


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.

--
David Buck
2D CAD for RISC OS at www.risccad.freeuk.com
Jan 9 '06 #4
David Buck wrote:
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.)

Jan 9 '06 #5

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

Similar topics

1
3660
by: James | last post by:
Hello Java NG, I not sure if this is the right NG for this type of question but if not please let me know which is, TIA Any way first off let me say I'm a student and this WAS last weeks lab,...
2
1855
by: katekukku | last post by:
HI, Could anyone please tell me what are static variables and what exactly are there features. I am a little bit confused. Thank You
9
6347
by: Bryan Parkoff | last post by:
I have noticed that C programmers put static keyword beside global variable and global functions in C source codes. I believe that it is not necessary and it is not the practice in C++. Static...
4
1855
by: Dave | last post by:
I used the following class and .aspx code below to understand how static works on variables and methods taken from...
8
6774
by: Simone Chiaretta | last post by:
I've a very strange behaveour related to a website we built: from times to times, something should happen on the server, and all static variables inside the web application, both defined inside aspx...
28
4574
by: Dennis | last post by:
I have a function which is called from a loop many times. In that function, I use three variables as counters and for other purposes. I can either use DIM for declaring the variables or Static. ...
5
6759
by: Jesper Schmidt | last post by:
When does CLR performs initialization of static variables in a class library? (1) when the class library is loaded (2) when a static variable is first referenced (3) when... It seems that...
9
8620
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang...
55
6150
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
16
8592
by: RB | last post by:
Hi clever people :-) I've noticed a lot of people stating not to use static variables with ASP.NET, and, as I understand it, the reason is because the variable is shared across user sessions -...
0
7216
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
7098
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
7367
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7018
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
5613
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,...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1528
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 ...
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
407
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.