Solved

# Matrix

Posted on 2011-10-12
235 Views
Why do I get segmentation gault? in code attached.
``````Why do I get segmentation fault in foll code:

int i,j,k;
double **m1, **m2, **r;
**m1 = (double **)malloc(size * sizeof(double*));
**m2 = (double **)malloc(size * sizeof(double*));
**r  = (double **)malloc(size * sizeof(double*));

for(i = 0; i < size; i++){
m1[i] = (double *)malloc(size * sizeof(double));
m2[i] = (double *)malloc(size * sizeof(double));
r[i] = (double *)malloc(size * sizeof(double));
}

while(size <=10)
{
size++;
for(i=0;i<size;i++){
for(j=0;j<size;j++)
m1[i][j] = 8.999;
}
``````
0
Question by:kites

LVL 45

Expert Comment

Hi Kites,

What's the value of "size" going into this code?

Kent
0

Author Comment

5
0

LVL 45

Accepted Solution

Then look at the while() loop.  You increment size and check the matrix as long as size is less than 10.  Unfortunately, with only 5 items, you're over indexing the array(s).

kent
0

LVL 45

Expert Comment

You probably want another variable to loop through the array using size as the upper limit, not 10.

Kent
0

Author Comment

You mean something like this?

int k=2, size=10;
while(k <= size)
{k++;
for(i=0;i<k;i++){
for(j=0;j<k;j++)
matrix1 i j = 6.908;
}

0

LVL 45

Expert Comment

Why are you resetting size to 10?  The matrix only has 5 items per row or column.

If you're trying to initialize all of the cells in the matrix to 8.99, it goes like this:

`````` for (i = 0; i < size; ++i)
for (j = 0; j < size;  ++j)
matrix[i][j] = 8.99;
``````

Kent
0

Author Comment

I want matrices of varying sizes , so I am using a while loop to increment size and initializing its values to 8.99(say). I want matrices of 3x3, 4x4,.....10x10
0

LVL 45

Expert Comment

Hi kites,

That's fine.  But you need to confine the indexing into the matrix to the size of the matrix.  Once you've got a matrix of 3x3, you can't index into row or column 4, 7, or 10.  That's what your original code is doing.

Kent
0

Author Comment

Thanks Kent!! But now why do I get negative values for my matrix elements.
Smthng like:

-10987777 -2877777
-10988770  -1233328
0

LVL 45

Expert Comment

You're actually creating 3 matrices (m1, m2, and r) but you're only initializing m1.

Initialize all of them (or only create the ones that you want).

Kent
0

Author Comment

The matrix that I initialized with 8.99 also gives me random numbers. I am trying to print those after initializing them to 8.99.
0

Author Comment

I got it I was using %ld for double values in C. Instead I used %f
0

LVL 53

Expert Comment

>>     **m1 = (double **)malloc(size * sizeof(double*));

would be the first candidate for a segmentation fault. You're dereferencing an uninitialized pointer ... twice.

It should be :

m1 = (double **)malloc(size * sizeof(double*));
0

LVL 32

Expert Comment

you might think of putting all that ugly pointer to pointer stuff into some functions.

``````typedef struct tag_double_square_matrix
{
double * p;
int dim;
} DSM, *PDSM;

void DSM_INIT(PDSM pdsm, int sz, double d)
{
int i, n = sz * sz;
pdsm->p = malloc(sz * sz * sizeof(double));
pdsm->dim = sz;
for (i = 0; i < n; ++i)
*(psdm->p+i) = d;
}

double * DSM_GET(PDSM pdsm, int x, int y)
{
if (x < 0 || x >= pdsm->dim || y < 0 || y >= pdsm->dim)
return NULL;
return pdsm->p + (y * pdsm->dim) + x;
}
``````

and more for setting values to a cell, or even resize a matrix.

Sara

0

## Featured Post

### Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.