IT

스택 영역은 밑에서 올라간다.

Data,Bss 영역은 위에서 내려간다.

 

함수 안에 static 변수를 쓰면 전역 변수처럼 프로그램이 종료될 때 메모리가 해제 된다.

또한 static 변수를 초기화 하지 않으면 자동으로 0으로 초기화가 되지만 ,

메모리 영역은 BSS 영역의 메모리를 할당 받는다.

Static 변수를 초기화 하면 Data 영역의 메모리를 할당 받는다.

  • 리눅스 환경에서는 위와 같다, Windows 환경에서는 다를 수 있다.

 

#include <stdio.h>
int f=6;
int g;
int h=7;
int i;
int j;

void test(int iNum);
int main()

    int a = 1; 
    int b = 2; 
    int c = 3; 
    int d = 4; 
    int e = 5; 

    int *p = (int*)malloc(4); 

    static int z; 

    printf("Code Address : %08X\n",main); 
    printf("Heap Address : %08X\n",p); 
    printf("Stack Address : %08X\n",&p); 
    free(p); 

    printf("\nStack Address\n"); 
    printf("a[%08X],%d\n",&a,a); 
    printf("b[%08X],%d\n",&b,b); 
    printf("c[%08X],%d\n",&c,c); 
    printf("d[%08X],%d\n",&d,d); 
    printf("e[%08X],%d\n",&e,e); 

    printf("\nData Address\n"); 
    printf("f[%08X],%d\n",&f,f); 
    printf("h[%08X],%d\n",&h,h); 

    printf("\nBss Address\n"); 
    printf("g[%08X],%d\n",&g,g); 
    printf("i[%08X],%d\n",&i,i); 
    printf("j[%08X],%d\n",&j,j); 

    printf("z[%08X],%d\n",&z,z);

    test(3); 
    test(3); 
    test(3); 

    return 0;
}

void test(int iNum)

    static int x=10; 
    static int x1; 
    int y=10; 

    y = y + iNum; 
    x = x + iNum; 
    x1 = x1 + iNum; 

    printf("Data Address\n"); 
    printf("X[%08X],%d\n",&x,x); 
    printf("Bss Address\n"); 
    printf("X1[%08X],%d\n",&x1,x1); 
    printf("Stack Address\n"); 
    printf("y[%08X],%d\n",&y,y);
}

'소프트웨어 > Linux' 카테고리의 다른 글

언어 인코딩  (0) 2013.09.11
분산 컴파일  (0) 2011.07.28
부팅 과정  (0) 2011.07.28
fopen, open의 차이점  (0) 2011.07.28
[파일 입출력] 고수준, 저수준  (0) 2011.07.28
Posted by sinpk