Skip to content

Instantly share code, notes, and snippets.

@ak110
Created July 22, 2013 10:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ak110/6052971 to your computer and use it in GitHub Desktop.
Save ak110/6052971 to your computer and use it in GitHub Desktop.
関数ローカルのstatic変数の仕組み。
; 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
// 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