0

I am trying to serialise and deserialise a binary tree. I wrote some code and tested it on VSCode, but in online code tester app gives me runtime error, when it works properly on VSCode.

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<stdbool.h>
#include<string.h>
#define MAX 2000
struct TreeNode
{
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
};

struct Queue{
    struct TreeNode* q[MAX];
    int front;
    int rear;
    char str[MAX];
};
void addQueue(struct Queue* Q, struct TreeNode* node){
    Q->q[Q->rear] = node;
    Q->rear = Q->rear + 1;
    return;
}
struct TreeNode* removeQueue(struct Queue* Q){
    struct TreeNode* temp = Q->q[Q->front];
    Q->front = Q->front + 1;
    return temp;
    
}
char removeStr(struct Queue* Q){
    if(Q->front>=Q->rear) return '\0';
    char temp = Q->str[Q->front];
    Q->front = Q->front + 1;
    return temp;
}
bool isEmpty(struct Queue* Q){
    if(Q->front >= Q->rear) return true;
    return false;
}
int qSize(struct Queue* Q){
    return Q->rear - Q->front;
}
void LOT(struct TreeNode* root,char* s){
    struct Queue Q1;
    Q1.front = 0;
    Q1.rear = 0;
    int index = 0;
    addQueue(&Q1,root);
    while(!isEmpty(&Q1)){
        int size = qSize(&Q1);
        for (int i = 0; i < size; i++)
        {
            root = removeQueue(&Q1);
            if(root->left!=NULL)addQueue(&Q1,root->left);
            if(root->right!=NULL)addQueue(&Q1,root->right);
            s[index]=root->val + '0';
            index++;
        }

    }
    s[index]='\0';

}
struct TreeNode* helper(struct Queue* Q){

    if(!isEmpty(Q)){
    char temp = removeStr(Q);
    if(temp == '\0') return NULL;
    struct TreeNode* new = malloc(sizeof(struct TreeNode));
    printf("\nChar : %c",temp);
    new->val = atoi(&temp);
    new->left = helper(Q);
    new->right = helper(Q);
    return new;
    }
    return NULL;
}
char* serialize(struct TreeNode* root) {
    if(!root) return NULL;
    char* s = (char*)malloc(sizeof(char)*MAX);
    LOT(root,s);
    return s;
}
struct TreeNode* deserialize(char* data) {
    struct TreeNode* root;
    struct Queue Q;
    Q.front = 0;
    Q.rear = 0;
    int i;
    int len = strlen(data);
    for(i = 0; i < len; i++){
        Q.str[i] = data[i];
    }
    Q.rear = i;
    root = helper(&Q);
    return root;
}
int main (){
    struct TreeNode* root = malloc(sizeof(struct TreeNode));
    root->val = 1;
    root->left = malloc(sizeof(struct TreeNode));
    root->right = malloc(sizeof(struct TreeNode));
    root->left->val = 2;
    root->right->val = 3;
    root->left->left = NULL;
    root->left->right = NULL;
    root->right->left = malloc(sizeof(struct TreeNode));
    root->right->right = malloc(sizeof(struct TreeNode));
    root->right->left->val = 4;
    root->right->right->val = 5;
    root->right->left->left = NULL;
    root->right->left->right = NULL;
    root->right->right->left = NULL;
    root->right->right->right = NULL;
    char* s = serialize(root);
    printf("\nStr = %s",s);
    struct TreeNode* new = deserialize(s);

    char* w = serialize(new);
    printf("\nStr = %s",w);
    return 0;
}

This is my code above. Output on VSCode : *Str = 12345 Char : 1 Char : 2 Char : 3 Char : 4 Char : 5 Str2 = 12345 But, when I run on online code tester, I get runtime error like;

Address 0x7f547eb00021 is located in stack of thread T0 at offset 33 in frame #0 0x40280f in helper /app/example.c:65 This frame has 1 object(s): [32, 33) 'temp' (line 68) <== Memory access at offset 33 overflows this variable

What does that message mean? How it overflows?

kuwira
  • 15
  • 4
  • 1
    Because you're not using AddressSanitizer in VSCode. Add `-fsanitize=address,undefined` to your compile command line. While you're there make sure you're also using -Wall -Wextra` https://godbolt.org/z/jGfEa6q87 You should know that `s = realloc(s,(sizeof(char)*(index+2)));` won't work correctly in `LOT` because you are assigning a copy of the original pointer, but as part of that process the original is likely freed. You need a double pointer to be able to change what the original points to inside a function. – Retired Ninja Jul 31 '23 at 16:09
  • [How to use realloc in a function in C](https://stackoverflow.com/questions/13748338/how-to-use-realloc-in-a-function-in-c) may help. – Retired Ninja Jul 31 '23 at 16:12

0 Answers0