help segmentation fault

Discussion in 'C Programming' started by subirs, Jun 13, 2010.

  1. subirs

    subirs Guest

    Hi,

    I am trying to write a simple code in C using memory allocation. It
    gives me segmentation fault. I have tried using mtrace. There are no
    memory leaks and no segmentation fault unless I uncomment the line

    for(i=0;i<=xcols;i++) x=0.0;
    That is If I try to write to any of the dynamically allocated arrays
    it gives me seg, fault. The code is attached , any helpis deeply
    appreciated.

    Regards
    Subir

    ---------------------------------------------------------------------------------------------------

    #include <stdio.h>

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

    #include<mcheck.h>



    int main()

    {
    mtrace();


    int trows=100;

    int xcols=90;
    int i,j,k;
    double dx,dt,temp,temp1;


    double **matu;



    matu=malloc(trows* sizeof(*matu));

    for(i=0; i<trows; i++)

    {
    matu=malloc(xcols * sizeof(*matu));

    if ( matu == NULL)

    {

    printf("No space for allocation of ith=%d row",i);

    exit(0);

    }
    }

    double *x=malloc(xcols * sizeof(x));
    double *t=malloc(trows * sizeof(t));

    for(i=0;i<=xcols;i++) x=0.0;
    //for(i=0;i<=trows;i++) t=0.0;

    //for(i=0;i<=xcols;i++)
    //for(j=0;j<=trows;j++)
    //matu[j]=0.0;

    /*dx=(1.0-0.0)/xcols;
    dt=(1.0-0.0)/trows;

    for(i=0;i<=xcols;i++)
    x=x[0]+dx*i;

    for(i=0;i<=trows;i++)
    t=t[0]+i*dt; */



    for(i=0; i<trows; i++)

    free(matu);

    free(matu);
    free(x);
    free(t);
    muntrace();

    return 0;

    }
     
    subirs, Jun 13, 2010
    #1
    1. Advertising

  2. Re: help segmentation fault

    On Jun 13, 8:31 am, subirs <> wrote:
    >
    > for(i=0;i<=xcols;i++) x=0.0;
    >

    Off by one. C arrays go from 0 to N-1, not 0 to N.
     
    Malcolm McLean, Jun 13, 2010
    #2
    1. Advertising

  3. subirs

    Ike Naar Guest

    In article <>,
    subirs <> wrote:
    >Hi,
    >
    >I am trying to write a simple code in C using memory allocation. It
    >gives me segmentation fault. I have tried using mtrace. There are no
    >memory leaks and no segmentation fault unless I uncomment the line
    >
    >for(i=0;i<=xcols;i++) x=0.0;
    >That is If I try to write to any of the dynamically allocated arrays
    >it gives me seg, fault. The code is attached , any helpis deeply
    >appreciated.
    >
    >Regards
    >Subir
    >
    >---------------------------------------------------------------------------------------------------
    >
    >#include <stdio.h>
    >
    >#include <stdlib.h>
    >#include <math.h>
    >
    >#include<mcheck.h>
    >
    >
    >
    >int main()
    >
    >{
    > mtrace();
    >
    >
    > int trows=100;
    >
    > int xcols=90;
    > int i,j,k;
    > double dx,dt,temp,temp1;
    >
    >
    > double **matu;
    >
    >
    >
    > matu=malloc(trows* sizeof(*matu));
    >
    > for(i=0; i<trows; i++)
    >
    > {
    > matu=malloc(xcols * sizeof(*matu));
    >
    > if ( matu == NULL)
    >
    > {
    >
    > printf("No space for allocation of ith=%d row",i);
    >
    > exit(0);
    >
    > }
    > }
    >
    > double *x=malloc(xcols * sizeof(x));
    > double *t=malloc(trows * sizeof(t));
    >
    > for(i=0;i<=xcols;i++) x=0.0;
    > //for(i=0;i<=trows;i++) t=0.0;
    >
    > //for(i=0;i<=xcols;i++)
    > //for(j=0;j<=trows;j++)
    > //matu[j]=0.0;
    >
    > /*dx=(1.0-0.0)/xcols;
    > dt=(1.0-0.0)/trows;
    >
    > for(i=0;i<=xcols;i++)
    > x=x[0]+dx*i;
    >
    > for(i=0;i<=trows;i++)
    > t=t[0]+i*dt; */
    >
    >
    >
    >for(i=0; i<trows; i++)
    >
    >free(matu);
    >
    >free(matu);
    >free(x);
    >free(t);
    >muntrace();
    >
    >return 0;
    >
    >}
     
    Ike Naar, Jun 13, 2010
    #3
  4. subirs

    Ike Naar Guest

    In article <hv2202$igh$-september.org>,
    Ike Naar <> wrote:
    [...]

    Sorry, made a mistake while creating the previous post.
    Here's new attempt, hope this one works out better.

    >In article <>,
    >subirs <> wrote:


    >> for(i=0;i<=xcols;i++) x=0.0;


    x has only xcols elements, but the statement above clobbers the
    xcols+1st element in the last iteration of the loop.

    >> [...]
    >>
    >>int main()


    Small nit: ``int main(void)'' is better.

    >> [...]
    >> double *x=malloc(xcols * sizeof(x));


    x is supposed to be an array of xcols elements of type double,
    but this allocates room for xcols elements of type pointer-to-double.

    Also, malloc can fail and return NULL, it is a good idea to check for this.
     
    Ike Naar, Jun 13, 2010
    #4
  5. subirs

    subirs Guest

    Re: help segmentation fault

    On Jun 13, 10:31 am, subirs <> wrote:
    > Hi,
    >
    > I am trying to write a simple code in C using memory allocation.  It
    > gives  me segmentation fault. I have tried using mtrace. There are no
    > memory leaks and no segmentation fault unless I uncomment the line
    >
    > for(i=0;i<=xcols;i++) x=0.0;
    > That is If I try to write to any of the dynamically allocated arrays
    > it gives me seg, fault.   The code is attached , any helpis deeply
    > appreciated.
    >
    > Regards
    > Subir
    >
    > ---------------------------------------------------------------------------------------------------
    >
    > #include <stdio.h>
    >
    > #include <stdlib.h>
    > #include <math.h>
    >
    > #include<mcheck.h>
    >
    > int  main()
    >
    > {
    >         mtrace();
    >
    >         int trows=100;
    >
    >         int xcols=90;
    >         int i,j,k;
    >         double dx,dt,temp,temp1;
    >
    >         double **matu;
    >
    >         matu=malloc(trows* sizeof(*matu));
    >
    >         for(i=0; i<trows; i++)
    >
    >         {
    >         matu=malloc(xcols * sizeof(*matu));
    >
    >         if ( matu == NULL)
    >
    >         {
    >
    >         printf("No space for allocation of ith=%d row",i);
    >
    >         exit(0);
    >
    >         }
    >         }
    >
    >         double *x=malloc(xcols * sizeof(x));
    >         double *t=malloc(trows * sizeof(t));
    >
    >                 for(i=0;i<=xcols;i++) x=0.0;
    >                 //for(i=0;i<=trows;i++) t=0.0;
    >
    >                 //for(i=0;i<=xcols;i++)
    >                 //for(j=0;j<=trows;j++)
    >                 //matu[j]=0.0;
    >
    >                 /*dx=(1.0-0.0)/xcols;
    >                 dt=(1.0-0.0)/trows;
    >
    >                 for(i=0;i<=xcols;i++)
    >                 x=x[0]+dx*i;
    >
    >                 for(i=0;i<=trows;i++)
    >                 t=t[0]+i*dt; */
    >
    > for(i=0; i<trows; i++)
    >
    > free(matu);
    >
    > free(matu);
    > free(x);
    > free(t);
    > muntrace();
    >
    > return 0;
    >
    > }
    >
    >


    Sorry made very basic mistakes..........ignore it
     
    subirs, Jun 13, 2010
    #5
  6. subirs

    Phil Carmody Guest

    (Ike Naar) writes:
    > In article <>,
    > subirs <> wrote:
    > >Hi,
    > >
    > >I am trying to write a simple code in C using memory allocation. It
    > >gives me segmentation fault. I have tried using mtrace. There are no
    > >memory leaks and no segmentation fault unless I uncomment the line
    > >
    > >for(i=0;i<=xcols;i++) x=0.0;


    If x has length xcols, then its elements are x[0] .. x[xcols-1].
    You're addressing x[xcols], and have just shot yourself in the foot.

    Phil
    --
    I find the easiest thing to do is to k/f myself and just troll away
    -- David Melville on r.a.s.f1
     
    Phil Carmody, Jun 13, 2010
    #6
  7. subirs

    Seebs Guest

    Seebs, Jun 13, 2010
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrey Tarasevich
    Replies:
    0
    Views:
    476
    Andrey Tarasevich
    Sep 2, 2003
  2. Kevin Goodsell
    Replies:
    0
    Views:
    496
    Kevin Goodsell
    Sep 2, 2003
  3. Marcia Hon

    Segmentation fault need help

    Marcia Hon, Feb 15, 2004, in forum: C Programming
    Replies:
    4
    Views:
    358
    CBFalconer
    Feb 16, 2004
  4. Fra-it
    Replies:
    13
    Views:
    521
    Keith Thompson
    Nov 6, 2005
  5. Ben
    Replies:
    8
    Views:
    469
Loading...

Share This Page