Register | Login
Views: 19364387
Main | Memberlist | Active users | ACS | Commons | Calendar | Online users
Ranks | FAQ | Color Chart | Photo album | IRC Chat
11-02-05 12:59 PM
0 user currently in Programming. | 3 guests
Acmlm's Board - I2 Archive - Programming - Question About Static Variable--Going Out of Scope | |
Add to favorites | "RSS" Feed | Next newer thread | Next older thread
User Post
beneficii

Lakitu
Level: 36

Posts: 393/567
EXP: 299656
For next: 8454

Since: 06-27-04
From: Cordova, TN, USA

Since last post: 14 hours
Last activity: 6 hours
Posted on 08-07-05 03:57 AM Link | Quote
When do static variables (declared in a particular source file) go out of scope? It seems like when a DlgProc is called in a source file the static variables in that source file go out of scope because all of a sudden they get weird random values (making me think that they are as though they haven't been initialized). So what's causing it?
Dish

Spiny
Level: 38

Posts: 504/596
EXP: 355646
For next: 14801

Since: 03-15-04
From: Disch

Since last post: 18 days
Last activity: 18 days
Posted on 08-07-05 04:16 AM Link | Quote
Static variables maintain "normal" scope in the sense that if you declare them within a function, they can only be used inside that function. Or when declared in the class, they can only be used in the class (or by YourClass::varname).

However, if you're talking about when the variable is created/destroyed -- static variables have global scope in that sense -- they're created once on program startup and destroyed on program exit. They maintain their value through any function calls or any scope changes, as long as the running instance remains in tact.

For example:




int CountUp(void)
{
static int count = 0;
count++;
return count;
}




Calling that function will return 1 the first time it's called, then 2 the next time, then 3, and so on. Whereas if 'count' were non-static, you'd get 1 every time.


Static class member variables follow in the same vein. If you declare a static variable within a class, only one variable is created (regardless of the number of class objects created) and EVERY OBJECT of that class type will share that same variable. Likewise if the variable is public -- 'outsiders' to the class can access that variable with 'YourClassName::thevarname' -- you don't even need an object of the class to access it -- since it doesn't rely on any.


(edited by Disch on 08-06-05 07:16 PM)
beneficii

Lakitu
Level: 36

Posts: 394/567
EXP: 299656
For next: 8454

Since: 06-27-04
From: Cordova, TN, USA

Since last post: 14 hours
Last activity: 6 hours
Posted on 08-07-05 04:22 AM Link | Quote
Originally posted by Disch
Static variables maintain "normal" scope in the sense that if you declare them within a function, they can only be used inside that function. Or when declared in the class, they can only be used in the class (or by YourClass::varname).

However, if you're talking about when the variable is created/destroyed -- static variables have global scope in that sense -- they're created once on program startup and destroyed on program exit. They maintain their value through any function calls or any scope changes, as long as the running instance remains in tact.

For example:




int CountUp(void)
{
static int count = 0;
count++;
return count;
}




Calling that function will return 1 the first time it's called, then 2 the next time, then 3, and so on. Whereas if 'count' were non-static, you'd get 1 every time.


Static class member variables follow in the same vein. If you declare a static variable within a class, only one variable is created (regardless of the number of class objects created) and EVERY OBJECT of that class type will share that same variable. Likewise if the variable is public -- 'outsiders' to the class can access that variable with 'YourClassName::thevarname' -- you don't even need an object of the class to access it -- since it doesn't rely on any.


What's been happening is this:

#include "myfavoriteheaderfile.h"

static int var1; //outside of function

int getbox(HWND owner) {
var1 = 5;
return DialogBox((HINSTANCE) GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_MOTHERDUDDER), owner, MyFavProc);
}

BOOL CALLBACK MyFavProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
switch(msg) {
case WM_INITDIALOG:{
char tempchr[10];
itoa(var1, tempchr, 10);
MessageBox(hwnd, tempchr, "Titlebar", MB_OK);
/* In the body of the MessageBox, it will show some weird ass value--every value I've seen is always "80808080" */
/* etc etc rest of program */

Do you seem my point, Disch? It's like var1 went out of scope, because it gave the value of 80808080. What do you recommend for that?


(edited by beneficii on 08-06-05 07:24 PM)
Dish

Spiny
Level: 38

Posts: 505/596
EXP: 355646
For next: 14801

Since: 03-15-04
From: Disch

Since last post: 18 days
Last activity: 18 days
Posted on 08-07-05 04:32 AM Link | Quote
I just tried that code and got it to print 5. Don't know what to tell ya.

Any paticular reason why you need this var to be static anyway? I mean it's already global....
beneficii

Lakitu
Level: 36

Posts: 395/567
EXP: 299656
For next: 8454

Since: 06-27-04
From: Cordova, TN, USA

Since last post: 14 hours
Last activity: 6 hours
Posted on 08-07-05 04:53 AM Link | Quote
Originally posted by Disch
I just tried that code and got it to print 5. Don't know what to tell ya.

Any paticular reason why you need this var to be static anyway? I mean it's already global....


That was just a sample. I thought you would have noticed that wasn't my real program. Anyway, I had the WndProc call a function and it didn't return 80808080 anymore, because like totally that is messed up. Still, somtimes when I call it function from the Proc it'll still give me 80808080. And I get all sorts of unpredictable effects from that.
Dish

Spiny
Level: 38

Posts: 506/596
EXP: 355646
For next: 14801

Since: 03-15-04
From: Disch

Since last post: 18 days
Last activity: 18 days
Posted on 08-07-05 04:59 AM Link | Quote
Originally posted by beneficii

That was just a sample. I thought you would have noticed that wasn't my real program.


Well regardless -- my confirmation of that code suggests that either you didn't post all the relevent code -- or the problem lies elsewhere in your program... or both.

Your var is being corrupted somehow. I couldn't say how without seeing the whole program -- maybe you have a bad pointer... maybe you typoed and are changing that var instead of another... maybe you don't realize you have two areas of your program messing with that var at the same time.... it could be any one of a million things.

static vars don't get destroyed until the instance closes -- that's for sure. So that's one thing that is definatly not your problem.
Add to favorites | "RSS" Feed | Next newer thread | Next older thread
Acmlm's Board - I2 Archive - Programming - Question About Static Variable--Going Out of Scope | |


ABII


AcmlmBoard vl.ol (11-01-05)
© 2000-2005 Acmlm, Emuz, et al



Page rendered in 0.014 seconds.