Я столкнулся с чем-то странным при освобождении памяти в двумерном массиве после того, как что-то пошло не так.
Дело 1:
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
int main(void)
{
int **a = NULL;
int i;
int j;
if(!(a = calloc(5, sizeof(int *))))
{
printf("Error, could not allocate memory for a!\n");
exit(EXIT_FAILURE);
}
for(i = 0; i < 5; i++)
{
if(i != 2)
{
if(!(a[i] = calloc(3, sizeof(int))))
{
printf("Error, could not allocate memory for a[%d]!\n",i);
for(j = 0; j < i; j++)
{
free(a[j]);
}
free(a);
}
}
else
{
if(!(a[i] = calloc(MAX_INT * 1000, sizeof(int))))
{
printf("Error, could not allocate memory for a[%d]\n", i);
for(j = 0; j < i; j++)
{
free(a[j]);
}
free(a);
}
}
}
return 0;
}
Случай 2:
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
int main(void)
{
int **a = NULL;
int i;
int j;
if(!(a = calloc(5, sizeof(int *))))
{
printf("Error, could not allocate memory for a!\n");
exit(EXIT_FAILURE);
}
for(i = 0; i < 5; i++)
{
if(i != 2)
{
if(!(a[i] = calloc(3, sizeof(int))))
{
printf("Error, could not allocate memory for a[%d]!\n",i);
for(j = 0; j <= i; j++)
{
free(a[j]);
}
free(a);
}
}
else
{
if(!(a[i] = calloc(MAX_INT * 1000, sizeof(int))))
{
printf("Error, could not allocate memory for a[%d]\n", i);
for(j = 0; j <= i; j++)
{
free(a[j]);
}
free(a);
}
}
}
return 0;
}
Единственное различие между этими двумя случаями состоит в том, что в случае 1, когда что-то идет не так, выделяя память (у меня сознательно есть большое выделение, чтобы вызвать его сбой при i == 2), я зацикливаюсь от j = 0 до j