Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int i;
- int **Bord = NULL;
- Bord = malloc(N_MAX * sizeof(*Bord));
- if(Bord == NULL)
- {
- printf("Error while allocating memory to an array");
- free(Bord);
- return -1;
- }
- for(i = 0; i < N_MAX; i++)
- {
- printf("%dn", i);
- Bord[i] = malloc(N_MAX * sizeof(*(Bord[i])));
- if(Bord[i] == NULL)
- {
- printf("Error while allocating memory to an array");
- while(i != 0)
- {
- free(Bord[i]);
- i--;
- }
- free(Bord);
- return -1;
- }
- }
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- static void print_array(int **base);
- static void init_array(int **base);
- enum { N_MAX = 8 };
- static inline void free_array(int n_rows, int **array)
- {
- for (int i = 0; i < n_rows; i++)
- free(array[i]);
- free(array);
- }
- int main(void)
- {
- int i;
- int **Bord = NULL;
- srand(time(0)); // Not good, but better than nothing
- Bord = malloc(N_MAX * sizeof(*Bord));
- if (Bord == NULL)
- {
- fprintf(stderr, "Error while allocating %zu bytes of memoryn", N_MAX * sizeof(*Bord));
- return -1;
- }
- for (i = 0; i < N_MAX; i++)
- {
- Bord[i] = malloc(N_MAX * sizeof(*(Bord[i])));
- if (Bord[i] == NULL)
- {
- fprintf(stderr, "Error while allocating %zu bytes of memoryn", N_MAX * sizeof(*Bord[i]));
- free_array(i, Bord);
- return -1;
- }
- }
- init_array(Bord);
- print_array(Bord);
- free_array(N_MAX, Bord);
- return 0;
- }
- static void init_array(int **base)
- {
- for (int i = 0; i < N_MAX; i++)
- {
- for (int j = 0; j < N_MAX; j++)
- base[i][j] = (100 * (i + 1)) + (10 * (j + 1)) + rand() % 10;
- }
- }
- static void print_array(int **base)
- {
- for (int i = 0; i < N_MAX; i++)
- {
- printf("[%d]:", i);
- for (int j = 0; j < N_MAX; j++)
- printf(" %3d", base[i][j]);
- putchar('n');
- }
- }
- [0]: 115 121 137 142 159 166 175 181
- [1]: 211 224 239 248 253 265 277 283
- [2]: 316 320 337 349 357 364 376 380
- [3]: 419 428 439 448 451 469 476 484
- [4]: 511 527 534 544 558 569 578 585
- [5]: 616 623 631 647 650 664 671 688
- [6]: 710 729 739 748 759 766 779 783
- [7]: 817 824 839 847 850 860 878 881
- // added missing `#include` statements
- #include <stdio.h>
- #include <stdlib.h>
- // added missing definition of N_MAX
- #define N_MAX 300
- int main( void ) // corrected signature for 'main'
- {
- //int i;
- // minimize the scope of variables
- int **Bord = NULL;
- //edited following line
- Bord = malloc(N_MAX * sizeof(*Bord));
- if(Bord == NULL)
- {
- //printf("Error while allocating memory to an array");
- // error messages should be output to 'stderr', not 'stdout'
- // suggest:
- perror( "malloc failed" );
- //free(Bord); DONT do this, the allocation was not successful
- return -1;
- }
- // implied else, malloc was successful
- for( int i = 0; i < N_MAX; i++)
- {
- printf("%dn", i);
- Bord[i] = malloc(N_MAX * sizeof(*(Bord[i]));
- if(Bord[i] == NULL)
- {
- //printf("Error while allocating memory to an array");
- // error messages should be output to 'stderr', not 'stdout'
- // suggest:
- perror( "malloc failed" );
- //while(i != 0)
- // this will not free the first sub allocation
- // suggest
- i--;
- while( i >= 0 )
- {
- free(Bord[i]);
- i--;
- }
- free(Bord);
- return -1;
- }
- }
- // the following will result in a memory leak
- // because all those memory allocations have not been passed to 'free()'
- return 0;
- }
Add Comment
Please, Sign In to add comment