Array of struct & segmentation fault :-(

Discussion in 'C Programming' started by Anna, Jun 22, 2008.

  1. Anna

    Anna Guest

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

    bufferlist=(buffer_t*)buffernew;

    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(buffer_t** bufferlist,int id,int packetsize){
    int i;
    buffer_t* buffernew;

    i=0;
    while(bufferlist!=NULL){
    i=i+1;
    if(i>=BUFFER_SIZE){
    printf("element not found & buffer overflow");
    break;}
    if(bufferlist->packetid==id){
    return bufferlist;
    }
    }
    //when element not found, we will add new buffer at the end of this
    bufferlist
    if(id<Kprime){
    buffernew=(buffer_t*)malloc(sizeof(buffer_t));
    buffernew->counter=0;
    buffernew->packetid=id;
    bufferlist=(buffer_t*)buffernew; <************************ the
    error come from this line

    }
    }

    int main(){
    packet_t* pk;
    buffer_t* buffer;
    buffer_t** bufferlist;
    int packetsize;
    packetsize=10;
    pk = malloc(sizeof(packet_t));
    pk->id=3;
    bufferlist = malloc(sizeof(buffer_t*)*BUFFER_SIZE);
    buffer = selectbuffer(bufferlist,pk->id,packetsize);
    buffer = selectbuffer(bufferlist,pk->id,packetsize);
    }
    Anna, Jun 22, 2008
    #1
    1. Advertising

  2. Anna wrote:
    > Could you please help me? I got a segmentation fault message while
    > trying to assign a pointer = pointer like this:
    >
    > bufferlist=(buffer_t*)buffernew;


    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(bufferlist,pk->id,packetsize);
    > buffer = selectbuffer(bufferlist,pk->id,packetsize);


    What meaning do you think assigning a value not returned has?
    Martin Ambuhl, Jun 22, 2008
    #2
    1. Advertising

  3. Anna

    Anna Guest

    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.
    Anna, Jun 22, 2008
    #3
  4. Anna

    rahul Guest

    On Jun 23, 12:24 am, Anna <> wrote:
    > Could you please help me? I got a segmentation fault message while
    > trying to assign a pointer = pointer like this:
    >
    > bufferlist=(buffer_t*)buffernew;
    >

    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.
    rahul, Jun 23, 2008
    #4
    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. Goh, Yong Kwang
    Replies:
    3
    Views:
    11,808
    Barry Schwarz
    Apr 3, 2004
  2. Replies:
    4
    Views:
    342
    Ian Collins
    May 1, 2007
  3. Anna
    Replies:
    2
    Views:
    326
    Keith Thompson
    Jun 23, 2008
  4. crystal twix

    Segmentation fault in struct

    crystal twix, Nov 10, 2009, in forum: C++
    Replies:
    2
    Views:
    460
    Ian Collins
    Nov 10, 2009
  5. Tuan  Bui
    Replies:
    14
    Views:
    472
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page