473,466 Members | 1,393 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Dynamically allocated array

I'm trying to write a C function to compute the compressed row storage
(CRS) vectors of a 2-d matrix. This is used primarily for increasing
computing efficiency of matrices whose main elements are zeros. So
for example, if you were given the following matrix:

A = { 0, 0, 1, 0;
0, 3, 0, 0;
6, 0, 4, 0}

the function would compute three vectors - the non-zero element
vector, the column index vector for each element, and the row pointer
vector. For this example, the element vector would be {1, 3, 6, 4},
the column index vector would be {2, 1, 0, 2}, and the row pointer
vector would be {0, 1, 2, 5}.

Now what I want to do is generate these arrays from matrix A, so
clearly I don't know the array size until I begin to go through each
row of the matrix to see which elements are non-zero. Because of
this, how would I go about allocating the array size at run-time? I'm
assuming you can use the malloc() function, but I'm new to this so any
help would be appreciated. Thanks!

Oct 1 '07 #1
8 3349
On Oct 1, 11:15 am, "Ray D." <ray.delvecc...@gmail.comwrote:
I'm trying to write a C function to compute the compressed row storage
(CRS) vectors of a 2-d matrix. This is used primarily for increasing
computing efficiency of matrices whose main elements are zeros. So
for example, if you were given the following matrix:

A = { 0, 0, 1, 0;
0, 3, 0, 0;
6, 0, 4, 0}

the function would compute three vectors - the non-zero element
vector, the column index vector for each element, and the row pointer
vector. For this example, the element vector would be {1, 3, 6, 4},
the column index vector would be {2, 1, 0, 2}, and the row pointer
vector would be {0, 1, 2, 5}.

Now what I want to do is generate these arrays from matrix A, so
clearly I don't know the array size until I begin to go through each
row of the matrix to see which elements are non-zero. Because of
this, how would I go about allocating the array size at run-time? I'm
assuming you can use the malloc() function, but I'm new to this so any
help would be appreciated. Thanks!
Make a guess as to the size, based on what you know about how sparse
the
matrix is. If you haven't a clue, just pick some arbitrary number
based on
the number of rows/columns, such as r*c/n, where n is 2, or 5, or 100,
or...

Then if you reach that number while indexing through the matrix,
realloc
the three vectors, increasing the size by some factor based on how far
you
have made it through the matrix.
--
Fred Kleinschmidt

Oct 1 '07 #2
Ray D. wrote On 10/01/07 14:15,:
I'm trying to write a C function to compute the compressed row storage
(CRS) vectors of a 2-d matrix. This is used primarily for increasing
computing efficiency of matrices whose main elements are zeros. So
for example, if you were given the following matrix:

A = { 0, 0, 1, 0;
0, 3, 0, 0;
6, 0, 4, 0}

the function would compute three vectors - the non-zero element
vector, the column index vector for each element, and the row pointer
vector. For this example, the element vector would be {1, 3, 6, 4},
the column index vector would be {2, 1, 0, 2}, and the row pointer
vector would be {0, 1, 2, 5}.

Now what I want to do is generate these arrays from matrix A, so
clearly I don't know the array size until I begin to go through each
row of the matrix to see which elements are non-zero. Because of
this, how would I go about allocating the array size at run-time? I'm
assuming you can use the malloc() function, but I'm new to this so any
help would be appreciated. Thanks!
Make a guess, then call malloc(). If the guess turns
out to be too small, make a larger guess and call realloc()
to enlarge the allocated array. Repeat as needed. Optional:
At the end, if the final guess turns out to have been larger
than needed, call realloc() again to shrink to the final size.

Or,

Count the number of non-zero elements in A. This tells
you how large the CRS vectors must be; call malloc() with that
exact size. Now make a second pass, filling in the vectors.

--
Er*********@sun.com
Oct 1 '07 #3
Thanks. Now I'm trying to compile and I'm getting a syntax error when
I try to assign values to the array. This seems really simple, but
for some reason I can't get it! Anyways, here is the code:

