473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 4596
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: jvax | last post by:
Hi, I'm new to C programming and am having the hardest time trying to figure out how to allocate memory for a structure array. Can somebody please see if I'm doing things correctly? My code is as...
10
by: Kieran Simkin | last post by:
Hi, I wonder if anyone can help me, I've been headscratching for a few hours over this. Basically, I've defined a struct called cache_object: struct cache_object { char hostname; char ipaddr;...
7
by: Chandrashekar Tippur | last post by:
All, I need to initialize an array of structures but I don't know how may elements are there. I tried to malloc the array but I am not sure how to initialize them. Snippet: struct myarray{...
8
by: Ben | last post by:
Hi, I am having trouble debugging a segmentation fault...here's my data structure: typedef struct CELL *pCELL; /* Pointers to cells */ struct CELL { SYMBOL symbol; pCELL prev_in_block;...
27
by: lovecreatesbea... | last post by:
This code snippet is an exercise on allocating two dimension array dynamically. Though this one is trivial, is it a correct one? Furthermore, when I tried to make these changes to the original...
4
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or...
13
by: ElderGeek | last post by:
Hello. I'm pretty new to C, and having a problem which I suspect this has to do with how I'm handling (or not handling) pointers. I have an array of dirent structures which I am passing to a...
3
by: Anna | last post by:
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...
4
by: arnuld | last post by:
I am passing an array of struct to a function to print its value. First I am getting Segfaults and weired values. 2nd, is there any elegant way to do this ? /* Learning how to use an array...
8
by: kiser89 | last post by:
I'm having a problem with my array of structs and segmentation faults. I have this struct that represents one line of a source file: struct threeTokens { int lineNumber; char* cmd; char* param;...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.