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\n",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
In C, to have one chunk of contiguous memory, you need one
malloc()
, or have a statically allocated array. Since you want dynamic memory, you will needmalloc()
. Since you need everything to be contiguous, you will need only one call to it.Now, what should the call look like? If I understood you correctly, you need
lx
timesly
values, each with sizesizeof(double)
, so you needlx*ly*sizeof(double)
bytes to be allocated.Digression: I prefer writing my
malloc()
calls as follows:Using
sizeof
withsizeof *pt
instead ofsizeof(T)
offers an advantage that if the type ofpt
changes, you don't need to change themalloc()
call. Not casting the result ofmalloc()
is nice because then the wholemalloc()
call is type-agnostic, and is easier to type and read. Be sure to#include <stdlib.h>
though.So, to allocate space for
n
double
s, you can do:Now, after allocating memory, you need to be able to index it. Let's say you have
lx == 2
andly == 3
. Your memory looks like:pd[0]
,pd[1]
andpd[2]
are thedouble
values corresponding to the first row,pd[3]
topd[6]
are thedouble
values corresponding to the second row. You should be able to generalize this observation to translate a givenx,y
index pair to one number that indexes into yourpd
array properly.Either you create a single dimension array
which you will access by
(get position x=28, y=12)
or you create a 2d array like you do, but you access it with
Your approach is definitely heading in the right general direction.
I think this:
would normally be:
And then without the contiguity requirement, this:
in most programs would look like:
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:
Putting it all together:
This code allocates a 10 by 5 contiguous block of memory, initializes it with incrementing doubles, and then prints the values indexed by x and y:
The 2d.h file should contain these lines:
Note: The memory created is only contiguous for some definitions. The memory is logically contiguous, but not necessarily physically contiguous. If this memory is for a device driver for instance, malloc won't work.