Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   creating a 2-d array using dynamic memory allocation (http://www.velocityreviews.com/forums/t435142-creating-a-2-d-array-using-dynamic-memory-allocation.html)

kaul 09-08-2004 06:51 AM

creating a 2-d array using dynamic memory allocation
 
i want to create a 2-d array containg r rows and c columns by dynamic
memory allocation in a single statement so that i will be able to access
the ith and jth index as say arr[i][j]
how is that possible?


Amit 09-08-2004 07:22 AM

Re: creating a 2-d array using dynamic memory allocation
 
kaul wrote:
> i want to create a 2-d array containg r rows and c columns by dynamic
> memory allocation in a single statement so that i will be able to access
> the ith and jth index as say arr[i][j]
> how is that possible?
>

try this:

int nrows,ncols;
int **array;
array = malloc(nrows * sizeof(int *));
for (i=0;i<nrows;i++)
array[i]=malloc(ncols * sizeof(int));

Andrew Au \(Newsgroup\) 09-08-2004 07:46 AM

Re: creating a 2-d array using dynamic memory allocation
 
Reducing malloc() call for performance, please

int** int_matrix_create(int width, int height, char* functionName, int
lineNumber) {
int** int_matrix;
int* cursor;
int i;
int_matrix = (int** )malloc(width * sizeof(int*) + width * height *
sizeof(int));
cursor = (int*)int_matrix + width;
for (i = 0; i < width; i++) {
int_matrix[i] = cursor;
cursor += height;
}
return int_matrix;
}

"Amit" <amit.a.gupta@oracle.com> ??? news:gBy%c.3$sk2.54@news.oracle.com
???...
> kaul wrote:
> > i want to create a 2-d array containg r rows and c columns by dynamic
> > memory allocation in a single statement so that i will be able to access
> > the ith and jth index as say arr[i][j]
> > how is that possible?
> >

> try this:
>
> int nrows,ncols;
> int **array;
> array = malloc(nrows * sizeof(int *));
> for (i=0;i<nrows;i++)
> array[i]=malloc(ncols * sizeof(int));




Keith Thompson 09-08-2004 08:17 AM

Re: creating a 2-d array using dynamic memory allocation
 
"kaul" <atpugnes@indiatimes.com> writes:
> i want to create a 2-d array containg r rows and c columns by dynamic
> memory allocation in a single statement so that i will be able to access
> the ith and jth index as say arr[i][j]
> how is that possible?


C FAQ 6.16.
<http://www.eskimo.com/~scs/C-faq/faq.html>
<http://www.eskimo.com/~scs/C-faq/q6.16.html>

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Mike Tyka 09-08-2004 10:43 AM

Re: creating a 2-d array using dynamic memory allocation
 

"Amit" <amit.a.gupta@oracle.com> wrote in message
news:gBy%c.3$sk2.54@news.oracle.com...
> kaul wrote:
> > i want to create a 2-d array containg r rows and c columns by dynamic
> > memory allocation in a single statement so that i will be able to access
> > the ith and jth index as say arr[i][j]
> > how is that possible?
> >

> try this:
>
> int nrows,ncols;
> int **array;
> array = malloc(nrows * sizeof(int *));
> for (i=0;i<nrows;i++)
> array[i]=malloc(ncols * sizeof(int));




I never though about doing it this way, i always allocate a linear array of
row*columns elements and
access them using a little macro:

#define sqrmat(y,x,width) ((y)*(width) + (x))
random access then looks like array[sqrmat(myRow,myColumn,nColumns)]

I wonder if anyone has any views on how the two approches compare in terms
of performance ?
In the above method, i assume C must do an internal multiplication of the
sort i'm implementing
explicitly ?

Mike




Andrew Au \(Newsgroup\) 09-08-2004 11:34 AM

Re: creating a 2-d array using dynamic memory allocation
 
Hi Mike,

The ultimate answer to this question is through benchmarking.
See what are your objectives (performance, or space or what)

There are various approach to create a matrix in C, namely:

multiple malloc() calls
single malloc() call with pointer arithmetic
simulate using 1D array.

I suggest a benchmarking by creating once, and index the array a lot of
times and measure the mean allocation time, mean access time, and mean free
time.

Andrew

"Mike Tyka" <m.tyka@gnx.net> bl news:I3pxt4.99y@bath.ac.uk g...
>
> "Amit" <amit.a.gupta@oracle.com> wrote in message
> news:gBy%c.3$sk2.54@news.oracle.com...
> > kaul wrote:
> > > i want to create a 2-d array containg r rows and c columns by dynamic
> > > memory allocation in a single statement so that i will be able to

access
> > > the ith and jth index as say arr[i][j]
> > > how is that possible?
> > >

> > try this:
> >
> > int nrows,ncols;
> > int **array;
> > array = malloc(nrows * sizeof(int *));
> > for (i=0;i<nrows;i++)
> > array[i]=malloc(ncols * sizeof(int));

>
>
>
> I never though about doing it this way, i always allocate a linear array

of
> row*columns elements and
> access them using a little macro:
>
> #define sqrmat(y,x,width) ((y)*(width) + (x))
> random access then looks like array[sqrmat(myRow,myColumn,nColumns)]
>
> I wonder if anyone has any views on how the two approches compare in terms
> of performance ?
> In the above method, i assume C must do an internal multiplication of the
> sort i'm implementing
> explicitly ?
>
> Mike
>
>
>




Old Wolf 09-09-2004 12:16 AM

Re: creating a 2-d array using dynamic memory allocation
 
"Andrew Au \(Newsgroup\)" <eg_ayh@hotmail.com> wrote:
> Reducing malloc() call for performance, please


Increased performance, reduced portability

> int** int_matrix_create(int width, int height, char* functionName, int
> lineNumber) {
> int** int_matrix;
> int* cursor;
> int i;
> int_matrix = (int** )malloc(width * sizeof(int*) + width * height *
> sizeof(int));


Casting malloc's return value is almost always silly (read the FAQ).

> cursor = (int*)int_matrix + width;


You mean (int *)(int_matrix + width), ie. (int *)&int_matrix[width].
Your version would only work if sizeof(int *) == sizeof(int).

For example, systems with 32-bit int and 64-bit (int *) are not
uncommon.

> for (i = 0; i < width; i++) {
> int_matrix[i] = cursor;
> cursor += height;
> }
> return int_matrix;
> }


If int is 64-bit with 64-bit alignment, and int* is 32-bit, and
width is odd, then you get undefined behaviour.

I'd suggest at least using 2 mallocs: one for the int * array
and one for the ints.

Also you have allocated in column-major form whereas C arrays
are row-major, and your system isn't conducive to re-sizing
(it would depend on the OP's requirements, whether this is
important).

Barry Schwarz 09-09-2004 06:14 AM

Re: creating a 2-d array using dynamic memory allocation
 
On Wed, 8 Sep 2004 15:46:11 +0800, "Andrew Au \(Newsgroup\)"
<eg_ayh@hotmail.com> wrote:

>Reducing malloc() call for performance, please
>
>int** int_matrix_create(int width, int height, char* functionName, int
>lineNumber) {
> int** int_matrix;
> int* cursor;
> int i;
> int_matrix = (int** )malloc(width * sizeof(int*) + width * height *
>sizeof(int));


The return from malloc is guaranteed to be properly aligned for any
type of variable, including int* and int.

> cursor = (int*)int_matrix + width;


This arithmetic is performed in units of sizeof(int). If sizeof(int)
is less than sizeof(int*), the value assigned to cursor will not leave
enough room for the "width" int* that are initialized in the next
loop.

> for (i = 0; i < width; i++) {
> int_matrix[i] = cursor;
> cursor += height;
> }
> return int_matrix;
>}




<<Remove the del for email>>


All times are GMT. The time now is 10:00 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.