Are there any differences between these 2 lines ?
Let's take:
memmove(&(Arr[i]), &(Arr[i+1]), (N-1-i)*sizeof(double*));
Basically what it does can be represented by copying byte-by-byte the specified amount of bytes from one pointer to another. So it can be written in this I hope correct code:
for (size_t j = 0; j < (N-1-i)*sizeof(double*); ++j) {
*( ((char*)&Arr[i]) + j ) = *( ((char *)(&Arr[i + 1]) + j );
Puff, it makes a big technical difference, but let's remove sizeof(double*)
and the casts:
for (size_t j = 0; j < N - 1 - i; ++j)
*(&Arr[i] + j) = *(&Arr[i + 1] + j);
The &Arr[i]
is equal to &*(Arr + i)
which is equal to Arr + i
, so:
for (size_t j = 0; j < N - 1 - i; ++j) {
*(Arr + i + j) = *(Arr + i + j + 1);
The *(a + i)
is equal to a[i]
, so we can:
for (size_t j = 0; j < N - 1 - i; ++j) {
Arr[i + j] = Arr[i + j + 1];
As i + j
is on both sides and j
starts from 0
, we could just start from i
:
for (size_t j = i; j < N - 1; ++j) {
Arr[j] = Arr[j + 1];
Now this copies the pointers values(!) from Arr[j + 1]
into Arr[j]
. This does something very different then copying the values that are pointed to by the pointers:
for(int j = i; j < N - 1; j++)
for(int k = 0; k < M; k++)
Arr[j][k] = Arr[j + 1][k];
On the other hand, the above code could be translated to:
for(int j = i; j < N - 1; j++)
memcpy(Arr[j], Arr[j + 1], M * sizeof(double));
//^^^^^^ or memove, but I guess not
Theses arrays are full of values
Your initialization routine that you have showed has this loop:
for(int j = 0; j < N-1; j++)
Arr[j] = malloc(M*sizeof(double))
Being sane and assuming N > 1
, then Arr[N - 1]
is left uninitialized. So, accessing in the above loops Arr[j + 1]
for the last loop when j = N - 2
is accessing uninitialized value. And the loop for(int k = 0; k < M; k++)
Arr[j][k] = Arr[j + 1][k];
dereferences uninitialized pointer in Arr[j + 1][k]
and is undefined behavior.