#define MATSIZE 8
....
int **A;
....
A = malloc(MATSIZE*sizeof(int *));
for (i = 0; i < MATSIZE; i++)
A[i] = malloc(MATSIZE*sizeof(int));

A = {{0, 1, 0, 0, 0, 3, 0, 0},
{0, 0, 0, 4, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 1},
{0, 0, 0, 1, 0, 7, 0, 0},
{6, 0, 8, 0, 0, 0, 2, 0},
{0, 0, 0, 0, 0, 3, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 5},
{0, 2, 0, 0, 0, 0, 1, 0}};

And here is the error I'm getting: error C2059: syntax error : '{' -
this is for the first line of the A = {{ declaration. I thought that
was the way to define an array? Is this done differently with
pointers? Again, thank you for the help.
Oct 1 '07 #4
On Oct 1, 12:41 pm, "Ray D." <ray.delvecc...@gmail.comwrote:
Thanks. Now I'm trying to compile and I'm getting a syntax error when
I try to assign values to the array. This seems really simple, but
for some reason I can't get it! Anyways, here is the code:

#define MATSIZE 8
...
int **A;
...
A = malloc(MATSIZE*sizeof(int *));
Better: A = malloc( MATSIZE*sizeof(*A) );
That way, if you ever change A to , say, double**,
you don't have to change this statement.
Don't forget to #include <stdlib.h>
for (i = 0; i < MATSIZE; i++)
A[i] = malloc(MATSIZE*sizeof(int));
again, use sizeof( *A[0] )
>
A = {{0, 1, 0, 0, 0, 3, 0, 0},
{0, 0, 0, 4, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 1},
{0, 0, 0, 1, 0, 7, 0, 0},
{6, 0, 8, 0, 0, 0, 2, 0},
{0, 0, 0, 0, 0, 3, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 5},
{0, 2, 0, 0, 0, 0, 1, 0}};

And here is the error I'm getting: error C2059: syntax error : '{' -
this is for the first line of the A = {{ declaration. I thought that
was the way to define an array? Is this done differently with
pointers? Again, thank you for the help.
You have to use:
for ( i=0; i < MATSIZE; i++ ) {
for ( j=0; j < MATSIZE; j++ ) {
A[i][j] = ...
}
}
--
Fred Kleinschmidt

Oct 1 '07 #5
[comp.lang.c] Eric Sosman <Er*********@sun.comwrote:
Make a guess, then call malloc(). If the guess turns
out to be too small, make a larger guess and call realloc()
to enlarge the allocated array. Repeat as needed.
Count the number of non-zero elements in A. This tells
you how large the CRS vectors must be; call malloc() with that
exact size. Now make a second pass, filling in the vectors.
I would think option #2 would be generally preferable, in the name of
reducing expensive *alloc calls.

--
C. Benson Manica | I appreciate all corrections, polite or otherwise.
cbmanica(at)gmail.com |
----------------------| I do not currently read any posts posted through
sdf.lonestar.org | Google groups, due to rampant unchecked spam.
Oct 2 '07 #6
Christopher Benson-Manica wrote:
[comp.lang.c] Eric Sosman <Er*********@sun.comwrote:
> Make a guess, then call malloc(). If the guess turns
out to be too small, make a larger guess and call realloc()
to enlarge the allocated array. Repeat as needed.
> Count the number of non-zero elements in A. This tells
you how large the CRS vectors must be; call malloc() with that
exact size. Now make a second pass, filling in the vectors.

I would think option #2 would be generally preferable, in the name of
reducing expensive *alloc calls.
Knuth illustrates the situation as follows:

"Little old lady, riding a bus: `Little boy, can you
tell me how to get off at Pasadena Street?'

"Little boy: `Just watch me, and get off two stops
before I do.'

"(The joke is that the little boy gives a two-pass
algorithm.)"

In other words: I am about to send you the values of matrix A,
one at a time, by smoke signal. Explain how to apply method #2.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Oct 2 '07 #7
[comp.lang.c] Eric Sosman <es*****@ieee-dot-org.invalidwrote:
In other words: I am about to send you the values of matrix A,
one at a time, by smoke signal. Explain how to apply method #2.
I admit that I didn't read the problem carefully. I plead guilty by
reason of yesterday being Monday :-)

--
C. Benson Manica | I appreciate all corrections, polite or otherwise.
cbmanica(at)gmail.com |
----------------------| I do not currently read any posts posted through
sdf.lonestar.org | Google groups, due to rampant unchecked spam.
Oct 2 '07 #8
#define MATSIZE 8
...
int **A;
...
A = malloc(MATSIZE*sizeof(int *));
for (i = 0; i < MATSIZE; i++)
A[i] = malloc(MATSIZE*sizeof(int));

A = {{0, 1, 0, 0, 0, 3, 0, 0},
{0, 0, 0, 4, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 1},
{0, 0, 0, 1, 0, 7, 0, 0},
{6, 0, 8, 0, 0, 0, 2, 0},
{0, 0, 0, 0, 0, 3, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 5},
{0, 2, 0, 0, 0, 0, 1, 0}};
This makes no sense because A is of type pointer to pointer to int.
You are trying to put a matrix into a pointer. You could do this by
using an initialized array:

#include <stdio.h>

int main (void) {
#define MATSIZE 8
int A[][MATSIZE] = {{0, 1, 0, 0, 0, 3, 0, 0},
{0, 0, 0, 4, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 1},
{0, 0, 0, 1, 0, 7, 0, 0},
{6, 0, 8, 0, 0, 0, 2, 0},
{0, 0, 0, 0, 0, 3, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 5},
{0, 2, 0, 0, 0, 0, 1, 0}};
int i,j;
for (i = 0; i < MATSIZE; i++) {
for (j = 0; j < MATSIZE - 1; j++)
printf ("%d, ", A[i][j]);
printf ("%d\n", A[i][j]);
}

return 0;
}

Oct 5 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: masood.iqbal | last post by:
I am having lots of trouble getting a simple program that initializs a dynamically allocated 2D array to work. My 2D array is not getting initialized properly, and additionally I am getting a...
2
by: songfire | last post by:
Hi everybody! Just wondering if it is possible to point to variables in the heap. For example, is this okay? int * ptr_to_DAIA; // pointer to dynamically allocated integer array ptr_to_DAIA =...
5
by: nmtoan | last post by:
Hi, I could not find any answer to this simple question of mine. Suppose I have to write a program, the main parts of it are as follows: #include <blahblah.h> struct {
94
by: smnoff | last post by:
I have searched the internet for malloc and dynamic malloc; however, I still don't know or readily see what is general way to allocate memory to char * variable that I want to assign the substring...
6
by: bwaichu | last post by:
Is my understanding of the allocation of these correct? I used fixed sized allocations for the example below, so I realize there is some optimization that can be done to those. I would like to...
4
by: wyrmmage | last post by:
hello :) I need to make a dynamically allocated array of pointers, using a .hpp and .cpp file; how do I accomplish this? I think I know how to make an array of pointers, and I know how to make a...
3
by: Jinkuzo | last post by:
Have been working on this project for a week, unfortunately missed the lesson on it and have been struggling to figure it out. These are the instructions and the point at which I'm up to. ...
7
by: Serpent | last post by:
The C-FAQ describes some techniques here: http://c-faq.com/aryptr/dynmuldimary.html I was using something slightly different from the C-FAQ and I was wondering if it was legal. Say I want a...
16
by: gucci09 | last post by:
I was wondering how you would go about creating a dynamically allocated array of strings from a file. i have a dictionary that i want to load in my program in order to spell check. i cant decide if...
7
by: runn | last post by:
Can a class has a member array which will be dynamically allocated during runtime? Such as double* A=new double here n is an integer member variable and will be determined during runtime and...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.