• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 239
  • Last Modified:

Matrix

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;
	}

Open in new window

0
kites
Asked:
kites
1 Solution
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi Kites,

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


Kent
0
 
kitesAuthor Commented:
5
0
 
Kent OlsenData Warehouse Architect / DBACommented:

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Kent OlsenData Warehouse Architect / DBACommented:

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


Kent
0
 
kitesAuthor Commented:
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
 
Kent OlsenData Warehouse Architect / DBACommented:
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;

Open in new window




Kent
0
 
kitesAuthor Commented:
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
 
Kent OlsenData Warehouse Architect / DBACommented:

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
 
kitesAuthor Commented:
Thanks Kent!! But now why do I get negative values for my matrix elements.
Smthng like:

-10987777 -2877777
-10988770  -1233328
0
 
Kent OlsenData Warehouse Architect / DBACommented:

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
 
kitesAuthor Commented:
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
 
kitesAuthor Commented:
I got it I was using %ld for double values in C. Instead I used %f
0
 
Infinity08Commented:
>>     **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
 
sarabandeCommented:
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;  
}

Open in new window


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

Sara

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now