Step by step

My diary

...

Search

breakinformation. Powered by Blogger.

2018년 9월 24일 월요일

multi memory allocation (simplify)


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
#include <stdio.h>
#include <stdlib.h>
 
int main(void) {
    
    int ***threeDimensional = calloc (3sizeof (int**)); // dynamic allocation, height
    unsigned int horizon = 0, vertical = 0, height = 0;
    
    for (vertical = 0; vertical < 4; vertical++) {
        
        threeDimensional[vertical] = calloc (4sizeof (int*)); // dynamic allocation, vertical
        for (horizon = 0; horizon < 5; horizon++) {
            
            threeDimensional[vertical][horizon] = calloc (5sizeof (int)); // dynamic allocation, horizon
        }
    }
    
    for (height = 0; height < 3; height++) {
        for (vertical = 0; vertical < 4; vertical++) {
            for (horizon = 0; horizon < 5; horizon++) {
                
                threeDimensional[height][vertical][horizon] = horizon + vertical + height;
                printf("%d ", threeDimensional[height][vertical][horizon]);
            }
            printf("\n");
        }
        printf("\n");
    }
    
    printf("%d\n"sizeof (threeDimensional));
    printf("%d\n"sizeof (*threeDimensional));
    printf("%d\n"sizeof (**threeDimensional));
    
    //=====================================================
    
    for (vertical = 0; vertical < 4; vertical++) {
        for (horizon = 0; horizon < 5; horizon++) {
            
            free (threeDimensional[vertical][horizon]); // clear dynamic allocation, horizon
        }
        free (threeDimensional[vertical]); // clear dynamic allocation, vertical
    }
    free (threeDimensional); // clear dynamic allocation, height
    
    return 0;
}
cs

0 개의 댓글:

댓글 쓰기