Merge sort stops running - c

So my problem is, when I try to run my merge sort algorithm, it writes the random array on screen but when it tries to write the sorted array, the program stops working. I've been trying find my error but no hope so far. Appreciate any help.
Here is my code;
#include <stdio.h>
#include <stdlib.h>
#define LEN 300
#define INF 30000
void merge (int *A, int p, int q, int r){
int i, j, k, n1 , n2 , *L, *R;
n1 = q - p + 1;
n2 = r - q;
L = (int *) malloc (n1* sizeof (int ) + 1);
R = (int *) malloc (n2* sizeof (int )) ;
for (i = 0; i < n1; i++) {
L[i] = A[p + i];
}
for (j = 0; j < n2; j++) {
R[j] = A[q + 1 + j];
}
i = j = 0;
L[n1] = R[n2] = INF ;
for (k = p; k < r; k++) {
if(L[i] <= R[j]){
A[k] = L[i];
i ++;
}
else{
A[k] = R[j];
j ++;
}
}
free(L);
free(R);
}
void mergesort (int *A, int p, int r){
int q;
if(r > (p + 1)){
q = (p + r)/2;
mergesort(A, p, q);
mergesort(A, q + 1, r);
merge(A, p, q, r);
}
}
void sorting_merge(int *A, int n){
mergesort (A, 0, LEN);
}
int main (){
int i, *n;
n = malloc ( sizeof (int )*LEN );
srand (666) ;
for (i = 0; i < LEN ; i++) {
n[i] = rand() % 1000;
printf ("%d ", n[i]);
}
printf ("\n");
sorting_merge(n, LEN);
for (i = 0; i < LEN ; i++) {
printf ("%d ", n[i]);
}
printf ("\n");
free (n);
system("PAUSE");
return 1;
}

