473,396 Members | 1,784 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,396 software developers and data experts.

seg fault with a linked list in another linked list

4
hi there
i have a linked list content another linked list. which is something like:
Expand|Select|Wrap|Line Numbers
  1. struct nodebranch{
  2.     unsigned int offset;
  3.     unsigned char data[130];
  4.     struct nodebranch *next;
  5. };
  6.  
  7. struct node{
  8.     unsigned int id;
  9.     struct nodebranch *branch;
  10.     struct node *next;
  11. };
  12.  
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(struct 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()
Expand|Select|Wrap|Line Numbers
  1. /*case 1, there s a node in the list have same id */
  2.         if(currentList != NULL && preList!=NULL){
  3.  
  4.             /* insert the packet into the node */
  5.             insertToBranch(&(currentList->branch), l2idu);
  6.             }
  7.  
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
Expand|Select|Wrap|Line Numbers
  1. void insertToBranch(struct nodebranch **bufferNode, L2_IDU l2idu){
  2.  
  3.     printf("in the insertToBranch function\n");
  4.     int offset;                         /* the offset of incoming packet l2idu*/
  5.     int i;                             /* counter i */
  6.     int length;                         /* totle length of the packet */
  7.     int lastPacketDataLength = 0;         /* the data length of the last packet(where MP=0)*/
  8.     struct nodebranch * newNode;      /* newNode to add into branch */
  9.     struct nodebranch * pre;         /* a linked list point one node before current*/
  10.     struct nodebranch * current;      
  11.  
  12.     /* current point to bufferNode */
  13.     current = *bufferNode;
  14.  
  15.  
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
Nov 2 '06 #1
3 1495
nbras
4
hi!
could you upload your file or send it to my e-mail alnbras@gmail.com

I can't understand your code completely...

I hope I can help u
Nov 2 '06 #2
idshiy
4
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.
Nov 3 '06 #3
idshiy
4
but here is the file i have problem with.
Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5.  
  6. #include "bool.h"
  7. #include "layer2.h"
  8. #include "layer3.h"
  9.  
  10. /* The Layer 3 PDU length is the MTU of Layer 3. */
  11. /* (even though we can't really put all that into one packet) */
  12. #define L3_PDU_MAXLENGTH    L3_MTU
  13.  
  14. /* the fill in for other headers which need to be set to zero*/
  15. #define ZERO 0x00;
  16.  
  17. /* a global varible for identification */
  18. static int identificator = 0;
  19.  
  20.  
  21. /* the branch linked list */
  22. struct nodebranch{
  23.     unsigned int offset;
  24.     unsigned char data[130];
  25.     struct nodebranch *next;
  26. };
  27.  
  28. struct node{
  29.     unsigned int id;
  30.     struct nodebranch *branch;
  31.     struct node *next;
  32. };
  33.  
  34. /* the linked list we use to store different msgs before send them*/
  35. static void insertToList(struct node **msgBuffer, L2_IDU l2idu);
  36. static void insertToBranch(struct nodebranch **bufferNode, L2_IDU l2idu);
  37. static int countLength(struct nodebranch **branch);
  38.  
  39. /* the function to check if all the packets have been stored, if is
  40.  * sort and send the msg and return a 1, else return 0
  41.  */
  42. void StoreAndSendMessage(struct nodebranch * branch, int branchLength, int lastPacketDataLength);
  43.  
  44. void checkAndSend(struct nodebranch **branch, L2_IDU l2idu); 
  45.  
  46. //void insert( NodePtr *sPtr, char value);
  47. //void delete( NodePtr *sPtr, char value);
  48.  
  49. /* the packet is a Layer 3 protocol data unit */
  50. typedef unsigned char L3_PDU[L3_PDU_MAXLENGTH];
  51.  
  52. int L3_connected=FALSE;
  53.  
  54. /* the function that Layer 2 calls */
  55. static void L3_receiveL2_IDU(L2_IDU l2idu);
  56.  
  57. /* the pointer to a function, to call when giving a DU to Layer 4 */
  58. static void (* receiveL3_IDU)(L3_IDU l3idu);  
  59.  
  60. /**
  61.  * Place your function header and description here.
  62.  *  - describe how you implemented the reassembly
  63.  *  - describe any data structures you used
  64.  *  - etc.
  65.  */
  66. static void L3_receiveL2_IDU(L2_IDU l2idu)
  67. {
  68.     /* the linked list of linked list which store all the packets
  69.      * before sending them
  70.      */
  71.     printf("show something before the seg fault!!\n");
  72.     struct node *msgBuffer = NULL;
  73.  
  74.     insertToList(&msgBuffer, l2idu);
  75. }
  76.  
  77. void insertToList(struct node **Buffer, L2_IDU l2idu){
  78.  
  79.     int id;                            /* identification */        
  80.     /* check if the linked list already have node with the same
  81.      * identificaton
  82.      */
  83.     struct node *currentList;            /*currentListList pointer*/
  84.     struct node *new;                /*use to add new node*/
  85.     struct node *preList;                /*point to one before msgBuffer*/
  86.  
  87.     /*allocate memory to new*/
  88.     new = malloc(sizeof(struct node));
  89.  
  90.     /* get id */
  91.     id = ((l2idu.data[4]<<8) | l2idu.data[5]);
  92.  
  93.     /* if there s space, insert packet*/
  94.     if(new != NULL){
  95.  
  96.         preList = NULL;
  97.         currentList = *Buffer;
  98.  
  99.         /* loop to the last node in the list or the node which got
  100.          * the same id no. with the packet*/
  101.         while(currentList != NULL && currentList->id != id){
  102.             preList = currentList;
  103.             currentList = currentList->next;
  104.         }
  105.         /*case 1, there s a node in the list have same id */
  106.         if(currentList != NULL && preList!=NULL){
  107.  
  108.             /* insert the packet into the node */
  109.             insertToBranch(&(currentList->branch), l2idu);
  110.             printf("out of insertToBranch function01");
  111.  
  112.         }
  113.         /* otherwise, the list comes to the end, and we ll add the packet
  114.          * on the end of the list*/
  115.         else{    
  116.             new->id = id;
  117.             insertToBranch(&(currentList->branch), l2idu);
  118.             printf("out of insertToBranch function02");
  119.             new->next = NULL; /*node doesnt link to other node*/
  120.  
  121.             /* case 3, empty list */
  122.             if (preList == NULL){
  123.                 new->next = *Buffer;
  124.                 *Buffer = new;
  125.                 /* point currentList to new */
  126.                 currentList = new;
  127.             }
  128.             /* case 2, no id in the list, add to the end of list */
  129.             else {
  130.                 preList->next = new;
  131.                 new->next = currentList;
  132.                 /* point currentList to new */
  133.                 currentList = new;
  134.             }
  135.         }
  136.     } 
  137.     else {
  138.         printf("%c not inserted. No memory available.\n", id );
  139.     }
  140.  
  141.     /* after insert the packet, we need to check if the branch which 
  142.      * just insert the packet has a MF set to true. if its true, means
  143.      * there s chance the whole msg has been recieved. if its so, store 
  144.      * and send the msg, and clear the branch free the memory. 
  145.      */
  146.      checkAndSend(&currentList->branch, l2idu);
  147.  
  148.      if(currentList->branch == NULL){
  149.          preList->next = currentList->next;
  150.          free(currentList);
  151.      }    
  152. }
  153.  
  154.  
  155.  
  156. /*insert the packet into the node with same id, along
  157.  * with data, a offset and a counter will be saved as well
  158.  * for later use. after the packet has been saved onto branch,
  159.  * check if all the packets has been recieved, by check if 
  160.  * the count equl to the offset/16. sort and send them and 
  161.  * make branch to NULL and free the memory.
  162.  * the packet with MF = 0 (the last packet) will be inserted
  163.  * on the end of the list while others will be inserted in the
  164.  * front. 
  165.  */
  166. void insertToBranch(struct nodebranch **bufferNode, L2_IDU l2idu){
  167.  
  168.     printf("in the insertToBranch function\n");
  169.     int offset;                         /* the offset of incoming packet l2idu*/
  170.     int i;                             /* counter i */
  171.     int length;                         /* totle length of the packet */
  172.     int lastPacketDataLength = 0;         /* the data length of the last packet(where MP=0)*/
  173.     struct nodebranch * newNode;      /* newNode to add into branch */
  174.     struct nodebranch * pre;         /* a linked list point one node before current*/
  175.     struct nodebranch * current;      
  176.  
  177.     /* current point to bufferNode */
  178.     current = *bufferNode;
  179.  
  180.     /* get the offset, while the MF is 1, need to change that bit 
  181.      * into 0 1st. 
  182.      */
  183.  
  184.     printf("the current is pointed to *bufferNode\n");
  185.     if( (l2idu.data[6] ^ (1<<5)) > l2idu.data[6]){
  186.         offset = ((l2idu.data[6]<<8) | l2idu.data[7]);
  187.         printf("the offset is : %d \n", offset);
  188.     }
  189.     else {
  190.         offset = ( ( (l2idu.data[6] ^ (1<<5)) <<8) | l2idu.data[7]);
  191.         printf("the offset is : %d \n", offset);
  192.     }
  193.  
  194.     /* get totle length of the packet */
  195.     length = ((l2idu.data[2]<<8) | l2idu.data[3]);
  196.  
  197.     /* asign memory to the new node */
  198.     newNode = malloc(sizeof(struct nodebranch));
  199.  
  200.     /*if the MF = 0, the bufferNode->MF will be set to TRUE 
  201.      */
  202.     if((l2idu.data[6] ^ (1<<5)) > l2idu.data[6]) {
  203.  
  204.         /* the data length of packet which have MF=0 */
  205.         lastPacketDataLength = length - 20;
  206.     }
  207.     /* for all the packets, will be inserted into a sorted list */
  208.     /* loop to get the sorted position of the packet */
  209.     while(current != NULL && offset > current->offset){
  210.         pre = current;
  211.         current = current->next;
  212.     }
  213.  
  214.     /* put packet into a new node */    
  215.     newNode->offset = offset;
  216.     for(i=0; i<(length-20); i++){
  217.         newNode->data[i] = l2idu.data[20+i];
  218.     }
  219.  
  220.     /* for empty list */
  221.     if(pre == NULL){
  222.         newNode->next = *bufferNode;
  223.         *bufferNode = newNode;
  224.     }
  225.     /* if list s not empty, insert new node */
  226.     else{
  227.         pre->next = newNode;
  228.         newNode->next = current;
  229.     }
  230. }
  231.  
  232.  
  233.  
  234. void checkAndSend(struct nodebranch **branch, L2_IDU l2idu){
  235.  
  236.     int i;
  237.     int count = 0;
  238.     int branchLength;
  239.     int lastPacketLength;
  240.     int lastPacketOffset;
  241.     struct nodebranch *pre;
  242.     struct nodebranch *currentCheck;
  243.     L3_IDU l3idu;
  244.  
  245.     /* point current to branch*/
  246.     /*********************************************************************/
  247.     /*THIS IS THE PART CAUSE SEG FAULT(I THINK) */
  248.     currentCheck = *branch;
  249.     /*********************************************************************/
  250.  
  251.     /* check the length of the branch */
  252.     branchLength = countLength(&*branch);
  253.  
  254.     /* get the last node(MF = 0)'s offset */
  255.     while(currentCheck->next != NULL){
  256.         pre = currentCheck;
  257.         currentCheck = currentCheck->next;
  258.     }
  259.     lastPacketOffset = currentCheck->offset;
  260.  
  261.     lastPacketLength = countLength(&currentCheck);
  262.  
  263.     /*compare the banch length with lastPackOffset/16 see if all the packets has been
  264.      * received */
  265.      if(branchLength == lastPacketOffset/16){
  266.  
  267.          /* store the msg into l3idu */
  268.          l3idu.length = (branchLength - 1)*128 + lastPacketLength;
  269.  
  270.          /*store the data, since they already been sorted in order, so just go node 
  271.           * by node, and store them into l3idu. */
  272.          while(currentCheck->next != NULL){
  273.  
  274.              for(i=0; i<128; i++){
  275.                  l3idu.data[i + (count*128)] = currentCheck->data[i];
  276.              }
  277.              pre->next = currentCheck;
  278.              currentCheck = currentCheck->next;
  279.              free(pre);
  280.              count++;
  281.          }
  282.          /* it come to the last node, which have MF=0 and data length from 1 to 130*/
  283.          for(i=0; i<lastPacketLength; i++){
  284.              l3idu.data[i + (count * 128)] = currentCheck->data[i];
  285.          }
  286.          free(currentCheck);
  287.  
  288.          receiveL3_IDU(l3idu);
  289.      }
  290. }
  291.  
  292.  
  293.  
  294. int countLength(struct nodebranch **branch){
  295.  
  296.     int counter = 0;
  297.     struct nodebranch * countlength = *branch;
  298.  
  299.     while(countlength != NULL){
  300.         counter++ ; 
  301.         countlength = countlength->next;
  302.     }    
  303.     return counter;
  304. }     
  305.  
  306.  
  307. /*******************************************************************************
  308. YOU DON'T NEED TO MODIFY ANY CODE BENEATH THIS POINT
  309. The following code will not be read during marking.
  310. *******************************************************************************/
  311.  
  312. void L3_callback(void (*func)(L3_IDU l3idu))
  313. {
  314.     receiveL3_IDU = func;
  315. }
  316.  
  317. void L3_connectToPeerDevice(const char *hostname, int port)
  318. {
  319.     /* establish connection to vhub, etc. */
  320.     L2_connectToPeerDevice(hostname, port);
  321.  
  322.     /* since Layer 1 terminates if can't connect, then assume connected */
  323.     L3_connected=TRUE;
  324.  
  325.     /* set the call back function for receiving bytes from Layer 2 */
  326.     L2_callback(L3_receiveL2_IDU);
  327.  
  328. }
  329.  
  330. void L3_disconnectFromPeerDevice(void)
  331. {
  332.     L2_disconnectFromPeerDevice();
  333.  
  334.     /* since Layer 1 terminates if can't disconnect, then assume disconnected */
  335.     L3_connected=FALSE;
  336. }
  337.  
  338.  
  339.  
have a look into function insertToList and insertToBranch, i think the line cause seg fault is in insertToBranch.

thanks
Nov 3 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

11
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:
3
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...
18
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...
10
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...
7
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"... ;-)...
4
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; }; ...
3
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...
51
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...
0
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...

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.