Array of struct & segmentation fault :-(

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);
}
 
J

Jens Thoms Toerring

Anna said:
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 said:
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
 
K

Keith Thompson

Anna said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top