A
Anna
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);
}
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);
}