Created
July 22, 2013 10:45
-
-
Save ak110/6052971 to your computer and use it in GitHub Desktop.
関数ローカルのstatic変数の仕組み。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; Function compile flags: /Ogtpy | |
; File c:\data\test\statictest.cpp | |
; COMDAT ?StaticTest@@YAHXZ | |
_TEXT SEGMENT | |
?StaticTest@@YAHXZ PROC ; StaticTest, COMDAT | |
; 6 : static int s = std::rand(); | |
mov eax, DWORD PTR ?$S1@?1??StaticTest@@YAHXZ@4IA | |
test al, 1 | |
jne SHORT $LN4@StaticTest | |
or eax, 1 | |
mov DWORD PTR ?$S1@?1??StaticTest@@YAHXZ@4IA, eax | |
call _rand | |
; 7 : return ++s; | |
inc eax | |
mov DWORD PTR ?s@?1??StaticTest@@YAHXZ@4HA, eax | |
; 8 : } | |
ret 0 | |
$LN4@StaticTest: | |
; 7 : return ++s; | |
mov eax, DWORD PTR ?s@?1??StaticTest@@YAHXZ@4HA | |
inc eax | |
mov DWORD PTR ?s@?1??StaticTest@@YAHXZ@4HA, eax | |
; 8 : } | |
ret 0 | |
?StaticTest@@YAHXZ ENDP ; StaticTest | |
_TEXT ENDS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// call "%VS110COMNTOOLS%vsvars32.bat" & cl /O2 /Fa /FAs StaticTest.cpp | |
#include <cstdio> | |
#include <cstdlib> | |
int StaticTest() { | |
static int s = std::rand(); | |
return ++s; | |
} | |
int main() { | |
std::printf("%d", StaticTest()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment