|
Personal experience says your approach will not work. Inaccessible address, I think. Depends on compiler, I guess. Maybe exception error at run time...
IIRC we are talking heap versus stack issues.
|
|
|
|
|
(Surprised) I tried it and it worked. There was no problem accessing the static variable by address outside the function.
Be certain not to in-line the function, as that has certain ramifications.
The static is created on the heap, and the address *is* accessible from outside the class and outside the function it is defined in.
I am quite surprised. I thought even the compiler would catch it. Ignore previous post.
<br />
int *CPBDialog::TryThis()<br />
{<br />
static int test;<br />
<br />
test += 1;<br />
return &test;<br />
}<br />
<br />
<br />
CErrorOkDlg::CErrorOkDlg(CWnd* pParent )<br />
: CPBDialog(CErrorOkDlg::IDD, pParent)<br />
{<br />
}<br />
<br />
<br />
BOOL CErrorOkDlg::OnInitDialog() <br />
{<br />
CPBDialog::OnInitDialog();<br />
<br />
ModifyStyleEx(0,WS_EX_NODRAG,0);<br />
<br />
SoundWarning();<br />
int *test = TryThis();<br />
int test2 = *test;<br />
<br />
return FALSE;<br />
}<br />
<br />
<br />
Gary
|
|
|
|
|
Thanks for your help and sample, Gary!
It is great!
regards,
George
|
|
|
|
|
This is a very interresting article about the use and the concept of the static keyword
clickety[^]
codito ergo sum
|
|
|
|
|
Thanks BadKarma,
If I mean local static function variable, and if I use the address of the variable as the return value, then access the address outside the function (read/write). Does this approach have any disadvantages?
regards,
George
|
|
|
|
|
Whether this is a bad design or a good one, depends on the place where you use this. So you should think carefully where to implement.
There is however on big disadvantage to this design. The function isn't thread safe.
This means that when called from multiple threads the behavior will be unpredictable.
codito ergo sum
|
|
|
|
|
Thanks BadKarma,
BadKarma wrote: This means that when called from multiple threads the behavior will be unpredictable
Could you provide more detailed scenario to describe why it is not thread safe please?
regards,
George
|
|
|
|
|
do you know what thread safe means ?
can't you search the web before asking dumb questions like you know doing very well, and weighting the forum for nothing ?
returning the address of a local static variable isn't thread safe simply means that if a thread is calling the function (so, potentially being modifying the variable value), but another thread is modifying the variable at the same time, using the direct access through its address, you cannot ensure of the final value of the variable...
when both threads write at the same place at the same time, the one which wrote the memory at last wins the game...
ps: so i see you keep playing with the voting system... be very careful when voting my posts. they can just explode on you face, but then don't ever wonder why :p
|
|
|
|
|
Thanks toxcct,
Your answer is clear.
regards,
George
|
|
|
|
|
int& MyFunction( )
{
static int localStatic = 0;
int nMyArray[ 10 ];
if ( localStatic < 10 )
{
//localStatic is not guaranteed < 10
nMyArray[ localStatic ] = 1;
}
return localStatic;
}
When one tread comes inside the “if statement” another tread could have set localStatic to for instance 11.
If you want to use a variable outside the scope of the function why are you not using a global?
When using a static in a function this “means” that the function is responsible for this variable. And it cannot, when this variable is managed outside this function. Keeping strict rules provides confusing situations, In terms of who is modifying what.
Another thing you should know is that when you are using a static within a class function, different instances of this class would use the same static instance; this can cause problems like the tread example above.
Using globals:
In header:
extern int g_MyVar;
In cpp
int g_MyVar = 0;
or when possible using a local variable giving to the function
void MyFunction( int& nValue )
{
nValue++;
…
}
int nValue = 0;
MyFunction( nValue );
|
|
|
|
|
Thanks koos,
Your answer is great!
regards,
George
|
|
|
|
|
George_George wrote: I am wondering how C or C++ manages static variable internally.
I believe it's in the data segment.
"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
|
|
|
|
|
Thanks DavidCrow,
Data segment is some global space of a process, compared with function local stack space?
regards,
George
|
|
|
|
|
George_George wrote: I am wondering how C or C++ manages static variable internally.
Global statics are placed on the heap, not the stack.
They are initialized upon first use by the application.
They are destroyed upon exit from the application.
Whereas variables on the stack come and go, the heap hangs around until program termination.
I had thought Global statics were handled differently than Local statics, but I proved myself wrong.
Beware of INLINE statics, however. If the code is actually INLINED, the compiler may create multiple instances of the static variable storage, plus one that is global. This will no doubt lead to problems.
Gary
|
|
|
|
|
Thanks Gary,
ghle wrote: I had thought Global statics were handled differently than Local statics, but I proved myself wrong.
You mean global static variable and function local variable are handled in the same way by compiler?
regards,
George
|
|
|
|
|
George_George wrote: You mean global static variable and function local variable are handled in the same way by compiler?
George, both the global statics and local statics are placed onto the heap - by the compiler. I'd say they are handled similar, but not identical. For example, global statics are initialized at start-up of the program, but local statics are not initialized until they first come into scope. Both are destroyed only upon termination of the program, that is when the heap is destroyed.
You said "function local variable", but I mean function local STATIC variable. A function local variable - non-static - are pushed onto the stack not put in the heap. When the function goes out of scope, so do it's variables. Returning from the function to the calling program will pop the stack, removing all local non-static variables from existence. But the function static variables were put on the heap, and they still exist after the function goes out of scope, available to the routine/method once it comes back into scope from another call. (And as noted earlier in this thread, I found local statics were available from outside of the local function if the address of the static is returned by the method.)
Heap
Global statics
Main() statics
Method B statics
Method C statics
/Heap
<br />
Stack after calling Main()<br />
Main() non-static variables<br />
/Stack<br />
<br />
Stack after calling method B from Main()<br />
Main() non-static variables<br />
Method B non-static variables<br />
/Stack<br />
<br />
Stack after calling method C from B<br />
Main() non-static variables<br />
Method B non-static variables<br />
Method C non-static variables<br />
/Stack<br />
<br />
Stack after calling method C from method C (recursion)<br />
Main() non-static variables<br />
Method B non-static variables<br />
Method C non-static variables<br />
Susequent method C non-static variables<br />
/Stack<br />
<br />
Stack after returning from method C to method B<br />
Main() non-static variables<br />
Method B non-static variables<br />
/Stack<br />
<br />
Stack after returning to Main() from method B<br />
Main() non-static variables<br />
/Stack
Heap after returning to Main(). All statics now initialized.
Global statics
Main() statics
Method B statics
Method C statics
/Heap
Gary
-- modified at 8:43 Monday 27th August, 2007
|
|
|
|
|
Thanks Gary,
Your reply is great!
regards,
George
|
|
|
|
|
thanks for your advice and help,what do you mean by 'doh' if you have any idea about the registry cleaner please help me asap.
|
|
|
|
|
dear, you are wanting to clean the registry isnt it?
|
|
|
|
|
Registry Cleaner is an application that is responsible for cleaning up the invalid entries that exist in the registry. These invalid entries could be those that are left after uninstalling a software, could be an invalid entry in the Known Dll's key, could delete cookies, temporary internet files and blah blah....
You would need to figure out what do you want your registry cleaner to do.
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
|
|
|
|
|
niki dutta wrote: thanks for your advice and help,what do you mean by 'doh' if you have any idea about the registry cleaner please help me asap.
have you have the clear requirement in your mind or written some where!
"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
|
|
|
|
|
niki dutta wrote: thanks for your advice and help,what do you mean by 'doh' if you have any idea about the registry cleaner please help me asap.
what is you question ?
actually, i know you're not asking one ; you were replying to someone. but then WHY don't you follow the same thread instead of starting a new one ?!
don't matter the thread moved on the second page and not visible directly anymore, because the guy you replied to will receive a mail as a notification...
|
|
|
|
|
Do you need to tool for clean registry? or its nit your question?
|
|
|
|
|
niki dutta wrote: if you have any idea about the registry cleaner please help me asap.
Stop this nonsense. Ask an intelligent question, or don't bother posting. No one is going to do your work for you so stop asking. If an article is not working for you, contact its author.
"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
|
|
|
|
|
i have 20 edit box on my dialog, it was formated into 4 rows with 5 edit box with each row.
these edit box displays integers. i want to save/load the integers to/from a .txt file.
the .txt file should have the following format,each number corresponds to each edit box, and each row corresponds to each row of edit box:
50,5,3,2,6
32,56,7,8,9
12,34,56,7,9
1,2,4,5,6
1,2,3,4,5
the numbers are just above is just an example.
How i do this?
thx
|
|
|
|
|