Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Array of struct & segmentation fault :-(

Reply
Thread Tools

Array of struct & segmentation fault :-(

 
 
Anna
Guest
Posts: n/a
 
      06-22-2008
Could you please help me? I got a segmentation fault message while
trying to assign a pointer = pointer like this:

bufferlist[i]=(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[i]!=NULL){
i=i+1;
if(i>=BUFFER_SIZE){
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=(buffer_t*)malloc(sizeof(buffer_t));
buffernew->counter=0;
buffernew->packetid=id;
bufferlist[i]=(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);
}
 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      06-22-2008
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*)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?
 
Reply With Quote
 
 
 
 
Anna
Guest
Posts: n/a
 
      06-22-2008
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.
 
Reply With Quote
 
rahul
Guest
Posts: n/a
 
      06-23-2008
On Jun 23, 12:24 am, Anna <(E-Mail Removed)> 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*)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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Segmentation fault in struct crystal twix C++ 2 11-10-2009 07:44 AM
Array of struct & segmentation fault :-( Anna C Programming 2 06-23-2008 07:52 PM
segmentation fault writing to a struct using strncpy lurch132002@yahoo.com C Programming 4 05-01-2007 01:52 AM
length of an array in a struct in an array of structs in a struct in an array of structs Tuan Bui Perl Misc 14 07-29-2005 02:39 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM



Advertisments