468,290 Members | 2,025 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,290 developers. It's quick & easy.

Array of struct & segmentation fault :-(

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);
}
Jun 27 '08 #1
3 4125
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?
Jun 27 '08 #2
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.
Jun 27 '08 #3
On Jun 23, 12:24 am, Anna <petitmou...@gmail.comwrote:
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.
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by jvax | last post: by
10 posts views Thread by Kieran Simkin | last post: by
7 posts views Thread by Chandrashekar Tippur | last post: by
3 posts views Thread by Anna | last post: by
4 posts views Thread by arnuld | last post: by
reply views Thread by NPC403 | last post: by
2 posts views Thread by MrBee | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.