Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
803 views
in Technique[技术] by (71.8m points)

c - How do I allocate a 2 D array with contigious memory ? How Do I use it to access rows and columns? Give me an example

I have created a 2 d array which reads as follows

     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);
     }

// I allocate a value of 0 to all the elements in this array as below

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

// I print out all my elements below

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

// When I see the output , it shows me a junk value at one particular position 13. I am unable to figure that out .. ALso kindly tell me how to access rows and columns like Eg to acces 7 th column row 0 and 5th row 6 th column in terms of lx, ly as shown in my code

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your approach is definitely heading in the right general direction.

I think this:

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

would normally be:

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

And then without the contiguity requirement, this:

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

in most programs would look like:

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

And finally, that last line needs to be in a loop that assigns each a[i] with it's own malloc'ed space.

However, that won't create contiguous memory. To do that you will need to do that big allocation and then divide it up for the row vector. So, instead of the second malloc in a loop, perhaps something like:

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

Putting it all together:

#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("
");
  }
}

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...