I work with a large two-dimensional array.
In the case when it is 30 x 200, everything is considered. With large volumes, the program crashes … I was advised to use MemSet (), but something does not particularly help …
Maybe you have any ideas?
# include & lt; stdio.h & gt;
#Include & lt; stdlib.h & gt;
#Include & lt; math.h & gt;
#Include & lt; String.h & gt;
Double ** B;
Int Main (int argc, char * argv [])
{
int n, n_a;
N = 32;
n_a = 350; / * If you do n_a = 200, then everything works * /
B = (Double **) Malloc (N * Sizeof (Double *));
for (i = 0; i & lt; n; i ++)
{
B [i] = (Double *) Malloc (N_A * SizeOF (Double));
Memset (B [i], 0, N_a * Sizeof (Double));
}
FREE (B);
Return 0;
}
Answer 1, Authority 100%
In general, the code is correct, but you need to add a check when the memory is allocated: this is necessary, because the memory may simply miss, and in this case the program will fall, because it will be recorded in non-existent memory. That is, whenever Malloc calls, it is necessary to verify that the return value is not equal to NULL.
B = (Double **) Malloc (N * Sizeof (Double *));
/ * Check that the memory is allocated * /
If (b! = NULL)
{
for (i = 0; i & lt; n; i ++)
{
B [i] = (Double *) Malloc (N_A * SizeOF (Double));
/ * Check that the memory is allocated * /
IF (B [i]! = NULL)
{
Memset (B [i], 0, N_a * Sizeof (Double));
FREE (B [i]);
}
}
FREE (B);
}
In addition, you must not forget to release the memory allocated by Malloc inside the cycle, otherwise there will be leaks.
Answer 2, Authority 25%
That’s all you can make one line
double * b;
B = (Double *) Calloc (N * N_A, SizeOF (Double));
FREE (B);
Instead of calling a heap of functions, you call one that immediately highlights the memory of the entire data array and reset it.
data access is carried out by the
formula
double a;
a = * (b + i * n_a + j);
where i – line number, j – column number
Answer 3
With large volumes, the program crashes …
Local buffer (more precisely, the size of the stack frame) should not have the size of a larger one page. Otherwise, it will take place beyond the limits of the stack watchdog, it will not be expanded and an exception will arise.