可能重复:
由于内存导致重新分配失败时如何处理?
假设我有一个指针数组
char **pointers_to_pChar = 0;
pointers_to_pChar = (char **)malloc(sizeof(char *) * SIZE);
for (i = 0; i < SIZE; ++i)
{
pointers_to_pChar[i] = malloc(sizeof(char) * 100));
}
//some stuff...
//time to realloc
pointers_to_pChar = realloc(pointers_to_pChar, sizeof(char *) * pointer_count + 1);
if (pointers_to_pChar == NULL)
{
//i have to free pointers in the array but i don't have access to array anymore...
}
realloc 失败时应该如何处理?我需要访问数组中的每个指针并释放它们以避免可能的内存泄漏。