新聞中心

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

結構體中動態(tài)內(nèi)存的管理(malloc和free)

作者: 時間:2016-12-01 來源:網(wǎng)絡 收藏
#include
#include
#include
struct student
{
char *name;
int score;
}stu,*pstu;
int main()
{
/*為name分配指向的一段內(nèi)存空間*/
stu.name = (char *)malloc(20*sizeof(char));
memset(stu.name,0,20*sizeof(char));
strcpy(stu.name,"Jimy");
stu.score = 99;
/*為pstu分配指向的一段內(nèi)存空間*/
pstu = (struct student *)malloc(sizeof(struct student));
memset(pstu,0,sizeof(struct student));
/*為name分配指向的一段內(nèi)存空間*/
pstu->name = (char *)malloc(20*sizeof(char));
memset(pstu->name,0,20*sizeof(char));
strcpy(pstu->name,"Jimy");
pstu->score = 99;
/*采用另外的指針訪問分配的存儲空間,測試內(nèi)存中內(nèi)容是否改變*/
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);
/*為了防止野指針產(chǎn)生*/
pstu->name = NULL;
stu.name = NULL;
pstu = NULL;
p = NULL;
ppstu = NULL;
return 0;
}
下面的全部是調(diào)試結果,根據(jù)調(diào)試結果說明問題:
(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