新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > 結構體中動態內存的管理(malloc和free)

        結構體中動態內存的管理(malloc和free)

        作者: 時間:2016-12-01 來源:網絡 收藏
        #include
        #include
        #include
        struct student
        {
        char *name;
        int score;
        }stu,*pstu;
        int main()
        {
        /*為name分配指向的一段內存空間*/
        stu.name = (char *)malloc(20*sizeof(char));
        memset(stu.name,0,20*sizeof(char));
        strcpy(stu.name,"Jimy");
        stu.score = 99;
        /*為pstu分配指向的一段內存空間*/
        pstu = (struct student *)malloc(sizeof(struct student));
        memset(pstu,0,sizeof(struct student));
        /*為name分配指向的一段內存空間*/
        pstu->name = (char *)malloc(20*sizeof(char));
        memset(pstu->name,0,20*sizeof(char));
        strcpy(pstu->name,"Jimy");
        pstu->score = 99;
        /*采用另外的指針訪問分配的存儲空間,測試內存中內容是否改變*/
        char *p = stu.name;
        char *p1 = (char *)0x804a008;//具體的地址值
        char *ppstu = pstu->name;
        char *pp = (char *)0x804a030;//具體的地址值
        /*釋放的順序要注意,pstu->name必須在pstu釋放之前釋放,
        *如果pstu先釋放,那么pstu->name就不能正確的訪問。
        */
        free(pstu->name);
        free(stu.name);
        free(pstu);
        /*為了防止野指針產生*/
        pstu->name = NULL;
        stu.name = NULL;
        pstu = NULL;
        p = NULL;
        ppstu = NULL;
        return 0;
        }
        下面的全部是調試結果,根據調試結果說明問題:
        (gdb) r
        Starting program: /home/gong/program/cprogram/TestStructPoint
        Breakpoint 1, main () at TestStructPoint.c:14
        14 stu.name = (char *)malloc(20*sizeof(char));
        Missing separate debuginfos, use: debuginfo-install glibc-2.12.90-17.i686
        (gdb) p stu
        $1 = {name = 0x0, score = 0}
        (gdb) p stu.name
        $2 = 0x0
        (gdb) c
        Continuing.
        Breakpoint 2, main () at TestStructPoint.c:17
        17 strcpy(stu.name,"Jimy");
        (gdb) p stu.name
        $3 = 0x804a008 ""
        (gdb) c
        Continuing.
        Breakpoint 3, main () at TestStructPoint.c:21
        21 pstu = (struct student *)malloc(sizeof(struct student));
        (gdb) p stu.name
        $4 = 0x804a008 "Jimy"
        (gdb) p stu
        $5 = {name = 0x804a008 "Jimy", score = 99}
        (gdb) p pstu
        $6 = (struct student *) 0x0
        (gdb) c
        Continuing.
        Breakpoint 4, main () at TestStructPoint.c:24
        24 pstu->name = (char *)malloc(20*sizeof(char));
        (gdb) p pstu
        $7 = (struct student *) 0x804a020
        (gdb) p pstu->name
        $8 = 0x0
        (gdb) c
        Continuing.
        Breakpoint 5, main () at TestStructPoint.c:27
        27 strcpy(pstu->name,"Jimy");
        (gdb) p pstu->name
        $9 = 0x804a030 ""
        (gdb) c
        Continuing.
        Breakpoint 6, main () at TestStructPoint.c:31
        31 char *p = stu.name;
        (gdb) p pstu->name
        $10 = 0x804a030 "Jimy"
        (gdb) p *pstu
        $11 = {name = 0x804a030 "Jimy", score = 99}
        (gdb) p p
        $12 = 0x854ff4 "|M205"
        (gdb) c
        Continuing.
        Breakpoint 7, main () at TestStructPoint.c:32
        32 char *p1 = (char *)0x804a008;//具體的地址值
        (gdb) p p1
        $13 = 0x855ca0 ""
        (gdb) c
        Continuing.
        Breakpoint 8, main () at TestStructPoint.c:33
        33 char *ppstu = pstu->name;
        (gdb) p p1
        $14 = 0x804a008 "Jimy"
        (gdb) p ppstu
        $15 = 0x855ca0 ""
        (gdb) c
        Continuing.
        Breakpoint 9, main () at TestStructPoint.c:34
        34 char *pp = (char *)0x804a030;//具體的地址值
        (gdb) p ppstu
        $16 = 0x804a030 "Jimy"
        (gdb) p pp
        $17 = 0x804826a "__libc_start_main"
        (gdb) c
        Continuing.
        Breakpoint 10, main () at TestStructPoint.c:37
        37 free(pstu->name);
        (gdb) p pp
        $18 = 0x804a030 "Jimy"
        (gdb) p pstu->name
        $19 = 0x804a030 "Jimy"
        (gdb) c
        Continuing.
        Breakpoint 11, main () at TestStructPoint.c:38
        38 free(stu.name);
        (gdb) p pstu->name
        $20 = 0x804a030 ""
        (gdb) c
        Continuing.
        Breakpoint 12, main () at TestStructPoint.c:39
        39 free(pstu);
        (gdb) p stu.name
        $21 = 0x804a008 "(240 主站蜘蛛池模板: 通渭县| 天津市| 鹰潭市| 麻栗坡县| 福贡县| 连江县| 纳雍县| 镇沅| 新建县| 聊城市| 普宁市| 田东县| 英超| 阿鲁科尔沁旗| 万宁市| 昭平县| 泌阳县| 金寨县| 大同市| 临夏县| 大埔县| 高密市| 平遥县| 得荣县| 宝应县| 溧水县| 拉萨市| 铜山县| 左贡县| 建平县| 阆中市| 鹿泉市| 布拖县| 青河县| 吴江市| 临澧县| 会东县| 乐至县| 林周县| 织金县| 威信县|