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) ;
    }
  • Martin Ambuhl

    #2
    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;
    Wrong. That is not at all where your problem occurs.
    Your function selectbuffer promises to return a buffer_t *.
    It does not do so.
    Then you try to assign the value not returned by selectbuffer
    buffer = selectbuffer(bu fferlist,pk->id,packetsize) ;
    buffer = selectbuffer(bu fferlist,pk->id,packetsize) ;
    What meaning do you think assigning a value not returned has?

    Comment

    • Anna

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

      Thank you so much for your kindness Martin, Jens, Eric. I'm new to C
      and don't know a lot of things about memory allocation. I'll try to
      implement it right after dinner and will update the results soon.
      thank you so much.

      Comment

      • rahul

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

        On Jun 23, 12:24 am, Anna <petitmou...@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;
        >
        As others have already pointed out, you are getting this as i is
        already past array size and you are trying to write to an invalid
        memory position.

        Comment

        Working...