Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


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

Posted on 2004-04-03
Medium Priority
Last Modified: 2010-04-15
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++){
      return cs;

void init(char side) {
      protocolInfo *ptr=&protoInfo[side-'A'];

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));

      // 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;
            //The checksum is wrong!
            printf("The Checksum is wrong!\n");

       protocolInfo *Nptr = &protoInfo[side - 'B'];

            printf("The Checksum is correct!\n");


void timerInterrupt(char side) {}

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



Question by:dinoguy
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2

Expert Comment

ID: 10750601
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.

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.

Author Comment

ID: 10750740
How would I intialise the Node. You have any tips on that thanks.

Expert Comment

ID: 10750906
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.
NODE *p=(NODE *)malloc(sizeof(NODE));
NODE temp;
NODE *p = &temp;
and see if that removes your segmentation fault.
NODE temp;
NODE *newP = &temp;


Author Comment

ID: 10751048
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?


Accepted Solution

PCT earned 100 total points
ID: 10752093
Well for the timer lookup
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!

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question