ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 연속 vs 불연속 2
    프로그래밍 언어 C, C++ 2021. 1. 4. 21:51
    728x90

    연속 vs 불연속 1 이어 작성한다.

    jamesbbun.tistory.com/9

     

    연속 vs 불연속 1

    정의 vs 선언1 에서 선언은 할당을 포함한다고 언급했다(할당=메모리확보) 링크 참조 jamesbbun.tistory.com/15 정의 vs 선언 1 Java, C# 고급 언어를 먼저 사용하고 C, C++을 나중에 시작했다면, 컴파일 에러

    jamesbbun.tistory.com

     

    지난 포스터에서 말했듯이, 컴파일 에러를 막더라도 런타임 에러는 막을수 없다.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    void main()
    {
        int i, j;
     
        //int a[4][2] = { {0, 1}, {2, 3}, {4, 5}, {6, 7} };
        //동적 할당으로 위의 값처럼 대입
        int** a = (int**)malloc(sizeof(int*)*4);
        for(i=0; i<4; i++)
        {
            a[i] = (int*)malloc(sizeof(int)*2);
            for(j=0; j<2; j++)
                a[i][j] = 2 * i + j;
        }
     
        int* b = (int*)a;
        for(i=0; i<8; i++)
            printf("b[%d] = %d\n", i, b[i]);
    }
     
    cs

    a는 결국 연속적이지 못하기 때문에, 아래처럼 쓸수 밖에 없다.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    void main()
    {
        int i, j;
     
        //int a[4][2] = { {0, 1}, {2, 3}, {4, 5}, {6, 7} };
        //동적 할당으로 위의 값처럼 대입
        int** a = (int**)malloc(sizeof(int*)*4);
        for(i=0; i<4; i++)
        {
            a[i] = (int*)malloc(sizeof(int)*2);
            for(j=0; j<2; j++)
                a[i][j] = 2 * i + j;
        }
     
        int** b = a;
        for(i=0; i<4; i++)
        {
            for(j=0; j<2; j++)
                printf("b[%d][%d] = %d\n", i, j, b[i][j]);
        }
    }
     
    cs

    다만, b[0], b[1], b[2], b[3]가 가지고 있는 메모리는 연속적이라서 아래처럼 쓸수 있다.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    void main()
    {
        int i, j;
     
        //int a[4][2] = { {0, 1}, {2, 3}, {4, 5}, {6, 7} };
        //동적 할당으로 위의 값처럼 대입
        int** a = (int**)malloc(sizeof(int*)*4);
        for(i=0; i<4; i++)
        {
            a[i] = (int*)malloc(sizeof(int)*2);
            for(j=0; j<2; j++)
                a[i][j] = 2 * i + j;
        }
     
        int** b = a;
        for(i=0; i<4; i++)
        {
            int* c = b[i];
            for(j=0; j<2; j++)
                printf("b[%d][%d] = %d\n", i, j, c[j]);
        }
    }
     
    cs

    공부하는 입장에서 보면 이 예제가 별로 도움이 안될것 처럼 보일것이다.

    조금 윽지 스럽지만, 게임 상에서 활용될때 이런식으로 표현된다.

    유형 1, 유형 2 실행하면 동일한 효과가 나온다.

    메모리의 연속과 불연속을 제대로 이해하고 있을때 코드의 표현력이 달라진다.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    struct Monster
    {
        const char* name;
        int hp;// 체력
        int ap;// 공격력
        int dp;// 방어력
        float rate;// 공격속도
    };
     
    Monster monster[2][4=
    {
        // 지상 유닛
        {
            { "쥐"10320.1f },
            { "토끼"10320.1f },
            { "고양이"10320.1f },
            { "개"10320.1f },
        },
        // 공중 유닛
        {
            { "참새"10320.1f },
            { "비둘기"10320.1f },
            { "까마귀"10320.1f },
            { "독수리"10320.1f },
        }
    };
     
    void main()
    {
        int i, j;
     
        const char* str[2= { "지상 유닛""공중 유닛" };
     
        // 유형 1
         for(i=0; i<2; i++)
        {
            printf("<<< %s >>> \n", str[i]);
            Monster* mon = monster[i];
     
            for(j=0; j<4; j++)
            {
                Monster* m = mon[j];
                printf("Monster[%d][%d] = { %s, %d, %d, %d, %f}\n",
                    i, j, m->name, m->hp, m->ap, m->dp, m->rate);
            }
        }
     
        // 유형 2
         for(i=0; i<8; i++)
        {
            if( i%4 == 0 )
                printf("<<< %s >>> \n", str[i/4]);
            Monster* m = monster[i][j];
            printf("Monster[%d][%d] = { %s, %d, %d, %d, %f}\n",
                    i, j, m->name, m->hp, m->ap, m->dp, m->rate);
        }
    }
     
    cs

     

    '프로그래밍 언어 C, C++' 카테고리의 다른 글

    프로그래머 워밍업 2  (0) 2021.01.09
    프로그래머 워밍업 1  (0) 2021.01.07
    개발자, 무엇을 준비 할까?  (0) 2021.01.04
    Visual Studio 2019 셋팅  (0) 2021.01.04
    연속 vs 불연속 1  (0) 2021.01.04
Designed by Tistory.