考慮的變量與malloc
定義(char *)
sTmp
,「malloc」ed內存是否隱式釋放?
如果我分配另一個變量此sTmp
,確實通過這個變量分配的存儲器獲得自動釋放或這是否存儲塊保持直到該過程結束分配(&無用) ?
讓我用一個代碼解釋:
int main() {
char *sTmp = (char *) malloc (50);
char *sNew = strdup ("some text"); // length is less than 50
sTmp = sNew;
/* now sTmp points to sNew and it's length is 10 chars
I wonder, what happened to the allocated memory of 50 chars in the first line;
Does it get freed implicitly or remain allocated?
*/
}
您已經創建了內存泄漏。內存仍然分配。 – perh 2014-09-28 11:55:48