程式扎記: [ C 文章收集 ] 2D arrays - dynamic allocation and freeing

標籤

2013年5月2日 星期四

[ C 文章收集 ] 2D arrays - dynamic allocation and freeing

來源自 這裡 
Preface: 
If the size is known at compile time; you don't need to allocate anything; let the compiler take care of it (no malloc(), no free()
  1.  int a[5][5];  
  2. ...  
  3. ...  
  4.   a[i][j] = xxx;  
  5. ...  
  6. ...  
2D array allocation: 
Now, if you want a 2D array of int, and the dimensions are not known at compile time, here's a way to do it. Suppose we want an array[size_x][size_y] of int, wheresize_x and size_y are variables. The "array" name will be a "pointer to a pointer to an int". 
1. Allocate memory for size_x pointers to int. The address returned by malloc() will be the "array".
2. For each of the pointers, allocate memory for size_y ints.
Then address the individual elements with array[][] notation. When you don't need it any more:
3. Free the memory allocated for the ints.
4. Free the memory allocated for the pointers.

Example: 
Here's an example (I have given size_x and size_y specific values for purposes of illustration, but these could be obtained from user input or from other program calculations, or whatever.
  1. /* illustration of dynamically allocated 2D array */  
  2. #include   
  3. #include   
  4.   
  5. int main()  
  6. {  
  7.   int i; /* general purpose variable used for loop index */  
  8.   int j; /* general purpose variable used for loop index */  
  9.   
  10.   int **a;     /* this is the array name */  
  11.   int size_x; /* this variable will be used for the first  dimension */  
  12.   int size_y; /* this variable will be used for the second dimension */  
  13.   
  14.   /* suppose we want an array of int: a[5][3] */  
  15.   size_x = 5;  
  16.   size_y = 3;  
  17.   
  18.   /*  allocate storage for an array of pointers */  
  19.   a = malloc(size_x * sizeof(int *));  
  20.   
  21.   /* for each pointer, allocate storage for an array of ints */  
  22.   for (i = 0; i < size_x; i++) {  
  23.     a[i] = malloc(size_y * sizeof(int));  
  24.   }  
  25.   
  26.   /* just for kicks, show the addresses (note: not all sequential) */  
  27.   /* assign an arbitrary value to each element        */  
  28.   for (i = 0; i < size_x; i++) {  
  29.     for (j = 0; j < size_y; j++) {  
  30.       printf("&a[%d][%d] = %p\n", i, j, &a[i][j]); /* show the addresses */  
  31.       a[i][j] = i * size_y + j; /* just some unique number for each element */  
  32.     }  
  33.     printf ("\n");  
  34.   }  
  35.   
  36.   /* now show the contents that were assigned */  
  37.   for (i = 0; i < size_x; i++) {  
  38.     for (j = 0; j < size_y; j++) {  
  39.       printf("a[%d][%d] = %2d\n", i, j, a[i][j]);  
  40.     }  
  41.     printf ("\n");  
  42.   }  
  43.   
  44.   /* now for each pointer, free its array of ints */  
  45.   for (i = 0; i < size_y; i++) {  
  46.     free(a[i]);  
  47.   }  
  48.   /* now free the array of pointers */  
  49.   free(a);  
  50.   
  51.   return 0;  
  52. }  
Remember: always free() everything that you got from malloc(), but never anything else. 
C++ Programmers: Do the same, but use new and delete instead of malloc() and free().

沒有留言:

張貼留言

網誌存檔

關於我自己

我的相片
Where there is a will, there is a way!