473,382 Members | 1,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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?

Nov 14 '05 #1
7 2982
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
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
"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

"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
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> ¦b¶l¥ó 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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Tan Thuan Seah | last post by:
Hi all, I was told this in one of the university course I was doing. In C we may expect good performance for: double a, c, d; for (i=0; i<N; i++) for(j=0; j<N; j++) a = a + c *d;
9
by: pvinodhkumar | last post by:
The number of elemets of the array, the array bound must be constant expression?Why is this restriction? Vinodh
6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
14
by: chai | last post by:
Can anyone help me in finding elements stored in a dynamic array in.
3
by: farseer | last post by:
i am getting "error C2057: expected constant expression" with the following code: ifstream f( argv ); f.seekg( 0, ios::end ); const long fSize = f.tellg(); f.close(); char content;
11
by: skumar434 | last post by:
Hi everybody, I am faceing problem while assigning the memory dynamically to a array of structures . Suppose I have a structure typedef struct hom_id{ int32_t nod_de; int32_t hom_id;
4
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
18
by: welch.ryan | last post by:
Hi all, Having a problem with addressing large amounts of memory. I have a simple piece of code here that is meant to allocate a large piece of memory on a ppc64 machine. The code is: /*...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.