본문 바로가기

IT/자료구조

[자료구조] malloc을 이용한 더블포인터와 주소



안녕하세요 지토우에요.

오늘은 malloc을 이용해 더블포인터 소스를 공부해봅시다.




이 소스에 gdb를 돌리면 어떻게 될까요~?




[ds1603:~, 4]$ gdb malloc3

GNU gdb (GDB) Red Hat Enterprise Linux (7.2-48.el6)

Copyright (C) 2010 Free Software Foundation, Inc.

License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software: you are free to change and redistribute it.

There is NO WARRANTY, to the extent permitted by law.  Type "show copying"

and "show warranty" for details.

This GDB was configured as "i686-redhat-linux-gnu".

For bug reporting instructions, please see:

<http://www.gnu.org/software/gdb/bugs/>...

Reading symbols from /home/ds-1/ds1603/malloc3...done.

(gdb) list 1,30

1       #include <stdio.h>

2       #include <stdlib.h>

3

4       int main()

5       {

6               int **p;

7               int i, j;

8               int m, n; // m 반 n 학생

9

10              printf("input M N : ");

11              scanf("%d %d", &m, &n);

12

13              p=(int **) malloc(sizeof(int *)*m); //m는 반의 개수

14

15              for(i=0; i<m; i++)

16              {

17                      *(p+i)=(int *)malloc(sizeof(int) * n); //10은 학생 수

18                      for(j=0;j<n;j++)

19                      {

20                              *(*(p+i)+j) = j +1;

21                      }

22              }

23

24              for(i=0; i<m; i++)

25              {

26                      printf("[%d 반] : ", i+1);

27                      for(j=0; j<n; j++)

28                              printf("%d ",*(*(p+i)+j));

29                      printf("\n");

30              }

(gdb) break 24

Breakpoint 1 at 0x804852a: file malloc3.c, line 24.

(gdb) run

Starting program: /home/ds-1/ds1603/malloc3

input M N : 4 6


Breakpoint 1, main () at malloc3.c:24

24              for(i=0; i<m; i++)

Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.192.el6.i686

(gdb) print p

$1 = (int **) 0x804a008

(gdb) print p+1

$2 = (int **) 0x804a00c

(gdb) print p+2

$3 = (int **) 0x804a010

(gdb) print p+3

$4 = (int **) 0x804a014

(gdb) print *(p+0)+0

$5 = (int *) 0x804a020

(gdb) print *(p+0)+1

$6 = (int *) 0x804a024

(gdb) print *(p+0)+2

$7 = (int *) 0x804a028

(gdb) print *(p+0)+3

$8 = (int *) 0x804a02c

(gdb) print *(p+0)+4

$9 = (int *) 0x804a030

(gdb) print *(p+0)+5

$10 = (int *) 0x804a034

(gdb) print *(p+1)+0

$11 = (int *) 0x804a040

(gdb) print *(p+1)+1

$12 = (int *) 0x804a044

(gdb) print *(p+1)+2

$13 = (int *) 0x804a048

(gdb) print *(p+1)+3

$14 = (int *) 0x804a04c

(gdb) print *(p+1)+4

$15 = (int *) 0x804a050

(gdb) print *(p+1)+5

$16 = (int *) 0x804a054

(gdb) print *(p+2)+0

$17 = (int *) 0x804a060

(gdb) print *(p+2)+1

$18 = (int *) 0x804a064

(gdb) print *(p+2)+2

$19 = (int *) 0x804a068

(gdb) print *(p+2)+3

$20 = (int *) 0x804a06c

(gdb) print *(p+2)+4

$21 = (int *) 0x804a070

(gdb) print *(p+2)+5

$22 = (int *) 0x804a074

(gdb) print *(p+3)+0

$23 = (int *) 0x804a080

(gdb) print *(p+3)+1

$24 = (int *) 0x804a084

(gdb) print *(p+3)+2

$25 = (int *) 0x804a088

(gdb) print *(p+3)+3

$26 = (int *) 0x804a08c

(gdb) print *(p+3)+4

$27 = (int *) 0x804a090

(gdb) print *(p+3)+5

$28 = (int *) 0x804a094

(gdb) print *(p+4)+0

$29 = (int *) 0x0



이렇게 보니 뭐가 뭔지 잘 모르시겠지요 ...




input m n : 

에서 4와 6을 넣어줬기 때문에

2번째 네모들은 4개이고, 3번째 네모들은 가로로 6씩입니다.

'IT > 자료구조' 카테고리의 다른 글

원형 연결리스트 코드  (0) 2017.11.23
[자료구조] 배열  (0) 2017.08.03
[자료구조] 추상 데이터 타입  (0) 2017.08.01
자료구조와 알고리즘  (0) 2017.08.01