[英]Variables confusion
我必須在函數中讀取更多元素P. 每次循環創建pElem更好嗎?
dataStr * process(char *start, char *stop, GTree* tree)
{
while ( (cp != NULL) && ( cp < nextI))
{
//I malloc inside of getPElem function
pElem * p = getPElem(cp, dateP, s);
free(p);
}
}
或者我應該更好地初始化一次P元素並每次重復使用它?
dataStr * process(char *start, char *stop, GTree* tree)
{
pElem * p = malloc(sizeof(p));
while ( (cp != NULL) && ( cp < nextI))
{
fillPElem(p, cp, dateP, s);
}
free(p);
}
如果一個元素會更好,我應該在函數外面對它進行malloc(函數“process”也在循環中調用):
dataStr * process(char *start, char *stop, GTree* tree, pElem * p )
{
while ( (cp != NULL) && ( cp < nextI))
{
fillPElem(p, cp, dateP, s);
}
}
或者每次在函數內部都像第二個例子一樣?
如果您不需要pElems比封閉范圍更長壽,則根本不需要動態分配:
dataStr * process(char *start, char *stop, GTree* tree)
{
pElem p;
while ( (cp != NULL) && ( cp < nextI))
{
fillPElem(&p, cp, dateP, s);
}
}
(是的,我知道cp,nextI等都沒有定義 - 我只是從問題中復制過來)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.