Guest User

Untitled

a guest
Dec 15th, 2018
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. int i;
  2. int **Bord = NULL;
  3.  
  4. Bord = malloc(N_MAX * sizeof(*Bord));
  5. if(Bord == NULL)
  6. {
  7. printf("Error while allocating memory to an array");
  8. free(Bord);
  9. return -1;
  10. }
  11. for(i = 0; i < N_MAX; i++)
  12. {
  13. printf("%dn", i);
  14. Bord[i] = malloc(N_MAX * sizeof(*(Bord[i])));
  15. if(Bord[i] == NULL)
  16. {
  17. printf("Error while allocating memory to an array");
  18. while(i != 0)
  19. {
  20. free(Bord[i]);
  21. i--;
  22. }
  23. free(Bord);
  24. return -1;
  25. }
  26. }
  27.  
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <time.h>
  31.  
  32. static void print_array(int **base);
  33. static void init_array(int **base);
  34.  
  35. enum { N_MAX = 8 };
  36.  
  37. static inline void free_array(int n_rows, int **array)
  38. {
  39. for (int i = 0; i < n_rows; i++)
  40. free(array[i]);
  41. free(array);
  42. }
  43.  
  44. int main(void)
  45. {
  46. int i;
  47. int **Bord = NULL;
  48.  
  49. srand(time(0)); // Not good, but better than nothing
  50.  
  51. Bord = malloc(N_MAX * sizeof(*Bord));
  52. if (Bord == NULL)
  53. {
  54. fprintf(stderr, "Error while allocating %zu bytes of memoryn", N_MAX * sizeof(*Bord));
  55. return -1;
  56. }
  57. for (i = 0; i < N_MAX; i++)
  58. {
  59. Bord[i] = malloc(N_MAX * sizeof(*(Bord[i])));
  60. if (Bord[i] == NULL)
  61. {
  62. fprintf(stderr, "Error while allocating %zu bytes of memoryn", N_MAX * sizeof(*Bord[i]));
  63. free_array(i, Bord);
  64. return -1;
  65. }
  66. }
  67.  
  68. init_array(Bord);
  69. print_array(Bord);
  70. free_array(N_MAX, Bord);
  71. return 0;
  72. }
  73.  
  74. static void init_array(int **base)
  75. {
  76. for (int i = 0; i < N_MAX; i++)
  77. {
  78. for (int j = 0; j < N_MAX; j++)
  79. base[i][j] = (100 * (i + 1)) + (10 * (j + 1)) + rand() % 10;
  80. }
  81. }
  82.  
  83. static void print_array(int **base)
  84. {
  85. for (int i = 0; i < N_MAX; i++)
  86. {
  87. printf("[%d]:", i);
  88. for (int j = 0; j < N_MAX; j++)
  89. printf(" %3d", base[i][j]);
  90. putchar('n');
  91. }
  92. }
  93.  
  94. [0]: 115 121 137 142 159 166 175 181
  95. [1]: 211 224 239 248 253 265 277 283
  96. [2]: 316 320 337 349 357 364 376 380
  97. [3]: 419 428 439 448 451 469 476 484
  98. [4]: 511 527 534 544 558 569 578 585
  99. [5]: 616 623 631 647 650 664 671 688
  100. [6]: 710 729 739 748 759 766 779 783
  101. [7]: 817 824 839 847 850 860 878 881
  102.  
  103. // added missing `#include` statements
  104. #include <stdio.h>
  105. #include <stdlib.h>
  106.  
  107. // added missing definition of N_MAX
  108. #define N_MAX 300
  109.  
  110. int main( void ) // corrected signature for 'main'
  111. {
  112. //int i;
  113. // minimize the scope of variables
  114.  
  115. int **Bord = NULL;
  116.  
  117. //edited following line
  118. Bord = malloc(N_MAX * sizeof(*Bord));
  119. if(Bord == NULL)
  120. {
  121. //printf("Error while allocating memory to an array");
  122. // error messages should be output to 'stderr', not 'stdout'
  123. // suggest:
  124. perror( "malloc failed" );
  125. //free(Bord); DONT do this, the allocation was not successful
  126. return -1;
  127. }
  128.  
  129. // implied else, malloc was successful
  130.  
  131. for( int i = 0; i < N_MAX; i++)
  132. {
  133. printf("%dn", i);
  134.  
  135. Bord[i] = malloc(N_MAX * sizeof(*(Bord[i]));
  136. if(Bord[i] == NULL)
  137. {
  138. //printf("Error while allocating memory to an array");
  139. // error messages should be output to 'stderr', not 'stdout'
  140. // suggest:
  141. perror( "malloc failed" );
  142.  
  143. //while(i != 0)
  144. // this will not free the first sub allocation
  145. // suggest
  146. i--;
  147. while( i >= 0 )
  148. {
  149. free(Bord[i]);
  150. i--;
  151. }
  152.  
  153. free(Bord);
  154. return -1;
  155. }
  156. }
  157.  
  158. // the following will result in a memory leak
  159. // because all those memory allocations have not been passed to 'free()'
  160. return 0;
  161. }
Add Comment
Please, Sign In to add comment