3

次のような2次元配列を作成しました

     int i,j,lx,ly;// lx,ly are the row and column respectively
     double** a;

     a=(double**) malloc((lx+2)*sizeof(double));

     a[0]= (double*) malloc((lx+2)*(ly+2)* sizeof(double));

     assert(a[0]); 

     for(i=1;i<lx+2;i++)
     {
       a[i]=a[i-1]+i*(ly+2);
     }

// 以下のように、この配列のすべての要素に値 0 を割り当てます

    for(i=0;i<(lx+2)*(ly+2);i++)
    {
      a[i]=0;
    } 

// 以下のすべての要素を出力します

      for(i=0;i<(lx+2)*(ly+2);i++)
      {
         printf("position %d values %d\n",i,a[i]);
      } 

// 出力を見ると、ある特定の位置 13 にジャンク値が表示されます。それを理解することはできません.. また、7 番目の列、行 0 および 5 にアクセスするために、たとえば行と列にアクセスする方法を教えてください。私のコードに示されているように、lxに関して行6列目、ly

4

4 に答える 4

5

あなたのアプローチは間違いなく正しい一般的な方向に向かっています。

私はこう思います:

a=(double**) malloc((lx+2)*sizeof(double));

通常は次のようになります。

a = malloc(lx * sizeof(double *));

そして、隣接要件がない場合、次のようになります。

a[0]= (double*) malloc((lx+2)*(ly+2)* sizeof(double));

ほとんどのプログラムでは次のようになります。

a[0] = malloc(ly * sizeof(double));

そして最後に、その最後の行は、それぞれa[i]に独自の malloc されたスペースを割り当てるループ内にある必要があります。

ただし、連続したメモリは作成されません。そのためには、大きな割り当てを行い、それを行ベクトルに分割する必要があります。したがって、ループ内の 2 番目の malloc の代わりに、おそらく次のようになります。

double *t = malloc(lx * ly * sizeof(double));
for (i = 0; i < lx; ++i)
    a[i] = t + i * ly;

すべてを一緒に入れて:

#include <stdio.h>
#include <stdlib.h>

void arrayDemo(int lx, int ly)
{
  double **a;
  int i, j;

  a = malloc(lx * sizeof(double *));
  double *t = malloc(lx * ly * sizeof(double));
  for(i = 0; i < lx; ++i)
    a[i] = t + i * ly;

  for(i = 0; i < lx; ++i)
    for(j = 0; j < ly; ++j)
      a[i][j] = i*100 + j;
  for(i = 0; i < lx; ++i) {
    for(j = 0; j < ly; ++j)
      printf(" %4.0f", a[i][j]);
    printf("\n");
  }
}

int main(int ac, char **av)
{
  arrayDemo(atoi(av[1]), atoi(av[2]));
  return 0;
}

$ cc -Wall all.c
$ ./a.out 4 7
    0    1    2    3    4    5    6
  100  101  102  103  104  105  106
  200  201  202  203  204  205  206
  300  301  302  303  304  305  306
于 2010-02-14T01:27:29.690 に答える
2

このコードは、10 x 5 の連続するメモリ ブロックを割り当て、それを double のインクリメントで初期化し、x と y でインデックス付けされた値を出力します。

#include "2d.h"

int main(void){

    unsigned int x,y;
    const unsigned int width = 10;
    const unsigned int height = 5;

    //we need an index into the x of the array
    double * index[width];

    //need the memory to store the doubles
    unsigned int memorySizeInDoubles = width * height;
    double * memory = malloc(memorySizeInDoubles * sizeof(double));

    //initialize the memory with incrementing values
    for(x = 0; x < memorySizeInDoubles; ++x){
        memory[x] = (double) x;
    }

    //initialize the index into the memory
    for(x = 0; x < width; ++x){
        index[x] = memory + height * x;
    }

    //print out how we did
    for(x = 0; x < width; ++x){
        for(y = 0; y < height; ++y){
           printf("[%u, %u]: Value = %f\n", x, y, index[x][y]);
        }
    }

    free(memory);

    return 0;
}

2d.h ファイルには次の行が含まれている必要があります。

#include <stdio.h>
#include <stdlib.h>

int main(void);

注:作成されるメモリは、一部の定義に対してのみ連続しています。メモリは論理的に連続していますが、必ずしも物理的に連続しているわけではありません。たとえば、このメモリがデバイス ドライバ用である場合、malloc は機能しません。

于 2010-02-14T02:28:55.720 に答える
0

単一次元配列を作成するか

double my_array = malloc(sizeof(double) * size_x * sizeof(double) * size_y);

あなたがアクセスする

(位置を取得 x=28, y=12)

my_array[12 * size_x + 28];

または、あなたと同じように2次元配列を作成しますが、次の方法でアクセスします

double **my_array = (double**) malloc(15 * sizeof(double));

for(int i = 0 ; i < 25; i++)
   {
   my_array[i] = (double*) malloc(30 * sizeof(double));
   for (int j = 0 ; j < 12; j++)
      {
      my_array[i][j] = 1.2;
      }
   }

double my_double = my_array[12][28];
于 2010-02-14T01:17:05.253 に答える
0

C では、連続したメモリのチャンクを 1 つ持つには、1 つ必要ですmalloc()。または、静的に割り当てられた配列が必要です。動的メモリが必要なので、malloc(). すべてが連続している必要があるため、必要な呼び出しは1 回だけです。

では、通話はどのように表示されるのでしょうか。私があなたを正しく理解していれば、それぞれが size のlxtimesly値が必要なので、バイトを割り当てるsizeof(double)必要があります。lx*ly*sizeof(double)

malloc()余談: 私は次のように自分の呼び出しを書くことを好みます:

#include <stdlib.h> /* for malloc's prototype */
T *pt; /* for any type T */
size_t n; /* need n objects of type T */

pt = malloc(n * sizeof *pt);

sizeofwithsizeof *ptの代わりに使用すると、型が変わった場合に呼び出しを変更する必要がないsizeof(T)という利点があります。呼び出し全体が型にとらわれず、入力と読み取りが簡単になるため、結果をキャストしないのは良いことです。ただし、必ず行ってください。ptmalloc()malloc()malloc()#include <stdlib.h>

したがって、s にスペースを割り当てるにはn double、次のようにします。

double *pd = malloc(n * sizeof *pd);
if (pd != NULL) {
    /* malloc succeeded */
} else {
    /* malloc failed */
}

メモリを割り当てたら、インデックスを作成できるようにする必要があります。lx == 2と があるとしましょうly == 3。あなたの記憶は次のようになります。

    +---+---+---+---+---+---+
pd: | 0 | 1 | 2 | 3 | 4 | 5 |
    +---+---+---+---+---+---+

pd[0]pd[1]およびpd[2]double最初の行に対応する値であり、pd[3]pd[6]double2 番目の行に対応する値です。この観察結果を一般化して、特定のx,yインデックス ペアを、pd配列に適切にインデックス付けされる 1 つの数値に変換できるはずです。

于 2010-02-15T05:33:11.417 に答える