• C

How do I solve this segmentation fault in my C programon Reliable Protocol Implementation

hi Everyone,

Please find below a listing of my program protocol.cpp.
When I compile the program using the make command in Linux, it compiles without errors but it halts when run giving a segmentation fault. Can someone plese be kidn enough to point out ot me wher ethis problem is coming from and how to solve it. Many Thanks.



// protocol.cpp
#include "protocol.h"
#include "simul.h"
#include "timer.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXDATA 10 //define the maxvalue of the data size which is 10.
#define MAX_SEQ 7 /*should be 2^n-1*/


struct HEADER {       // header which has info and frame
  short checksum;
  char type;
};

enum frameTypes{DATA,ACK,NAK};

struct FRAME{
      HEADER header;
      char data[MAXDATA];
};

struct NODE{
      NODE *next; //This is a link to the next item in the quque
      int length;
      FRAME frame;
};

struct protocolInfo {
  // control information
  NODE *head, *tail;
  int seqnum;
  int acknum;
  short checksum;
  char data[10];
} protoInfo[2];

short checksum(const char *data,int length){
      int i;short cs=0;
      for(i=0; i<length;i++){
            cs+=data[i];
      }
      return cs;
}


void init(char side) {
      protocolInfo *ptr=&protoInfo[side-'A'];
      ptr->acknum=1;
      ptr->seqnum=1;
      ptr->checksum=1;
}



void send(char side, const char *message, int length) {
      // From here on ONLY use the pointer pi
      protocolInfo *ptr=&protoInfo[side-'A'];
      NODE *p=(NODE *)malloc(sizeof(NODE));
      p->next=NULL;
      p->length=length;
      p->frame.header.type=DATA;
      p->frame.header.checksum;
      checksum(&(p->frame.header.type),length+sizeof(HEADER)-sizeof(short));
      memcpy(p->frame.data,message,length);


      // Simplistic solution transmits the data "as is"
      output(side,(char*)&(ptr->head->frame), ptr->head->length+sizeof(HEADER));
}
void input(char side, const char *frame, int length) {
      protocolInfo *ptr=&protoInfo[side-'A'];
      FRAME *f=(FRAME*)frame;
      if(f->header.checksum!=checksum(&(f->header.type),length+sizeof(HEADER)-sizeof(short))){
            //The checksum is wrong!
            printf("\n\t1.%d\n",f->header.checksum);
            printf("'\t2.%d\n",length+sizeof(HEADER)+sizeof(short));
            printf("The Checksum is wrong!\n");
            NODE*newP=(NODE*)malloc(sizeof(NODE));
            newP->next=NULL;
            newP->length=0;
            newP->frame.header.type=NAK;
            newP->frame.header.checksum=0;
            memcpy(newP->frame.data,0,0);

       protocolInfo *Nptr = &protoInfo[side - 'B'];
       //Adding
       Nptr->head->frame.header.type=NAK;

       output(side,(char*)&(Nptr->head->frame),Nptr->head->length+sizeof(HEADER));
      //output(side,(char*)&Nptr->head->frame),length);
      //output(sied,0,length);
      //stopTimer(side);
       }
       else{
            printf("The Checksum is correct!\n");
            //receive(side,frame,length);
       receive(side,f->data,length-sizeof(HEADER));
       stopTimer(side);
       }


}


void timerInterrupt(char side) {}



also what are some lines of codes that I can write in the function void Timer Interrupt?


Regards,

Dinesh


























dinoguyAsked:
Who is Participating?
 
PCTConnect With a Mentor Commented:
Well for the timer lookup
http://sources.redhat.com/ml/bug-glibc/2002-04/msg00593.html
It gives an example.

So far in your code you don't need malloc.
and comment out memcpy(newP->frame.data,0,0);

Let us know if that removes the error!
0
 
PCTCommented:
My guess it that malloc for some reason is returning a Null pointer.
You really need to check for those errors.
Or That you have not initialised you Node * thus it points off into nowhere.
Or memcpy(newP->frame.data,0,0) could be a problem I don't know how the library would handle this. This line doesn't do anything.

Note:
Are you freeing your allocated memory?
Do you really need to use Malloc? I see that you don't seem to be linking any of you structures.
0
 
dinoguyAuthor Commented:
How would I intialise the Node. You have any tips on that thanks.
0
 
PCTCommented:
It is hard for me to say what the problem is because you could have protocol errors as well.
Also your code sample seems incomplete
Eg Send can be written like this -
void send(char side, const char *message, int length) {
     // From here on ONLY use the pointer pi
     protocolInfo *ptr=&protoInfo[side-'A'];
     output(side,(char*)&(ptr->head->frame), ptr->head->length+sizeof(HEADER));
}
which does exactly the same as the example you gave.
So if this is your actual code you have some design problems, I think.
Change
NODE *p=(NODE *)malloc(sizeof(NODE));
with
NODE temp;
NODE *p = &temp;
and see if that removes your segmentation fault.
and
NODE*newP=(NODE*)malloc(sizeof(NODE));
with
NODE temp;
NODE *newP = &temp;

 
0
 
dinoguyAuthor Commented:
Thanks PCT,


Does this mean that the command NODE *p=(NODE *)malloc(sizeof(NODE));

should not be there in the code. Please clarify thanks. Also, do u have any idea on what I can write inside the function void TimerInterrupt?















0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.