0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

C言語 func1(){int a=8;} func2(){int b;} bの中身は8

Posted at

実験概要

実験用プログラム

test.c
#include<stdio.h>

void func1(){
    int a=8;
}

void func2(){
    int b;
    printf("%d",b);
}

int main(){
    func1();
    func2();
    return 0;
}

func1及びfunc2のアセンブリ表記

func1

image.png

func2

image.png

実行結果

8

考察

アセンブリ言語を見ると、変数abDWORD PTR [rbp-0x4]となっている。
どちらもmainから直接呼び出された関数なので、fanc1fanc2が呼び出された時点でrbpは一致するはずである。
スタックを解放する時、rsprbpの挿しているアドレスを変更するだけで値の初期化はしていない。
よってbが指しているDWORD PTR [rbp-0x4]aで代入された8が残ったままとなっている。
(スタックフレームのことは又別で投稿する)

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?