Seg fault again 
Author Message
 Seg fault again

now I cant transpose matrix

code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

typedef struct {
        float **data;
        int size[2];
        int type;

Quote:
}* matrix;

matrix newmatrix(int m, int n);
matrix trans(matrix a);

int main(int argc, char *argv[])
{
        matrix a, b;
        a = newmatrix(2, 3);
        b = trans(a);/* not working .. dont know why*/
        return 0;

Quote:
}

matrix newmatrix(int m, int n)
{
        int i, j;
        float **tmp;

        matrix tmpmat = NULL;
        tmp = malloc(m * sizeof(float *));
        if(tmp == NULL)
        {
                printf("FATAL ERROR in failed allocation at %s:%d\n", __FILE__,
__LINE__);
                exit(EXIT_FAILURE);
        }

        for(i = 0; i < m; i++)
        {
                tmp[i] = malloc(n * sizeof(float));
                if(tmp[i] == NULL)
                {
                        printf("FATAL ERROR in failed allocation at %s:%d\n", __FILE__,
__LINE__);
                        exit(EXIT_FAILURE);
                }
        }

        for (i = 0; i < m; i++)
        {
                for (j = 0; j < n; j++)
                {
                        tmp[i][j] = 0.0;
                }
        }

        tmpmat = malloc(sizeof(matrix));
        if(tmpmat == NULL)
        {
                printf("FATAL ERROR in failed allocation at %s:%d\n", __FILE__,
__LINE__);
                exit(EXIT_FAILURE);
        }

        tmpmat->data = tmp;
        tmpmat->size[0] = m;
        tmpmat->size[1] = n;
        tmpmat->type = 00;

        return tmpmat;

Quote:
}

matrix trans(matrix a)
{
        matrix tmpmat = NULL;
        int i,j;

        if (a == NULL)
        {
                printf("ERROR: no matrix to transpose at %s:%d\n", __FILE__,
__LINE__);
                return NULL;
        }
        tmpmat = newmatrix(a->size[1], a->size[0]);/* it is not getting
a->size[0] or a-> size[1].. dont know why ... this is the source of
error, help please */
        for (i = 0; i < (a->size[0]); i++)
        {
                for (j = 0; j < (a->size[1]); j++)
                {
                        tmpmat->data[j][i] = a->data[i][j];
                }
        }
        return tmpmat;

Quote:
}



Sat, 10 Jan 2004 20:20:13 GMT  
 Seg fault again

Quote:
> #include <stdio.h>
> #include <stdlib.h>
> #include <math.h>

> typedef struct {
>    float **data;
>    int size[2];
>    int type;
> }* matrix, MATRIX;

             ^^^^^^^ add MATRIX here

Write a macro to deal with malloc() failure.
for e.g.
#define die do{\
printf("FATAL ERROR in failed allocation at %s:%d\n",__FILE__,__LINE__);\
exit(EXIT_FAILURE);\

Quote:
}while(0)
> matrix newmatrix(int m, int n);
> matrix trans(matrix a);

prototype declaration:
matrix newmatrix(int, int);
matrix trans(matrix);

I prefer:
matrix newmatrix();
matrix trans();
It gives you liberty to change the arguments of a function.

Quote:
> int main(int argc, char *argv[])

int main() is enough because you do not use argc and argv.

Quote:
> {
>    matrix a, b;
>    a = newmatrix(2, 3);
>    b = trans(a);/* not working .. dont know why*/
>    return 0;
> }
> matrix newmatrix(int m, int n)
> {
>    int i, j;
>    float **tmp;

>    matrix tmpmat = NULL;
>    tmp = malloc(m * sizeof(float *));
>    if(tmp == NULL)

         die;

Quote:
>    for(i = 0; i < m; i++)
>    {
>            tmp[i] = malloc(n * sizeof(float));
>            if(tmp[i] == NULL)

                  die;
Quote:
>    }

>    for (i = 0; i < m; i++)
>    {
>            for (j = 0; j < n; j++)
>            {
>                    tmp[i][j] = 0.0;
>            }
>    }

>    tmpmat = malloc(sizeof(matrix));

                 ^^^^^^^^^^^^^^^^^^^^^^^ ERROR HERE!!!
        tmpmat = malloc(sizeof(MATRIX));

Quote:
>    if(tmpmat == NULL)
          die;          
>    tmpmat->data = tmp;
>    tmpmat->size[0] = m;
>    tmpmat->size[1] = n;
>    tmpmat->type = 00;

>    return tmpmat;
> }