change to
L = (int *) malloc((n1+1)* sizeof(int));
R = (int *) malloc((n2+1)* sizeof(int));
for (k = p; k <= r; k++) {//include r
if(r > p){
void sorting_merge(int *A, int n){//call sorting_merge(n, LEN); note LEN is out range
mergesort (A, 0, n-1);
}

L[n1] = R[n2] = INF ; accesses beyond the allocated memory. Your allocation should read
L = malloc((n1 + 1) * sizeof (int));
R = malloc((n2 + 1) * sizeof (int));
if you really want to access index n1 and n2.
I haven't checked anything else, that was the first thing that sprang in my eyes.

Related

Implementing a count sort is crashing my program every time, cannot spot malloc/free error

I've been stuck on this for a while and absolutely cannot find a solution to my problem. I following along Sedgewick's algorithms in C to implement a count sort, but for some reason my b array is not reading the values of my input array correctly, and also crashes the program when trying to free. Any help will be appreciated.
void count_sort(int* a, int l, int r, int M)
{
int i, j;
int* cnt = (int*)malloc(M * sizeof(int));
if (!cnt) {
printf("Returned NULL PTR for cnt\n");
exit(1);
}
int* b = (int*)malloc((r + 1) * sizeof(int));
if (!b) {
printf("Returned NULL PTR for b\n");
exit(1);
}
printf("\n\n\n");
for (j = 0; j < M; ++j)
cnt[j] = 0;
for (i = l; i <= r; ++i)
cnt[a[i]]++;
for (i = 1; i < M; ++i)
cnt[i] += cnt[i - 1];
/*
free(b);
free(cnt);
printf("Able to free here\n");
exit(0);
*/
for (i = l; i <= r; ++i) {
b[cnt[a[i]]] = a[i];
printf("%d\n", b[cnt[a[i]]]);
++cnt[a[i]];
}
/*
free(b);
free(cnt);
printf("Able to free here\n");
exit(0);
*/
for (i = l; i <= r; ++i)
a[i] = b[i - l];
free(cnt);
free(b);
}
int is_sort(int* a, int N)
{
int i;
for (i = 0; i < N - 1; ++i) {
if (a[i + 1] < a[i]) {
printf("%d %d\t%d %d\n", i, a[i], i + 1, a[i + 1]);
return 0;
}
}
return 1;
}
int main(int argc, char** argv)
{
srand(time(NULL));
rand();
int N = atoi(argv[1]);
int M = atoi(argv[2]);
int* arr = (int*)malloc(N * sizeof(int));
int i;
for (i = 0; i < N; ++i) {
arr[i] = ((int)(1000 * (1.0 * rand() / RAND_MAX))) % M;
printf("%d\n", arr[i]);
}
count_sort(arr, 0, N - 1, M);
if (is_sort(arr, N))
printf("sorted\n");
else
printf("not sorted");
free(arr);
return 0;
}
The issue lies in these lines:
for (i = l; i <=r; ++i) {
b[cnt[a[i]]] = a[i];
printf("%d\n", b[cnt[a[i]]]);
++cnt[a[i]];
}
You want to decrement cnt[a[i]], not increment and also you want to do it before the assignment b[cnt[a[i]]] = a[i];, not after.
With these modifications the code works correctly.

Writing Merge Sort Pseudo-Code Procedure in C

I have been going through Introduction to Algorithms, and have been trying to implement the MERGE-SORT algorithm in C programming language to gain a better understanding of it.
The book presents two pseudo-codes:
and
While I do understand the above procedures, I must be missing something during the implementation.
I must be missing something from the pseudo-code but cannot figure it out yet. Any suggestions as to why this is happening would be appreciated.
EDIT: Updated Code and Output
/* C program for Merge Sort */
#include<stdlib.h>
#include<stdio.h>
void MERGE(int [], int , int , int );
void printArray(int [], int );
void MERGE_SORT(int [], int , int );
int main(void)
{
int A[] = { 12, 11, 13, 5, 6, 7, 2, 9 };
int arr_size = sizeof(A) / sizeof(A[0]);
printf("Given array is \n");
printArray(A, arr_size);
MERGE_SORT(A, 0, arr_size); //Fixed: Index to start from zero
printf("\nSorted array is \n");
printArray(A, arr_size);
return 0;
}
void MERGE(int A[], int p, int q, int r)
{
int i = 0;
int j = 0;
int n1 = q - p + 1; //Computing length of sub-array 1
int n2 = r - q; //Computing length of sub-array 2
int *L = malloc((n1 + 2) * sizeof(*L + 1)); //Creating Left array
int *R = malloc((n2 + 2) * sizeof(*R + 1)); //Creating Right array
for (int i = 0; i <= n1; i++) { //Fixed: <=, i start from 0
L[i] = A[p + i - 1];
}
for (int j = 0; j <= n2; j++) { //Fixed: <=, i start from 0
R[j] = A[q + j];
}
L[n1 + 1] = 99; //Placing Sentinel at the end of array
R[n2 + 1] = 99;
i = 1;
j = 1;
/*Prior to the first iteration k = p, so the subarray is empty.
Both L[i] and R[j] are the smallest elements of their arrays and have not
been copied back to A*/
for (int k = p; k <= r; k++) { //Fixed: <=
if (L[i] <= R[j]) {
A[k] = L[i];
i++;
}
else { //Fixed: Assignment and not condition check for A[k]
A[k] = R[j];
j++;
}
}
free(L);
free(R);
}
void MERGE_SORT(int A[], int p, int r)
{
//During first iteration p = 1 & r = 8
if (p < r) {
int q = (p + r) / 2;
MERGE_SORT(A, p, q);
MERGE_SORT(A, q + 1, r);
MERGE(A, p, q, r);
}
}
/* Function to print an array */
void printArray(int Arr[], int size)
{
int i;
for (i = 0; i < size; i++)
printf("%d ", Arr[i]);
printf("\n");
}
Looked in the pseudo code and found out that some things have been mistakenly written wrong.
1. You need to be careful with the array index to start from 0 or 1
2. Merge last part in for loop is actually an assignment instead for conditional check.
Edit: Have updated the code to fix for the error Stack around the variable A was corrupted
Please find the corrected code here(Lookout for //Fixed for fixes)
/* C program for Merge Sort */
#include<stdlib.h>
#include<stdio.h>
void MERGE(A, p, q, r);
void printArray(Arr, size);
void MERGE_SORT(A, p, r);
int main(void)
{
int A[] = { 12, 11, 13, 5, 6, 7, 2, 9 };
int arr_size = sizeof(A) / sizeof(A[0]);
printf("Given array is \n");
printArray(A, arr_size);
MERGE_SORT(A, 0, arr_size - 1); //Fixed: Index to start from zero, arr_size - 1
printf("\nSorted array is \n");
printArray(A, arr_size);
return 0;
}
void MERGE(int A[], int p, int q, int r)
{
int i = 0;
int j = 0;
int n1 = q - p + 1; //Computing length of sub-array 1
int n2 = r - q; //Computing length of sub-array 2
int *L = malloc((n1+1) * sizeof(*L+1)); //Creating Left array
int *R = malloc((n2+1) * sizeof(*R+1)); //Creating Right array
for (int i = 0; i < n1; i++) { //Fixed: i start from 0
L[i] = A[p + i];
}
// int arr_size = sizeof(A) / sizeof(A[0]);
for (int j = 0; j < n2; j++) { //Fixed: j start from 0
R[j] = A[q + j + 1];
}
L[n1] = 99; //Placing Sentinel at the end of array
R[n2] = 99;
i = 0; //Fixed: i and j to start from 0
j = 0;
/*Prior to the first iteration k = p, so the subarray is empty.
Both L[i] and R[j] are the smallest elements of their arrays and have not
been copied back to A*/
for (int k = p; k <= r; k++) { //Fixed: <=
if (L[i] <= R[j]) {
A[k] = L[i];
i++;
}
else { //Fixed: Assignment and not condition check for A[k]
A[k] = R[j];
j++;
}
}
free(L);
free(R);
}
void MERGE_SORT(int A[], int p, int r)
{
//During first iteration p = 1 & r = 8
if (p < r) {
int q = (p + r) / 2;
MERGE_SORT(A, p, q);
MERGE_SORT(A, q + 1, r);
MERGE(A, p, q, r);
}
}
/* Function to print an array */
void printArray(int Arr[], int size)
{
int i;
for (i = 0; i < size; i++)
printf("%d ", Arr[i]);
printf("\n", size);
}
Hope it helps.
Revert for any doubts.
here are some changes i have done to your code `
#include<stdlib.h>
#include<stdio.h>
void MERGE(int *A,int p,int q,int r);
void printArray(int *Arr,int size);
void MERGE_SORT(int *A,int p,int r);
int main(void){
int A[] = { 12, 11, 13, 5, 6, 7, 2, 9 };
int arr_size = sizeof(A) / sizeof(A[0]);
printf("Given array is \n");
printArray(A, arr_size);
MERGE_SORT(A, 0, arr_size -1); // pass the indices of the array
printf("\nSorted array is \n");
printArray(A, arr_size);
return 0;
}
void MERGE(int A[], int p, int q, int r){
int i = 0;
int j = 0;
int k; //declair it here
int n1 = q - p + 1; //Computing length of sub-array 1
int n2 = r - q; //Computing length of sub-array 2
int *L = malloc((n1) * sizeof(*L+1)); //Creating Left array
int *R = malloc((n2) * sizeof(*R+1)); //Creating Right array
for (int i = 0; i < n1; i++) { //start coping from zero
L[i] = A[p + i];
}
for (int j = 0; j < n2; j++) {
R[j] = A[q +1 + j];
}
// L[n1] = 99; we won't be needing these as to mark the end we already know the size of arrays
// R[n2] = 99;
// i = 1;
// j = 1;
/*Prior to the first iteration k = p, so the subarray is empty.
Both L[i] and R[j] are the smallest elements of their arrays and have not
been copied back to A*/
for (k = p; k < r+1 && i < n1 && j<n2; k++) {
//i & j checks weather the array has completed or not
if (L[i] <= R[j]) {
A[k] = L[i];
i++;
}
else {
A[k]=R[j];
j++;
}
}
// when one of the array is empty u can copy the rest of the array with out compairing
while(i<n1)
A[k++]=L[i++];
while(j<n2)
A[k++]=R[j++];
free(L);
free(R);
}
void MERGE_SORT(int A[], int p, int r)
{
//During first iteration p = 1 & r = 8
if (p < r) {
int q = (p + r) / 2;
MERGE_SORT(A, p, q);
MERGE_SORT(A, q + 1, r);
MERGE(A, p, q, r);
}
}
/* Function to print an array */
void printArray(int Arr[], int size){
int i;
for (i = 0; i < size; i++)
printf("%d ", Arr[i]);
printf("\n");
}`
first of all you were not passing the right parameters to the function.
then the concept of using infinity to indicate is not good as one can want to sort bigger number than that in that case you would have to increase infinity an alternative approach is given above.
Mean while i also solved the problem with your code here it was again with the array index were not rightly used check it out now its working:`
#include<stdlib.h>
#include<stdio.h>
void MERGE(A, p, q, r);
void printArray(Arr, size);
void MERGE_SORT(A, p, r);
int main(void)
{
int A[] = { 12, 11, 13, 5, 6, 7, 2, 9 };
int arr_size = sizeof(A) / sizeof(A[0]);
printf("Given array is \n");
printArray(A, arr_size);
MERGE_SORT(A, 1, arr_size);
printf("\nSorted array is \n");
printArray(A, arr_size);
return 0;
}
void MERGE(int A[], int p, int q, int r)
{
int i = 0;
int j = 0;
int n1 = q - p + 1; //Computing length of sub-array 1
int n2 = r - q; //Computing length of sub-array 2
int *L = malloc((n1+1) * sizeof(*L+1)); //Creating Left array
int *R = malloc((n2+1) * sizeof(*R+1)); //Creating Right array
for (int i = 1; i < n1; i++) {
L[i] = A[p + i - 1];
}
for (int j = 1; j < n2; j++) {
R[j] = A[q + j];
}
L[n1] = 99; //Placing Sentinel at the end of array
R[n2] = 99;
i = 1;
j = 1;
/*Prior to the first iteration k = p, so the subarray is empty.
Both L[i] and R[j] are the smallest elements of their arrays and have not
been copied back to A*/
for (int k = p; k < r; k++) {
if (L[i] <= R[j]) {
A[k] = L[i];
i++;
}
else if (A[k] == L[i])
j++;
}
free(L);
free(R);
}
void MERGE_SORT(int A[], int p, int r)
{
//During first iteration p = 1 & r = 8
if (p < r) {
int q = (p + r) / 2;
MERGE_SORT(A, p, q);
MERGE_SORT(A, q + 1, r);
MERGE(A, p, q, r);
}
}
/* Function to print an array */
void printArray(int Arr[], int size)
{
int i;
for (i = 0; i < size; i++)
printf("%d ", Arr[i]);
printf("\n");
}

How does the merge() in merge_sort() work?

So I was looking at the C example of merge sort on Rosetta Code and I'm a bit confused about how the merge() function works. I think it is the syntax they use that throws me off with the colons and ?'s.
void merge (int *a, int n, int m) {
int i, j, k;
int *x = malloc(n * sizeof (int));
for (i = 0, j = m, k = 0; k < n; k++) {
x[k] = j == n ? a[i++]
: i == m ? a[j++]
: a[j] < a[i] ? a[j++]
: a[i++];
}
for (i = 0; i < n; i++) {
a[i] = x[i];
}
free(x);
}
void merge_sort (int *a, int n) {
if (n < 2)
return;
int m = n / 2;
merge_sort(a, m);
merge_sort(a + m, n - m);
merge(a, n, m);
}
What exactly is happening in the for loop of the merge() function? Can someone explain it please?
Read the comments:
void merge (int *a, int n, int m) {
int i, j, k;
// inefficient: allocating a temporary array with malloc
// once per merge phase!
int *x = malloc(n * sizeof (int));
// merging left and right halfs of a into temporary array x
for (i = 0, j = m, k = 0; k < n; k++) {
x[k] = j == n ? a[i++] // right half exhausted, take from left
: i == m ? a[j++] // left half exhausted, take from right
: a[j] < a[i] ? a[j++] // right element smaller, take that
: a[i++]; // otherwise take left element
}
// copy temporary array back to original array.
for (i = 0; i < n; i++) {
a[i] = x[i];
}
free(x); // free temporary array
}
void merge_sort (int *a, int n) {
if (n < 2)
return;
int m = n / 2;
// inefficient: should not recurse if n == 2
// recurse to sort left half
merge_sort(a, m);
// recurse to sort right half
merge_sort(a + m, n - m);
// merge left half and right half in place (via temp array)
merge(a, n, m);
}
A simpler and more efficient version of the merge function, using only half as much temporary space:
static void merge(int *a, int n, int m) {
int i, j, k;
int *x = malloc(m * sizeof (int));
// copy left half to temporary array
for (i = 0; i < m; i++) {
x[i] = a[i];
}
// merge left and right half
for (i = 0, j = m, k = 0; i < m && j < n; k++) {
a[k] = a[j] < x[i] ? a[j++] : x[i++];
}
// finish copying left half
while (i < m) {
a[k++] = x[i++];
}
}
A faster version of merge_sort involves allocating a temporary array x of size n * sizeof(*a) and passing it to a recursive function merge_sort1 that calls merge with as extra parameter as well. The logic in merge is also improved here with half as many comparisons on i and j:
static void merge(int *a, int n, int m, int *x) {
int i, j, k;
for (i = 0; i < m; i++) {
x[i] = a[i];
}
for (i = 0, j = m, k = 0;;) {
if (a[j] < x[i]) {
a[k++] = a[j++];
if (j >= n) break;
} else {
a[k++] = x[i++];
if (i >= m) return;
}
}
while (i < m) {
a[k++] = x[i++];
}
}
static void merge_sort1(int *a, int n, int *x) {
if (n >= 2) {
int m = n / 2;
if (n > 2) {
merge_sort1(a, m, x);
merge_sort1(a + m, n - m, x);
}
merge(a, n, m, x);
}
}
void merge_sort(int *a, int n) {
if (n < 2)
return;
int *x = malloc(n / 2 * sizeof (int));
merge_sort1(a, n, x);
free(x);
}

Debugging a merge sort implementation in c

I am debugging a c implementation of merge sort. For some reason, the value of r (in merge) is nan. Yet, n2 (defined as (int) (r - q)) is well defined. Why is r == nan ?
#include <stdio.h>
#include <limits.h>
#include <math.h>
void merge(int *A,float p,float q,float r)
{
float n = r - p + 1; /* number of elements in array A */
int n1 = (int) (q - p + 1);
int n2 = (int) (r - q);
printf("n2 = %d\n",n2);
int i, j;
float k;
int *L, *R;
for (i = 1; i <= n1; i++){
L[i] = A[(int) (p + i - 1)];
}
for (j = 1; j < n2; j++){
R[j] = A[(int) (q + j)];
}
L[n1+1] = INT_MAX;
R[n2+1] = INT_MAX;
i = 1;
j = 1;
printf("p = %f\n",p);
printf("r = %f\n",r);
for (k = p; k <= r; k++){
printf("k=%f\n",k);
if (L[i] <= R[j]){
A[(int) k] = L[i];
i += 1;
}
else{
A[(int) k] = R[j];
j += 1;
}
}
}
void merge_sort(int *A, float p, float r)
{
int i;
/* for (i=0; i<=r-p;i++){
printf("%d\n",A[i]);
}*/
float q;
if (p < r){
/* printf("merge_sort p = %d\n",p);
printf("merge_sort r = %d\n",r);*/
q = floor((p + r)/2);
/* printf("q = %f\n",q);*/
merge_sort(A, p, q);
merge_sort(A, q+1, r);
/* printf("done\n");*/
merge(A, p, q, r);
}
}
int main()
{
int x, c, *array;
int n;
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d elements\n",n);
for (c = 0; c < n; c++){
scanf("%d", &array[c]);
}
merge_sort(array, 0, n-1);
for (c = 0; c < n; c++){
printf("%d\n", array[c]);
}
}
int *L, *R;
for (i = 1; i <= n1; i++){
L[i] = A[(int) (p + i - 1)];
}
L and R are used uninitialized
Same for array:
int x, c, *array;
...
for (c = 0; c < n; c++){
scanf("%d", &array[c]);

assigning all combinations of a variable number of variable objects

I'm having difficulty with this recursion problem. I thought I had an answer to it but it doesn't work, and I simply don't know why, so I thought I would ask the experts. Please go easy on me, I took C programming more than 15 years ago and even then I was maybe a B student. I don't know C++ or Java.
The purpose is to generate all of the possible combinations of integers from 0 to (n[j]-1), where j can be an arbitrary integer. Right now it is hard-coded as 2, but I would like it to be able to take any value eventually.
Anyway, here is my code. Thanks in advance for your help.
Edit:
For the code below, I define 2 sequences, with the 0th sequence having a length of 2 (0,1) and the 1st sequence having a length of 3 (0, 1, 2).
The desired output is as follows:
p[0][0] = 0
p[0][1] = 0
p[1][0] = 0
p[1][1] = 1
p[2][0] = 0
p[2][1] = 2
p[3][0] = 1
p[3][1] = 0
p[4][0] = 1
p[4][1] = 1
p[5][0] = 1
p[5][1] = 2
That is,
the 0th combination contributes 0 from sequence 0 and 0 from sequence 1
the 1st combination contributes 0 from sequence 0 and 1 from sequence 1
the 2nd combination contributes 0 from sequence 0 and 2 from sequence 1
the 3rd combination contributes 1 from sequence 0 and 0 from sequence 1
the 4th combination contributes 1 from sequence 0 and 1 from sequence 1
the 5th combination contributes 1 from sequence 0 and 2 from sequence 1
I hope this makes it clearer what I'm trying to do!
#include <stdio.h>
#include <stdlib.h>
int recurse (int **p, int *n, int nclass, int classcount, int combcount);
int recurse (int **p, int *n, int nclass, int classcount, int combcount)
{
int k, j, kmax;
kmax = n[classcount];
j = classcount;
if (j == nclass) {
return (combcount+1);
}
for (k = 0; k < kmax; k++) {
p[combcount][j] = k;
combcount = recurse (p, n, nclass, j+1, combcount);
}
}
int main (void)
{
int **p, n[2], i, j;
n[0] = 2;
n[1] = 3;
p = (int **) malloc ((n[0]*n[1]) * sizeof (int *));
for (i = 0; i < (n[0]*n[1]); i++) {
p[i] = (int *) malloc (2 * sizeof (int));
for (j = 0; j < 2; j++)
p[i][j] = -1;
}
/* p[i][j] = the value of the integer in the ith combination
arising from the sequence 0...n[j]-1 */
recurse (p, n, 2, 0, 0);
for (i = 0; i < (n[0]*n[1]); i++)
for (j = 0; j < 2; j++)
printf ("%d %d: %d\n", i, j, p[i][j]);
for (i = 0; i < (n[0]*n[1]); i++)
free (p[i]);
free (p);
return (0);
}
#include <stdio.h>
#include <stdlib.h>
void recurse(int *n, int *accum, int **p, int N, int k) {
static int comb;
int i, j;
if (k == 0)
comb = 0;
if (k == N) {
for (i = 0; i < N; ++i)
p[comb][i] = accum[i];
comb++;
}
else
for (i = 0; i < n[k]; ++i) {
accum[k] = i;
recurse(n, accum, p, N, k+1);
}
}
int main(void) {
const int N = 2;
int n[N];
int accum[N];
int **p;
int mult;
int i, j;
n[0] = 2;
n[1] = 3;
for (mult = 1, i = 0; i < N; mult *= n[i], ++i);
p = malloc(mult*sizeof(int*));
for (i = 0; i < mult; i++)
p[i] = malloc(N*sizeof(int));
recurse(n, accum, p, N, 0);
for (i = 0; i < mult; ++i)
for (j = 0; j < N; ++j)
printf("p[%d][%d] = %d\n", i, j, p[i][j]);
for (i = 0; i < mult; i++)
free(p[i]);
free(p);
}
#include <stdio.h>
#include <stdlib.h>
int recurse (int **p, int *n, int nclass, int classcount, int p_size){
int i, j, jmax, k, kmax;
if (classcount == nclass) return 1;
i = 0;
kmax = n[classcount];
while(i < p_size){
for (k = 0; k < kmax; ++k){
jmax = recurse (p, n, nclass, classcount+1, p_size);
for(j = 0;j < jmax; ++j)
p[i++][classcount] = k;
}
}
return kmax*jmax;
}
int main (void){
int **p, n[2], i, j;
int sizeAll, sizeN;
n[0] = 2;
n[1] = 3;
sizeAll = n[0]*n[1];
sizeN = sizeof(n)/sizeof(int);
p = (int **) malloc (sizeAll * sizeof (int *));
for (i = 0; i < sizeAll; ++i) {
p[i] = (int *) malloc (sizeN * sizeof (int));
for (j = 0; j < sizeN; ++j)
p[i][j] = -1;
}
recurse (p, n, sizeN, 0, sizeAll);
for (i = 0; i < sizeAll; ++i)
for (j = 0; j < sizeN; ++j)
printf ("%d %d: %d\n", i, j, p[i][j]);
for (i = 0; i < sizeAll; ++i)
free (p[i]);
free (p);
return (0);
}

Resources