Array of struct & segmentation fault :-(

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Anna

    Array of struct & segmentation fault :-(

    Could you please help me? I got a segmentation fault message while
    trying to assign a pointer = pointer like this:

    bufferlist[i]=(buffer_t*)buf fernew;

    What's the error by doing this? Here is the full C script of what I
    did. I would be really really appreciate your help. I need to finish
    this code by monday but i'm stuck at this point and can't solve it :-(
    Thank you very much

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

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #define K 5
    #define Kprime 7
    #define N 10
    #define MAX_PACKETSIZE 1000
    #define BUFFER_SIZE 20

    typedef struct _buffer {
    int packetid; /**< the parameter key **/
    int** decodetab; /**< the parameter value **/
    int counter;
    } buffer_t;


    typedef struct _packet {
    int* data;
    int id;
    } packet_t;


    buffer_t* selectbuffer(bu ffer_t** bufferlist,int id,int packetsize){
    int i;
    buffer_t* buffernew;

    i=0;
    while(bufferlis t[i]!=NULL){
    i=i+1;
    if(i>=BUFFER_SI ZE){
    printf("element not found & buffer overflow");
    break;}
    if(bufferlist[i]->packetid==id ){
    return bufferlist[i];
    }
    }
    //when element not found, we will add new buffer at the end of this
    bufferlist
    if(id<Kprime){
    buffernew=(buff er_t*)malloc(si zeof(buffer_t)) ;
    buffernew->counter=0;
    buffernew->packetid=id;
    bufferlist[i]=(buffer_t*)buf fernew; <************** ********** the
    error come from this line

    }
    }

    int main(){
    packet_t* pk;
    buffer_t* buffer;
    buffer_t** bufferlist;
    int packetsize;
    packetsize=10;
    pk = malloc(sizeof(p acket_t));
    pk->id=3;
    bufferlist = malloc(sizeof(b uffer_t*)*BUFFE R_SIZE);
    buffer = selectbuffer(bu fferlist,pk->id,packetsize) ;
    buffer = selectbuffer(bu fferlist,pk->id,packetsize) ;
    }
  • Jens Thoms Toerring

    #2
    Re: Array of struct &amp; segmentation fault :-(

    Anna <petitmouton@gm ail.comwrote:
    Could you please help me? I got a segmentation fault message while
    trying to assign a pointer = pointer like this:
    bufferlist[i]=(buffer_t*)buf fernew;
    What's the error by doing this? Here is the full C script of what I
    did. I would be really really appreciate your help. I need to finish
    this code by monday but i'm stuck at this point and can't solve it :-(
    I have a bit of a doubt about the exact place where the crash
    happens. I am going to put the main function first since then
    it's easier to see...
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #define K 5
    #define Kprime 7
    #define N 10
    #define MAX_PACKETSIZE 1000
    #define BUFFER_SIZE 20
    typedef struct _buffer {
    int packetid; /**< the parameter key **/
    int** decodetab; /**< the parameter value **/
    int counter;
    } buffer_t;
    >
    typedef struct _packet {
    int* data;
    int id;
    } packet_t;
    >
    int main(){
    packet_t* pk;
    buffer_t* buffer;
    buffer_t** bufferlist;
    int packetsize;
    packetsize=10;
    pk = malloc(sizeof(p acket_t));
    pk->id=3;
    bufferlist = malloc(sizeof(b uffer_t*)*BUFFE R_SIZE);
    Here you make 'bufferlist' point to an arrau of BUFFER_SIZE
    pointers. Please note that the memoty that 'bufferlist' is
    now pointing to isn't initialized, so it will contain some
    random data.
    buffer = selectbuffer(bu fferlist,pk->id,packetsize) ;
    buffer = selectbuffer(bu fferlist,pk->id,packetsize) ;
    }
    buffer_t* selectbuffer(bu ffer_t** bufferlist,int id,int packetsize){
    int i;
    buffer_t* buffernew;
    i=0;
    while(bufferlis t[i]!=NULL){
    And here you compare the elements of 'bufferlist' with NULL. But
    since the memory 'bufferlist' points to was never initialized
    you have a rather good chance that the elements aren't set to
    NULL.
    i=i+1;
    if(i>=BUFFER_SI ZE){
    printf("element not found & buffer overflow");
    break;}
    I guess you should return here. Otherwise you may be going to
    use a non-existing element later on in the function.
    if(bufferlist[i]->packetid==id ){
    And now you derefence a pointer that points to some random location
    in memory. In many cases that will lead to a segmentation fault.
    return bufferlist[i];
    }
    }
    //when element not found, we will add new buffer at the end of this
    //bufferlist
    if(id<Kprime){
    buffernew=(buff er_t*)malloc(si zeof(buffer_t)) ;
    Why the cast of the return value of malloc()? It doesn't help
    and just can hide errors like forgetting to include <stdlib.h>
    buffernew->counter=0;
    buffernew->packetid=id;
    bufferlist[i]=(buffer_t*)buf fernew; <************** ********** the
    error come from this line
    The only reason I can see why it would crash here is that
    'i' got as large as BUFFER_SIZE and you now assign to an
    element of 'bufferlist' that doesn't exist.
    }
    }
    You should initialize the 'bufferlist' array in main, e.g. after
    bufferlist = malloc(sizeof(b uffer_t*)*BUFFE R_SIZE);
    do

    for ( i = 0; i < BUFFER_SIZE; i++ )
    bufferlist[ i ] = NULL;

    and instead of continuing when 'i' has reached BUFFER_SIZE in the
    selectbuffer() function you need to bail out. That should take care
    of the problems pointed out above.

    There are some other issues here like not returning the promised
    value from main() and selectbuffer(), but they probably don't
    contribute to your problems but should be removed anyway.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ jt@toerring.de
    \______________ ____________ http://toerring.de

    Comment

    • Eric Sosman

      #3
      Re: Array of struct &amp; segmentation fault :-(

      Anna wrote:
      Could you please help me? I got a segmentation fault message while
      trying to assign a pointer = pointer like this:
      >
      bufferlist[i]=(buffer_t*)buf fernew;
      >
      What's the error by doing this? Here is the full C script of what I
      did. I would be really really appreciate your help. I need to finish
      this code by monday but i'm stuck at this point and can't solve it :-(
      The lack of indentation makes your code hard to read.
      My eyes hurt from trying to make sense out of it, so I've
      given it only a brief look. That is, there may well be
      more errors than those I've spotted ...


      [...]
      buffer_t* selectbuffer(bu ffer_t** bufferlist,int id,int packetsize){
      int i;
      buffer_t* buffernew;
      >
      i=0;
      while(bufferlis t[i]!=NULL){
      i=i+1;
      if(i>=BUFFER_SI ZE){
      printf("element not found & buffer overflow");
      break;}
      This is broken in multiple ways. For example, it never
      inspects or uses bufferlist[0], because it increments `i' too
      early. And it's happy to inspect bufferlist[BUFFER_SIZE], even
      though there's no such element in the array, because the test
      for `i' reaching BUFFER_SIZE is too late. And if the test
      does happen to fire, the loop exits with `i' out of range --
      and then the rest of the function just goes ahead and uses
      the out-of-range `i' ...

      Try something more along these lines:

      for (i = 0; i < BUFFER_SIZE; ++i) {
      if (bufferlist[i] == NULL) {
      /* allocate and insert new item */
      }
      if (bufferlist[i]->packetid == id) {
      return bufferlist[i];
      }
      }
      fprintf (stderr, "Not found and couldn't add\n");
      return NULL; /* or exit(EXIT_FAILU RE) or ... */

      Other matters: When you ask malloc() for memory, check
      whether it succeeded by comparing the returned pointer to
      NULL. Also, when you ask malloc() for memory, don't assume
      anything about the contents of that memory; there is no
      reason to believe that the memory allocated to bufferlist
      will be magically filled with NULLs.

      --
      Eric Sosman
      esosman@ieee-dot-org.invalid

      Comment

      • Keith Thompson

        #4
        Re: Array of struct &amp; segmentation fault :-(

        Anna <petitmouton@gm ail.comwrites:
        Could you please help me? I got a segmentation fault message while
        trying to assign a pointer = pointer like this:
        >
        bufferlist[i]=(buffer_t*)buf fernew;
        >
        What's the error by doing this? Here is the full C script of what I
        did. I would be really really appreciate your help. I need to finish
        this code by monday but i'm stuck at this point and can't solve it :-(
        Thank you very much
        >
        [snip]
        int main(){
        packet_t* pk;
        buffer_t* buffer;
        buffer_t** bufferlist;
        int packetsize;
        packetsize=10;
        pk = malloc(sizeof(p acket_t));
        pk->id=3;
        bufferlist = malloc(sizeof(b uffer_t*)*BUFFE R_SIZE);
        buffer = selectbuffer(bu fferlist,pk->id,packetsize) ;
        buffer = selectbuffer(bu fferlist,pk->id,packetsize) ;
        }
        Your code is very difficult to read. Please indent it properly if you
        expect anyone to read it.

        If you're using tabs for indentation, please try to use just spaces;
        Usenet software sometimes deletes tabs.

        --
        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
        Nokia
        "We must do something. This is something. Therefore, we must do this."
        -- Antony Jay and Jonathan Lynn, "Yes Minister"

        Comment

        Working...