By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,865 Members | 1,717 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,865 IT Pros & Developers. It's quick & easy.

creating a 2-d array using dynamic memory allocation

P: n/a
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?

Nov 14 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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));
Nov 14 '05 #2

P: n/a
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" <am**********@oracle.com> ??? news:gB************@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));

Nov 14 '05 #3

P: n/a
"kaul" <at******@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) ks***@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.
Nov 14 '05 #4

P: n/a

"Amit" <am**********@oracle.com> wrote in message
news:gB************@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

Nov 14 '05 #5

P: n/a
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.****@gnx.net> bl news:I3********@bath.ac.uk g...

"Amit" <am**********@oracle.com> wrote in message
news:gB************@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

Nov 14 '05 #6

P: n/a
"Andrew Au \(Newsgroup\)" <eg****@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).
Nov 14 '05 #7

P: n/a
On Wed, 8 Sep 2004 15:46:11 +0800, "Andrew Au \(Newsgroup\)"
<eg****@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>>
Nov 14 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.