segmentation fault 
Author Message
 segmentation fault

I am writting c program to deal with matrix
I am getting an run time error

here is the code

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

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

Quote:
}matrix;

int newmatrix(matrix *a, int m, int n);
int printmat(matrix *a);

int main(int argc, char *argv[])
{
        matrix a;
        newmatrix(&a, 2, 3);
        printmat(&a);

        return 0;

Quote:
}

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

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

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

        return 0;

Quote:
}

int printmat(matrix *a)
{
        int i,j;
        float **tmp = a->data;

        if (tmp)
        {
                printf("\n");
                for (i = 0; i < (a->size[0]); i++)
                {
                        for (j = 0; j < (a->size[1]); j++)
                        {
                                printf("%5.2f\t", tmp[i][j]);
                                /*here is the error*/
                        }
                        printf("\n");
                }
        }
        return 0;

Quote:
}

/*program ends*/

I know I havent initialize the value of data but it should give me garbage value.

tia

Hemang Ajmera



Sat, 10 Jan 2004 16:46:47 GMT  
 segmentation fault


... about problems allocating a float matrix ...

Hi Hemang Ajmera,

Quote:
> int newmatrix(matrix *a, int m, int n)
> {
> int i;
> float **tmp;

> tmp = (float **)malloc(m * sizeof(float *));
> for(i = 0; i < m; i++);

Amazing what a single character like ";" can do, isn't it ?
In this case, it ends the for-loop.

Quote:
> {
> tmp[i] = (float *)malloc(n * sizeof(float));
> }

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

> return 0;
> }

Regards Rainer


Sat, 10 Jan 2004 17:18:25 GMT  
 segmentation fault

Quote:

> typedef struct
> {
>    float **data;
>    int size[2];
>    int type;
> }matrix;
> int newmatrix(matrix *a, int m, int n)
> {
>    int i;
>    float **tmp;

>    tmp = (float **)malloc(m * sizeof(float *));

Don't cast the return value of malloc(). It's already a void *, it can
be assigned to anything without casting. Also, it's safer to use the
variable's size rather than the type's size. In toto:

 tmp = malloc(m * sizeof *tmp);

Quote:
>            tmp[i] = (float *)malloc(n * sizeof(float));

Ditto.

BTW, I hope that in your real code you check that the malloc()s succeed.
If one fails and you dereference it, boom.

Quote:
>    a->type = 00;

00? Why not just 0?

Quote:
> I know I havent initialize the value of data but it should give me garbage value.

Uninitialised floats can contain trap values. Using uninitialised
objects invokes undefined behaviour. Your compiler doesn't have to give
you anything. Mind you, I'd have expected a bus error, not a segfault,
but that's UB for you.

Richard



Sat, 10 Jan 2004 17:20:43 GMT  
 segmentation fault
Still a long way from correct, but most of the absurd errors have been
removed.

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

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

Quote:
}               matrix;

int             newmatrix(matrix * a, int m, int n);
int             printmat(matrix * a);

int             main(int argc, char *argv[])
{
    matrix          a;
    newmatrix(&a, 2, 3);
    printmat(&a);

    return 0;

Quote:
}

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

    tmp = malloc(m * sizeof(float *));
    if (!tmp) {
        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] == 0) {
            printf("FATAL ERROR in failed allocation at %s:%d\n", __FILE__,
__LINE__);
            exit(EXIT_FAILURE);
        }
    }

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

    return 0;

Quote:
}

int             printmat(matrix * a)
{
    int             i,
                    j;
    float         **tmp = a->data;

    if (tmp) {
        printf("\n");
        for (i = 0; i < (a->size[0]); i++) {
            for (j = 0; j < (a->size[1]); j++) {
                printf("%5.2f\t", tmp[i][j]);
                /* here is the error */
            }
            printf("\n");
        }
    }
    return 0;

Quote:
}

