Segmentation fault--allocation error?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fishwater00
    New Member
    • Jun 2008
    • 17

    #1

    Segmentation fault--allocation error?

    First of all, I need to thank all friends helping me here. Those days I learn a lot from your advices.
    The basic idea of my code is to read velocity file into domain[i].velocity[k] seperately.
    Start!!!
    Code:
    typedef struct
    {int nx, ny, nz;
      float *velocity;
    }
    
    void a(Model *domain, int i);
    void init(Model *domain, int steps);
    
    void main()
    {int i, j;
     int STEPS = 10;
     
     Model *Domain;
     Domain=(Model *)malloc(sizeof(Model));
    
     init(Domain, STEPS);
    
     for(i=0; i<STEPS; i++)
           a(Domain, i);
    
    return;
    }
    
    void init(Model *domain, int steps)
    {int n=100;
      int k;
     for (k=0; i<n; i++)
      { domain[steps].nx=domain[steps].ny=domain[steps].nz=50;
        domain[steps].velocity=NULL; 
      }
    return;
    }
    
    void a(Model *domain, int i)
    {short buff_v;
      int j,m;
    
     buff_v=(short *)malloc(domain[i].nz*sizeof(short);
    
     read(fd,vel_buf,domain[il].nz * sizeof(short));   /* fd is my original velocity file, it has been created by other program*/
    
    for(dz=0, j=0; dz<domain[i].nz; dz++,j++)
        domain[i].veocity[j] = buff_v[dz] * 0.001 ;  /* segmentation fault comes out here*/
    return;
    }
    Is the allocation of velocity wrong? How can I fix it? HIGHLY appriciated!!!
  • jorba101
    New Member
    • Jun 2008
    • 86

    #2
    Yes, the allocation is wrong.

    You should allocate as much memory as you're actually using, i.e. when doing:

    Code:
    domain = malloc( sizeof( Model ) )
    you're reserving heap memory for only 1 Model element. Afterwards, if you try to initialize STEPS elements in domain (namely 10), it will crash.

    You should declare:

    Code:
    domain = malloc( 10 * sizeof( Model ) )
    But still, I still do not understand why you're using dynamic allocation. You should only use dynamic allocation if you can't know how much memory you need at compile-time (i.e. before running the program). If you do know how many "domain" elements you're going to use, there is no sense in doing malloc.

    In your case I would change the lines:

    Code:
    Model *domain;
    domain=malloc( 10 * sizeof( Model ) )
    To simply:

    Code:
    Model domain[10];
    Same for your declaration of velocity inside Model struct.

    Comment

    • fishwater00
      New Member
      • Jun 2008
      • 17

      #3
      Originally posted by jorba101
      Yes, the allocation is wrong.

      You should allocate as much memory as you're actually using, i.e. when doing:

      But still, I still do not understand why you're using dynamic allocation. You should only use dynamic allocation if you can't know how much memory you need at compile-time (i.e. before running the program). If you do know how many "domain" elements you're going to use, there is no sense in doing malloc.

      Same for your declaration of velocity inside Model struct.
      Thank you. In my case, the struct include many different arrays, such velocity[k], wave1[k], wave2[k], wave3[k], derivatives1[k], derivatives2[k], and so on. every array almost need k=nx*ny*nz*size of(float) memory. But, like you said, I do not know how much memory I need to reserve, and I try to allocate dynamic memory. In other word, In domain[0], domain[1],... each domain[i] includes velocity[k], wave1[k], wave2[k],... All I did is in 3D case.

      Because I am not familiar with struct memory allocation, hope you guys can give me clear direction, or can paste modified codes here.

      Comment

      • jorba101
        New Member
        • Jun 2008
        • 86

        #4
        You should declare buff_v like this:

        Code:
        short *buff_v;
        i.e. a pointer to short data, instead of:

        Code:
        short buff_v;
        which is a variable containing a single short element.

        Comment

        • fishwater00
          New Member
          • Jun 2008
          • 17

          #5
          Originally posted by jorba101
          You should declare buff_v like this:

          Code:
          short *buff_v;
          Thanks. I already add * to it, but still have those problem. I think it is the allocation problem.

          I wonder if you or anyone can write some codes about allocation of arrays of struct. That would be great.

          Thank all in advance.

          Comment

          • jorba101
            New Member
            • Jun 2008
            • 86

            #6
            In this line:

            Code:
            domain[i].veocity[j] = buff_v[dz] * 0.001 ;
            You are assigning values to some place in velocity before allocating memory for it, eg, you should do a malloc for it, as large as the number of elements you are to access in it (I think it's domain[i].nz in this case)

            Greetings,

            Comment

            • amitkp
              New Member
              • Jul 2008
              • 1

              #7
              #
              typedef struct
              #
              {int nx, ny, nz;
              #
              float *velocity;
              #
              }

              as far as understanding of pointer is concerned , always consider these pointer as a field, which doesn't where to point unless you initialize them.
              ok , dont confuse ..
              you have declared Domain *domain , you have declared a pointer to that structure .but you didnt allocate any memory to that..
              so if you try to access domain[10] , you must assign that much memory to domain from heap.
              Previous reply will help you in that..
              Other question comes for velocity, velocity is pointer to a float variable . Again it doesn't know where and what to point.
              so aytime when you access velocity[] (means you are derefrencing velocity for that particular offset), you must be having memory for that..
              any illegal access to memory wll give you seg fault..

              hope it ll clear your doubt..for coding .. assign (do malloc)required memory to velocity , it ll work. :-)

              Comment

              Working...