代碼看起來是這樣的:Ç - 連續釋放calloc調用腐敗一些內存
char *global1 = NULL;
char *global2 = NULL;
char *global3 = NULL;
char *global4 = NULL;
void func2(char *gPtr, char **gPtrToInitialize)
{
if(*gPtrToInitialize == NULL) // Here, gPtr is correct
{
*gPtrToInitialize = dlcalloc(MAX_PATH, 1);
} // Here, gPtr has trailing junk characters
// Do some initialization
}
void func1()
{
if(global1 == NULL)
{
global1 = dlcalloc(MAX_PATH, 1);
}
func2(global1, &global2);
func2(global1, &global3);
func2(global1, &global4);
// Do other tasks
// Free all allocated global variables
}
注: 在上面的代碼,dlcalloc
指Doug Lea的malloc.c定義的代碼。
的calloc
內func2()
,
gPtr
= 「C:\ Program Files文件\測試\ Path.txt」 以前calloc
內func2()
,
後gPtr
=「C:\ Program Files \ Test \Path.txt♂」
我的問題是,做連續dlcalloc()
呼叫具有破壞其他一些變量的內存很小的機率?上面的代碼是我正在處理的某個大代碼庫的一部分的泛化。