1833777 Members
2163 Online
110063 Solutions
New Discussion

doubt on static variable

 
SOLVED
Go to solution
Vishal Augustine
Frequent Advisor

doubt on static variable

Hi

Is it possible by any chance that a static variable (C++) will get corrupted ? I set a value for a static variable and just access it .. never writes it. Is it possible that the variable will get corrupt if the process is a heavy weight one ?

Thanks and Regrads
Vishal
2 REPLIES 2
Solution

Re: doubt on static variable

In general, there is no guarantee that even const variables can be protected against corruption, given a sufficiently buggy or perverse program. If, somehow, a pointer in another compilation unit points to its memory location (perhaps the pointer is uninitialised and starts with a garbage value that just happens to be the address of the variable) and the value pointed to is assigned, the static variable can be changed even though the assignment code does not have the static variable in scope.

That said, if something really should never change, you should declare it to be const. The compiler might be able to place the thing into a read-only section. Illegal attempts to alter its value will then cause a SEGV, and you will easilly be able to locate the bug.
Vishal Augustine
Frequent Advisor

Re: doubt on static variable

Thanks. Tht was a convincing reply ...

Regards
Vishal