|
Im sorry - I read your mail above to state "I don't know call stack...".
My fault. Please excuse me.
Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal. George Orwell, "Keep the Aspidistra Flying", Opening words
|
|
|
|
|
|
I did reply to you by mail (using the mail-button below the text). You did not receive my Mail?
As an alternative, could you put the code on some of the free webspace providers and post the adress here?
I am reluctant to post my mail adress here in the open.
Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal. George Orwell, "Keep the Aspidistra Flying", Opening words
|
|
|
|
|
devvvy wrote: do you think I can send you the stripped down package?
As a professional, you should not have a need to do this. Simply do as previously suggested and you'll find the exact cause of the problem. The debugger is your friend. It exists for a reason.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
My MFC experience told me sometimes some MFC supplied member/attributes no valid until some sequence completed... using it before hand would crash it...
can't remember the detail now that's just an impression from a difficult bug i had long ago. But yes step thru the code with a debugger will allow you to zoom in on the problem
|
|
|
|
|
You mean like, oh, I don't know, maybe creating the resource (HWND, HFONT, HBITMAP, etc) first by calling one of the CreateXXX methods on MFC? Is that what you're referring to?
¡El diablo está en mis pantalones! ¡Mire, mire!
Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)!
SELECT * FROM User WHERE Clue > 0
0 rows returned
Save an Orange - Use the VCF!
VCF Blog
|
|
|
|
|
Hello everyone,
I am wondering how C or C++ manages static variable internally. Since each time when we again a function again, if in this function, a static variable is defined, the value will be the value last time when we entered this function (i.e. will not be initialized again, and only initialized at the 1st time).
I suspect it is stored in some global structure to reserve the value?
thanks in advance,
George
|
|
|
|
|
My understanding is that static vars are initialised before main() is called. You could check this by creating a class with a constructor, init a static variable of this type, put a breakpoint in the constructor and see what happens
|
|
|
|
|
Thanks Josh,
Do you mean static variable inside a function? If yes, how do you think such *local* static variable will be stored, since normal local variable (value) will vanish after function returns. But for static *local* variable, the value will not vanish -- and will be recorded next time when we entered the same function.
Any ideas or comments?
regards,
George
|
|
|
|
|
I dont know to be honest but I would imagine that all static variables are pushed onto the stack before main() is called and therefore "local" statics dont really go out of scope when a function ends. I suspect the actual details may vary between compilers.
|
|
|
|
|
Thanks Josh,
Good answer! I have a further question about programming best practice. Do you think it is safe to let the address of the *local* static variable as the return value of a function? Then other part of code (out of this function) will access or even modify the variable by the returned address of the *local* static variable?
Any disadvantages of this approach?
Example,
<br />
<br />
int* func()<br />
{<br />
static int i;<br />
<br />
return &i;<br />
}<br />
<br />
regards,
George
|
|
|
|
|
when you mark some variable static, compiler wil decide how to treat that variable.. so there no scope of local and global variable concept here!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow Never mind - my own stupidity is the source of every "problem" - Mixture
cheers,
Alok Gupta
VC Forum Q&A :- I
IV
Support CRY- Child Relief and You
|
|
|
|
|
Thanks Alok!
If you think static variable is different from local or global variables, how do you think static variable is stored and managed so that the value is reserved each time we enters the function in which the static variable is defined?
regards,
George
|
|
|
|
|
George_George wrote: Good answer! I have a further question about programming best practice. Do you think it is safe to let the address of the *local* static variable as the return value of a function? Then other part of code (out of this function) will access or even modify the variable by the returned address of the *local* static variable?
Any disadvantages of this approach?
I'd say it smells like bad design, the only reason to return a pointer rather than a copy of the int is to allow some other code to modify it. I'd rather provide a method to allow other code to modify the static.
|
|
|
|
|
Josh Gray wrote: I'd say it smells like bad design, the only reason to return a pointer rather than a copy of the int is to allow some other code to modify it. I'd rather provide a method to allow other code to modify the static.
sorry to interrupt you.. it's somewhat Singleton pattern...! so i don't think it bad design.. though it break OOPS basic concept of encapsulation and abstraction!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow Never mind - my own stupidity is the source of every "problem" - Mixture
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You
|
|
|
|
|
ThatsAlok wrote: i don't think it bad design.. though it break OOPS basic concept of encapsulation and abstraction!
Which to me makes it bad design. You can still have the static and therefore the singleton pattern but my comments was more about how you expose the interface to it. Returning the address of what is basically a private variable is bad.
|
|
|
|
|
Thanks Josh,
I can generally understand and agree with your points. But I am confused about some details. Could you show us your points by a couple of lines of code please?
regards,
George
|
|
|
|
|
Thanks Alok!
How do you think returning static variable is similar to Singleton pattern? Any more details about how similar points do they have?
regards,
George
|
|
|
|
|
George_George wrote: How do you think returning static variable is similar to Singleton pattern
just on basic.. concept of Singleton pattern says that.. there should only copy exist for any variable.. look more in detail here:-
http://www.codeproject.com/cpp/singletonrvs.asp[^]
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow Never mind - my own stupidity is the source of every "problem" - Mixture
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You
|
|
|
|
|
Thanks Alok,
The similarity you mean is only one copy of data?
regards,
George
|
|
|
|
|
thats what single pattern all about
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow Never mind - my own stupidity is the source of every "problem" - Mixture
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You
|
|
|
|
|
Thanks Josh,
Josh Gray wrote: I'd rather provide a method to allow other code to modify the static.
I agree with most points of you. But I do not quite understand your above points. Could you show me a sample please about how to modify the static variable please?
I am wondering how to modify the value of the static variable from other function (other than from the function in which it is defined)?
regards,
George
|
|
|
|
|
George_George wrote: I am wondering how to modify the value of the static variable from other function (other than from the function in which it is defined)?
Sorry, my mistake as you cant do that.
You can however define a static in a class and access it from any of the methods in that class. Even const methods!
|
|
|
|
|
Hi Josh,
I think you mean static member of a class. But I mean a static variable defined inside a function.
regards,
George
|
|
|
|
|
George_George wrote: I think you mean static member of a class. But I mean a static variable defined inside a function.
Yes you are right which makes me wonder why you have global methods? I suspect there is probably a better to solution to what you are trying to do but its hard to suggest anything without knowing more about what you are doing and in what context
|
|
|
|