0

I am implementing a simple serialization and deserialization program, but I am getting an error during deserialization, as segmentation fault(core dumped).

I am using '|' as a delimiter, to retrieve the data. I think I am making a mistake while putting the data back into the member variable.

I will be trying to send the data over a socket as the next step

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 100

typedef struct Frame
{
    char p[MAX];
    char q[MAX];
    char r[MAX];

}frame;

void serial(char [] , frame*);
void deserial(char [], frame*); 


int main ()
{
    char buffer [MAX];

    int  i = 0;

    frame* packet1;
    frame* packet2;

    char temp[sizeof(frame)]; 

    strcpy(packet1->p, "1234");
    strcpy(packet1->q, "qwerty");
    strcpy(packet1->r, "abcdef");
    serial( temp, packet1);


    deserial( temp, packet2);
    return 0 ;
}

void serial ( char temp[], frame * packet1)
{
    char* p = temp;
    int len ;
    int i=0;

    len = strlen (packet1->p);
    while ( i<len)
    {
        *p = packet1->p[i];
         p++;
         i++;
    }   
    strcat(p,"|");
    p++;
    i  = 0;
    len = strlen (packet1->q);
    while ( i<len)
    {
        *p = packet1->q[i];
         p++;
         i++;
    }
    strcat(p,"|");
    p++;    
    i  = 0;
    len = strlen (packet1->r);
    while ( i<len)
    {
        *p = packet1->r[i];
         p++;
         i++;
    }
    strcat(p,"|");
    return;  
}

void deserial ( char temp[], frame *packet2)
{
    char *str = temp;
    int i= 0 ;
    char delimit[]="|";
    printf("\n");
    while (*str!='|')
    {
        printf ("%c\n", *str);
        packet2->p[i] = *str;  //error here
        str++;
        i++;
    }           
}
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • 1
    Learn to use a debugger. Actually your program probably never made it to `deserial`and not even to `serial` because the segfault most likely happened here: `strcpy(packet1->p, "1234");` when you use the uninitialized `packet1` pointer. – Jabberwocky Feb 08 '18 at 09:35
  • And there are logic errors in the `serial` function: when you use `strcat(p,"|");`, the string in buffer `p` has no NUL terminator, therefore the result of `strcat` results un UB (undefined behaviour). – Jabberwocky Feb 08 '18 at 09:41
  • ... and there are many other issues.... – Jabberwocky Feb 08 '18 at 09:45
  • It made it till the line 'error here' in deserial, dats the reason I forgot about allocating the memory. But Now I fixed them all Thank you very much – user2650443 Feb 08 '18 at 10:00

2 Answers2

1

You have an issue with this code

frame* packet1;   // This is a pointer and it is uninitialized
frame* packet2;

char temp[sizeof(frame)]; 

strcpy(packet1->p, "1234");  // Use of uninitialized pointer

Did you want to do

frame packet1;
frame packet2;

char temp[sizeof(frame)]; 

strcpy(packet1.p, "1234");
...
serial( temp, &packet1);

Further - when you do

strcat(p,"|");

p is pointing to some uninitialized memory so it isn't a string and strcat will (likely) fail. Instead do:

*p = '|';
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
0
strcpy(packet1->p, "1234"); 

Here you are dereferencing a pointer which points to nowhere.You have to allocate memory first.

frame* packet1 = malloc(sizeof(*packet1 ));
Martin Chekurov
  • 733
  • 4
  • 15