hi there
i have a linked list content another linked list. which is something like: -
struct nodebranch{
-
unsigned int offset;
-
unsigned char data[130];
-
struct nodebranch *next;
-
};
-
-
struct node{
-
unsigned int id;
-
struct nodebranch *branch;
-
struct node *next;
-
};
-
what i need to do is, search the list, find the node with same id, and insert the packet into the branch of that id with sorted order.
so i have a function called insertToList(st ruct node **msgBuffer, L2_IDU l2idu)
and a function insertToBranch( struct nodebranch **bufferNode, L2_IDU l2idu)
the insertToBranch will be called in insertToList. insertToList basiclly check if the id match node, if yes, call insertToBranch( ), not, add node and call insertToBranch( )
here is how i call insertToBranch( ) from inserToList() -
/*case 1, there s a node in the list have same id */
-
if(currentList != NULL && preList!=NULL){
-
-
/* insert the packet into the node */
-
insertToBranch(&(currentList->branch), l2idu);
-
}
-
and it worked all fine untill when i get into isertToBranch() , when i trying to point a current pointer to the node. part of the code is -
void insertToBranch(struct nodebranch **bufferNode, L2_IDU l2idu){
-
-
printf("in the insertToBranch function\n");
-
int offset; /* the offset of incoming packet l2idu*/
-
int i; /* counter i */
-
int length; /* totle length of the packet */
-
int lastPacketDataLength = 0; /* the data length of the last packet(where MP=0)*/
-
struct nodebranch * newNode; /* newNode to add into branch */
-
struct nodebranch * pre; /* a linked list point one node before current*/
-
struct nodebranch * current;
-
-
/* current point to bufferNode */
-
current = *bufferNode;
-
-
i think there s something wrong with the pointer reference been carried to the insertToBranch( ). but not sure. i know it looks very messy. shoot me questions if u dont get it. and sorry i cant give all the code since they r HUGE.
ps. yes, i have to use linked list of linked list, since i need carefully watch the memory for my program.
cheers
dave
3 1515
hi!
could you upload your file or send it to my e-mail alnbras@gmail.c om
I can't understand your code completely...
I hope I can help u
is there any space i can upload my files here? i have about eight files for this project, so i dont know if i should upload them all.
but here is the file i have problem with. -
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <unistd.h>
-
-
#include "bool.h"
-
#include "layer2.h"
-
#include "layer3.h"
-
-
/* The Layer 3 PDU length is the MTU of Layer 3. */
-
/* (even though we can't really put all that into one packet) */
-
#define L3_PDU_MAXLENGTH L3_MTU
-
-
/* the fill in for other headers which need to be set to zero*/
-
#define ZERO 0x00;
-
-
/* a global varible for identification */
-
static int identificator = 0;
-
-
-
/* the branch linked list */
-
struct nodebranch{
-
unsigned int offset;
-
unsigned char data[130];
-
struct nodebranch *next;
-
};
-
-
struct node{
-
unsigned int id;
-
struct nodebranch *branch;
-
struct node *next;
-
};
-
-
/* the linked list we use to store different msgs before send them*/
-
static void insertToList(struct node **msgBuffer, L2_IDU l2idu);
-
static void insertToBranch(struct nodebranch **bufferNode, L2_IDU l2idu);
-
static int countLength(struct nodebranch **branch);
-
-
/* the function to check if all the packets have been stored, if is
-
* sort and send the msg and return a 1, else return 0
-
*/
-
void StoreAndSendMessage(struct nodebranch * branch, int branchLength, int lastPacketDataLength);
-
-
void checkAndSend(struct nodebranch **branch, L2_IDU l2idu);
-
-
//void insert( NodePtr *sPtr, char value);
-
//void delete( NodePtr *sPtr, char value);
-
-
/* the packet is a Layer 3 protocol data unit */
-
typedef unsigned char L3_PDU[L3_PDU_MAXLENGTH];
-
-
int L3_connected=FALSE;
-
-
/* the function that Layer 2 calls */
-
static void L3_receiveL2_IDU(L2_IDU l2idu);
-
-
/* the pointer to a function, to call when giving a DU to Layer 4 */
-
static void (* receiveL3_IDU)(L3_IDU l3idu);
-
-
/**
-
* Place your function header and description here.
-
* - describe how you implemented the reassembly
-
* - describe any data structures you used
-
* - etc.
-
*/
-
static void L3_receiveL2_IDU(L2_IDU l2idu)
-
{
-
/* the linked list of linked list which store all the packets
-
* before sending them
-
*/
-
printf("show something before the seg fault!!\n");
-
struct node *msgBuffer = NULL;
-
-
insertToList(&msgBuffer, l2idu);
-
}
-
-
void insertToList(struct node **Buffer, L2_IDU l2idu){
-
-
int id; /* identification */
-
/* check if the linked list already have node with the same
-
* identificaton
-
*/
-
struct node *currentList; /*currentListList pointer*/
-
struct node *new; /*use to add new node*/
-
struct node *preList; /*point to one before msgBuffer*/
-
-
/*allocate memory to new*/
-
new = malloc(sizeof(struct node));
-
-
/* get id */
-
id = ((l2idu.data[4]<<8) | l2idu.data[5]);
-
-
/* if there s space, insert packet*/
-
if(new != NULL){
-
-
preList = NULL;
-
currentList = *Buffer;
-
-
/* loop to the last node in the list or the node which got
-
* the same id no. with the packet*/
-
while(currentList != NULL && currentList->id != id){
-
preList = currentList;
-
currentList = currentList->next;
-
}
-
/*case 1, there s a node in the list have same id */
-
if(currentList != NULL && preList!=NULL){
-
-
/* insert the packet into the node */
-
insertToBranch(&(currentList->branch), l2idu);
-
printf("out of insertToBranch function01");
-
-
}
-
/* otherwise, the list comes to the end, and we ll add the packet
-
* on the end of the list*/
-
else{
-
new->id = id;
-
insertToBranch(&(currentList->branch), l2idu);
-
printf("out of insertToBranch function02");
-
new->next = NULL; /*node doesnt link to other node*/
-
-
/* case 3, empty list */
-
if (preList == NULL){
-
new->next = *Buffer;
-
*Buffer = new;
-
/* point currentList to new */
-
currentList = new;
-
}
-
/* case 2, no id in the list, add to the end of list */
-
else {
-
preList->next = new;
-
new->next = currentList;
-
/* point currentList to new */
-
currentList = new;
-
}
-
}
-
}
-
else {
-
printf("%c not inserted. No memory available.\n", id );
-
}
-
-
/* after insert the packet, we need to check if the branch which
-
* just insert the packet has a MF set to true. if its true, means
-
* there s chance the whole msg has been recieved. if its so, store
-
* and send the msg, and clear the branch free the memory.
-
*/
-
checkAndSend(¤tList->branch, l2idu);
-
-
if(currentList->branch == NULL){
-
preList->next = currentList->next;
-
free(currentList);
-
}
-
}
-
-
-
-
/*insert the packet into the node with same id, along
-
* with data, a offset and a counter will be saved as well
-
* for later use. after the packet has been saved onto branch,
-
* check if all the packets has been recieved, by check if
-
* the count equl to the offset/16. sort and send them and
-
* make branch to NULL and free the memory.
-
* the packet with MF = 0 (the last packet) will be inserted
-
* on the end of the list while others will be inserted in the
-
* front.
-
*/
-
void insertToBranch(struct nodebranch **bufferNode, L2_IDU l2idu){
-
-
printf("in the insertToBranch function\n");
-
int offset; /* the offset of incoming packet l2idu*/
-
int i; /* counter i */
-
int length; /* totle length of the packet */
-
int lastPacketDataLength = 0; /* the data length of the last packet(where MP=0)*/
-
struct nodebranch * newNode; /* newNode to add into branch */
-
struct nodebranch * pre; /* a linked list point one node before current*/
-
struct nodebranch * current;
-
-
/* current point to bufferNode */
-
current = *bufferNode;
-
-
/* get the offset, while the MF is 1, need to change that bit
-
* into 0 1st.
-
*/
-
-
printf("the current is pointed to *bufferNode\n");
-
if( (l2idu.data[6] ^ (1<<5)) > l2idu.data[6]){
-
offset = ((l2idu.data[6]<<8) | l2idu.data[7]);
-
printf("the offset is : %d \n", offset);
-
}
-
else {
-
offset = ( ( (l2idu.data[6] ^ (1<<5)) <<8) | l2idu.data[7]);
-
printf("the offset is : %d \n", offset);
-
}
-
-
/* get totle length of the packet */
-
length = ((l2idu.data[2]<<8) | l2idu.data[3]);
-
-
/* asign memory to the new node */
-
newNode = malloc(sizeof(struct nodebranch));
-
-
/*if the MF = 0, the bufferNode->MF will be set to TRUE
-
*/
-
if((l2idu.data[6] ^ (1<<5)) > l2idu.data[6]) {
-
-
/* the data length of packet which have MF=0 */
-
lastPacketDataLength = length - 20;
-
}
-
/* for all the packets, will be inserted into a sorted list */
-
/* loop to get the sorted position of the packet */
-
while(current != NULL && offset > current->offset){
-
pre = current;
-
current = current->next;
-
}
-
-
/* put packet into a new node */
-
newNode->offset = offset;
-
for(i=0; i<(length-20); i++){
-
newNode->data[i] = l2idu.data[20+i];
-
}
-
-
/* for empty list */
-
if(pre == NULL){
-
newNode->next = *bufferNode;
-
*bufferNode = newNode;
-
}
-
/* if list s not empty, insert new node */
-
else{
-
pre->next = newNode;
-
newNode->next = current;
-
}
-
}
-
-
-
-
void checkAndSend(struct nodebranch **branch, L2_IDU l2idu){
-
-
int i;
-
int count = 0;
-
int branchLength;
-
int lastPacketLength;
-
int lastPacketOffset;
-
struct nodebranch *pre;
-
struct nodebranch *currentCheck;
-
L3_IDU l3idu;
-
-
/* point current to branch*/
-
/*********************************************************************/
-
/*THIS IS THE PART CAUSE SEG FAULT(I THINK) */
-
currentCheck = *branch;
-
/*********************************************************************/
-
-
/* check the length of the branch */
-
branchLength = countLength(&*branch);
-
-
/* get the last node(MF = 0)'s offset */
-
while(currentCheck->next != NULL){
-
pre = currentCheck;
-
currentCheck = currentCheck->next;
-
}
-
lastPacketOffset = currentCheck->offset;
-
-
lastPacketLength = countLength(¤tCheck);
-
-
/*compare the banch length with lastPackOffset/16 see if all the packets has been
-
* received */
-
if(branchLength == lastPacketOffset/16){
-
-
/* store the msg into l3idu */
-
l3idu.length = (branchLength - 1)*128 + lastPacketLength;
-
-
/*store the data, since they already been sorted in order, so just go node
-
* by node, and store them into l3idu. */
-
while(currentCheck->next != NULL){
-
-
for(i=0; i<128; i++){
-
l3idu.data[i + (count*128)] = currentCheck->data[i];
-
}
-
pre->next = currentCheck;
-
currentCheck = currentCheck->next;
-
free(pre);
-
count++;
-
}
-
/* it come to the last node, which have MF=0 and data length from 1 to 130*/
-
for(i=0; i<lastPacketLength; i++){
-
l3idu.data[i + (count * 128)] = currentCheck->data[i];
-
}
-
free(currentCheck);
-
-
receiveL3_IDU(l3idu);
-
}
-
}
-
-
-
-
int countLength(struct nodebranch **branch){
-
-
int counter = 0;
-
struct nodebranch * countlength = *branch;
-
-
while(countlength != NULL){
-
counter++ ;
-
countlength = countlength->next;
-
}
-
return counter;
-
}
-
-
-
/*******************************************************************************
-
YOU DON'T NEED TO MODIFY ANY CODE BENEATH THIS POINT
-
The following code will not be read during marking.
-
*******************************************************************************/
-
-
void L3_callback(void (*func)(L3_IDU l3idu))
-
{
-
receiveL3_IDU = func;
-
}
-
-
void L3_connectToPeerDevice(const char *hostname, int port)
-
{
-
/* establish connection to vhub, etc. */
-
L2_connectToPeerDevice(hostname, port);
-
-
/* since Layer 1 terminates if can't connect, then assume connected */
-
L3_connected=TRUE;
-
-
/* set the call back function for receiving bytes from Layer 2 */
-
L2_callback(L3_receiveL2_IDU);
-
-
}
-
-
void L3_disconnectFromPeerDevice(void)
-
{
-
L2_disconnectFromPeerDevice();
-
-
/* since Layer 1 terminates if can't disconnect, then assume disconnected */
-
L3_connected=FALSE;
-
}
-
-
-
have a look into function insertToList and insertToBranch, i think the line cause seg fault is in insertToBranch.
thanks
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: C++fan |
last post by:
Suppose that I define the following class:
class example_class{
public:
example_class();
void funtion_1();
void function_2();
protected:
|
by: Francis Bell |
last post by:
Hello,
I'm trying to read data from a file and then insert that into a linked
list. The way I have it, the program compiles, however, I'm getting a
segmentation fault error message when I run the program. I'm fairly new
at the pointer business, and I'd appreciate any advice. Here's my code:
int main()
{
ifstream fin;
|
by: bumps |
last post by:
Guys,
I am getting segmentation fault while I am trying to open a file in a
small function.
int PingSamplesList::saveListElements (char *outputFileString)
{
PingSampleElement *tmpPtr;
int rc = 0;
ofstream outFile;
|
by: Ben |
last post by:
Hi,
I am a newbie with C and am trying to get a simple linked list working
for my program. The structure of each linked list stores the char
*data and *next referencing to the next link. The problem I get is
that I am trying to link a struct that I have defined and its refusing
to link. I have tried casting my struct into char * but attempts to
cast it back to its original struct to access its contents only seg
faults.
|
by: Alexandre |
last post by:
Hello,
Maybe it's a little OT, but the fact is that I don't necessarly want to
know "how to correct?", but "why it happens?"
I have a program who "segment fault" (ok, that's "normal"... ;-) but
this time, it's not my code who "segment fault" but it's in the call of
malloc/mallopt
I've got:
| |
by: JS |
last post by:
I have a file called test.c. There I create a pointer to a pcb struct:
struct pcb {
void *(*start_routine) (void *);
void *arg;
jmp_buf state;
int stack;
};
struct pcb *pcb_pointer;
|
by: Zheng Da |
last post by:
Program received signal SIGSEGV, Segmentation fault.
0x40093343 in _int_malloc () from /lib/tls/libc.so.6
(gdb) bt
#0 0x40093343 in _int_malloc () from /lib/tls/libc.so.6
#1 0x40094c54 in malloc () from /lib/tls/libc.so.6
It's really strange; I just call malloc() like "tmp=malloc(size);"
the system gives me Segmentation fault
I want to write a code to do like a dynamic array, and the code is as
|
by: Joerg Schoen |
last post by:
Hi folks!
Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.)
For linked lists, mergesort is the typical choice.
While I was looking for a optimized implementation of mergesort for
linked lists, I couldn't find one. I read something about Mcilroy's
"Optimistic Merge Sort" and studied some implementation, but they
were for arrays. Does anybody know if Mcilroys optimization is applicable to
truly linked lists at all?
|
by: Atos |
last post by:
SINGLE-LINKED LIST
Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be anything from a short integer value to a complex struct type, also has a pointer to
the next node in the single-linked list. That pointer will be NULL if the end of the single-linked list is encountered.
The single-linked list travels only one...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |