考虑的变量与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