> matrix trans(matrix a)
> {
>    matrix tmpmat = NULL;
>    int i,j;

>    if (a == NULL)
>    {printf("ERROR: no matrix to transpose at %s:%d\n", __FILE__,
>               __LINE__);
>     return NULL;
>    }
>    tmpmat = newmatrix(a->size[1], a->size[0]);
>    for (i = 0; i < (a->size[0]); i++)
>    {
>            for (j = 0; j < (a->size[1]); j++)
>            {
>                    tmpmat->data[j][i] = a->data[i][j];
>            }
>    }
>    return tmpmat;
> }



Sat, 10 Jan 2004 21:45:39 GMT  
 Seg fault again
thanks for soln. and tips

Quote:


>> #include <stdio.h>
>> #include <stdlib.h>
>> #include <math.h>

>> typedef struct {
>>        float **data;
>>        int size[2];
>>        int type;
>> }* matrix, MATRIX;
>             ^^^^^^^ add MATRIX here

>Write a macro to deal with malloc() failure.
>for e.g.
>#define die do{\
>printf("FATAL ERROR in failed allocation at %s:%d\n",__FILE__,__LINE__);\
>exit(EXIT_FAILURE);\
>}while(0)

>> matrix newmatrix(int m, int n);
>> matrix trans(matrix a);

>prototype declaration:
>matrix newmatrix(int, int);
>matrix trans(matrix);

>I prefer:
>matrix newmatrix();
>matrix trans();
>It gives you liberty to change the arguments of a function.

>> int main(int argc, char *argv[])

>int main() is enough because you do not use argc and argv.

>> {
>>        matrix a, b;
>>        a = newmatrix(2, 3);
>>        b = trans(a);/* not working .. dont know why*/
>>        return 0;
>> }

>> matrix newmatrix(int m, int n)
>> {
>>        int i, j;
>>        float **tmp;

>>        matrix tmpmat = NULL;
>>        tmp = malloc(m * sizeof(float *));
>>        if(tmp == NULL)
>         die;

>>        for(i = 0; i < m; i++)
>>        {
>>                tmp[i] = malloc(n * sizeof(float));
>>                if(tmp[i] == NULL)
>                  die;
>>        }

>>        for (i = 0; i < m; i++)
>>        {
>>                for (j = 0; j < n; j++)
>>                {
>>                        tmp[i][j] = 0.0;
>>                }
>>        }

>>        tmpmat = malloc(sizeof(matrix));
>                 ^^^^^^^^^^^^^^^^^^^^^^^ ERROR HERE!!!
>        tmpmat = malloc(sizeof(MATRIX));

>>        if(tmpmat == NULL)
>          die;
>>        tmpmat->data = tmp;
>>        tmpmat->size[0] = m;
>>        tmpmat->size[1] = n;
>>        tmpmat->type = 00;

>>        return tmpmat;
>> }

>> matrix trans(matrix a)
>> {
>>        matrix tmpmat = NULL;
>>        int i,j;

>>        if (a == NULL)
>>        {printf("ERROR: no matrix to transpose at %s:%d\n", __FILE__,
>>               __LINE__);
>>         return NULL;
>>        }
>>        tmpmat = newmatrix(a->size[1], a->size[0]);

>>        for (i = 0; i < (a->size[0]); i++)
>>        {
>>                for (j = 0; j < (a->size[1]); j++)
>>                {
>>                        tmpmat->data[j][i] = a->data[i][j];
>>                }
>>        }
>>        return tmpmat;
>> }

--
Hemang Ajmera


Sat, 10 Jan 2004 23:45:25 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. priority queue causing seg fault

2. Postgres and C seg fault problem

3. seg fault ;(

4. Stacktrace on seg fault

5. seg fault

6. God damn seg faults

7. seg fault in malloc

8. fscanf seg fault -- why?!?

9. seg fault woes

10. Fun with strings...why do I seg-fault?

11. extern variables causing seg fault?

12. Seg Fault || Dynamic Linked Lists || ISSUES

 

 
Powered by phpBB® Forum Software