亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区
Chinaunix
標(biāo)題:
請問我這個malloc分配的內(nèi)存,該如何釋放
[打印本頁]
作者:
bw_0927
時間:
2011-08-23 14:27
標(biāo)題:
請問我這個malloc分配的內(nèi)存,該如何釋放
struct entry
{
char* attrName;
}
entry* visitEntry()
{
entry *tmp = (entry*)malloc(100);
/*do something with tmp*/
attr(tmp);
return tmp;
}
node* attr(entry* node)
{
char *tmpAttr = (char*)malloc(100);
/*do something with tmpAttr*/
node->attrName = tmpAttr;
return node;
}
復(fù)制代碼
entry()會被調(diào)用好多次,請問這兩個函數(shù)中的內(nèi)存我該如何釋放
作者:
Moon_Bird
時間:
2011-08-23 15:02
在每次使 entry* tmp 重定向的時候,釋放掉它之前的空間,如:
23 node->attrName = tmpAttr;
前 free下 node指針。
不知道 說的對不對。
作者:
crazyhadoop
時間:
2011-08-23 15:30
有幾個malloc就free幾次,至于順序,應(yīng)該是 后malloc的先free
作者:
ilwmin
時間:
2011-08-23 15:39
從內(nèi)到外釋放就可以了。
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct tgentry
{
char* attrName;
}entry;
entry* attr(entry* node)
{
char *tmpAttr = (char*)malloc(100);
printf("tmpAttr=%p\n",tmpAttr);
/*do something with tmpAttr*/
node->attrName = tmpAttr;
printf("node->attrName=%p\n",node->attrName);
memset(node->attrName ,0x00,100);
strcpy(node->attrName ,"testhello");
printf("node=%p\n",node);
return node;
}
entry* visitEntry()
{
entry *tmp = (entry*)malloc(100);
printf("tmp01=%p\n",tmp);
/*do something with tmp*/
attr(tmp);
printf("tmp02=%p\n",tmp);
return tmp;
}
int main( int argc,char *argv[] )
{
entry *tmp;
tmp=visitEntry();
printf("%s\n",tmp->attrName);
printf("tmp03=%p\n",tmp);
printf("tmp->attrName=%p\n",tmp->attrName);
free(tmp->attrName);
free(tmp);
return 0;
}
運(yùn)行結(jié)果如下:
tmp01=003E3D50
tmpAttr=003E2430
node->attrName=003E2430
node=003E3D50
tmp02=003E3D50
testhello
tmp03=003E3D50
tmp->attrName=003E2430
作者:
bw_0927
時間:
2011-08-23 18:26
非常感謝樓上
歡迎光臨 Chinaunix (http://www.72891.cn/)
Powered by Discuz! X3.2