creating a 2-d array using dynamic memory allocation

K

kaul

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[j]
how is that possible?
 
A

Amit

kaul said:
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[j]
how is that possible?

try this:

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

Andrew Au \(Newsgroup\)

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 = cursor;
cursor += height;
}
return int_matrix;
}

Amit said:
kaul said:
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[j]
how is that possible?

try this:

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

Mike Tyka

Amit said:
kaul said:
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[j]
how is that possible?

try this:

int nrows,ncols;
int **array;
array = malloc(nrows * sizeof(int *));
for (i=0;i<nrows;i++)
array=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
 
A

Andrew Au \(Newsgroup\)

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 said:
Amit said:
kaul said:
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[j]
how is that possible?

try this:

int nrows,ncols;
int **array;
array = malloc(nrows * sizeof(int *));
for (i=0;i<nrows;i++)
array=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
 
O

Old Wolf

Andrew Au \(Newsgroup\) said:
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 = 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).
 
B

Barry Schwarz

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 = cursor;
cursor += height;
}
return int_matrix;
}




<<Remove the del for email>>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top