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;


    > 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(packet_t));
    > pk->id=3;
    > bufferlist = malloc(sizeof(buffer_t*)*BUFFER_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(bufferlist,pk->id,packetsize);
    > buffer = selectbuffer(bufferlist,pk->id,packetsize);
    > }


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


    > i=0;
    > while(bufferlist!=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_SIZE){
    > 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->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;
    > }
    > }
    > //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));


    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=(buffer_t*)buffernew; <************************ 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(buffer_t*)*BUFFER_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 ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, Jun 22, 2008
    #2
    1. Advertising

  3. Anna <> writes:
    > 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
    >

    [snip]
    > 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);
    > }


    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_Keith) <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"
     
    Keith Thompson, Jun 23, 2008
    #3
    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,880
    Barry Schwarz
    Apr 3, 2004
  2. Replies:
    4
    Views:
    359
    Ian Collins
    May 1, 2007
  3. Anna
    Replies:
    3
    Views:
    296
    rahul
    Jun 23, 2008
  4. crystal twix

    Segmentation fault in struct

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

Share This Page