--
C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
 "The C-FAQ Book" ISBN 0-201-84519-9
C.A.P. FAQ: ftp://cap.connx.com/pub/Chess%20Analysis%20Project%20FAQ.htm


Sat, 10 Jan 2004 17:08:52 GMT  
 segmentation fault

Quote:

>> for(i = 0; i < m; i++);
>Amazing what a single character like ";" can do, isn't it ?

thanks ... that was the real BUG ...

thanks to all other who improved my malloc.

------------------------------------------------------------
 Get your FREE web-based e-mail and newsgroup access at:
                http://MailAndNews.com

 Create a new mailbox, or access your existing IMAP4 or
 POP3 mailbox from anywhere with just a web browser.
------------------------------------------------------------



Sat, 10 Jan 2004 18:43:02 GMT  
 segmentation fault

Quote:

> here is the code [snipped in places]

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

I'd be more helpful here about which one is the number of rows and
which the number of columns.    

Quote:

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

>         tmp = (float **)malloc(m * sizeof(float *));

You should not cast the return value of malloc.  For the reason,
please see the FAQ, section...WHOA!  This is not quite covered in the
FAQ.  We have

  7.6 Why am I getting ``warning: assignment of pointer from integer
  lacks a cast'' for calls to malloc?  

  A: Have you #included <stdlib.h>, or otherwise arranged for malloc to
  be declared properly?

and

  7.7 Why does some code carefully cast the values returned by malloc
  to the pointer type being allocated?

  A: Before ANSI/ISO Standard C introduced the void * generic pointer
  type, these casts were typically required to silence warnings (and
  perhaps induce conversions) when assigning between incompatible
  pointer types.  (Under ANSI/ISO Standard C, these casts are no
  longer necessary.)

Although a clever reader could put them together to get to the answer
[#1], could we make the case that the FAQ should make the relationship
between the cast and the (hiding of the) non-inclusion more clear?
This comes up many times, and the parenthetical note in 7.7 doesn't
tell the whole story[#2].

Quote:
>         for(i = 0; i < m; i++); <--- This ; is presumably a typo?
>         {
>                 tmp[i] = (float *)malloc(n * sizeof(float));

And here again, drop the cast.

Quote:
>         }

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

A plain 0 would do.  Be aware that a leading 0 indicates an octal
constant.

Quote:

> int printmat(matrix *a)
> {
>         int i,j;
>         float **tmp = a->data;

>         if (tmp)
>         {
>                 printf("\n");
>                 for (i = 0; i < (a->size[0]); i++)
>                 {
>                         for (j = 0; j < (a->size[1]); j++)
>                         {
>                                 printf("%5.2f\t", tmp[i][j]);
>                                 /*here is the error*/

Yup, and you even identified the cause, but your following assumption
is incorrect.

Quote:
> I know I havent initialize the value of data but it should give me
> garbage value.

If the argument does not match the conversion specifier, the behaviour
is undefined.  Since you haven't stored any floats in a->data, you
can't hope for any predictable behaviour if you try to use them.

Nishad

[#1] ...and a smartass reader could put 7.6 and 7.7 together to get
15.3: "Why don't function prototypes guard against mismatches in
printf's arguments?"

[#2] FWIW, I think the FAQ book does address this directly, but it's
in my office right now so I don't know for certain.
--
"Underneath the concrete, the dream is still alive" -- Talking Heads



Sat, 10 Jan 2004 18:56:04 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Segmentation Fault

2. Segmentation fault caused by malloc()

3. segmentation fault

4. sscanf() : segmentation fault

5. fopen() cause segmentation fault?!

6. Help with Segmentation fault occuring during a function return

7. segmentation fault(Core dumped)

8. segmentation fault(Core dumped)

9. +O1 option ok but +O2 causes segmentation fault

10. Segmentation fault in free

11. Segmentation Fault

12. segmentation fault

 

 
Powered by phpBB® Forum Software