The last part of the code where I am allocating memory to common_set is not working whereas other mallocs worked fine above. Similarly when I freed the unique_set for the third time it is not done properly. I am not able to get the printf sentences which I wrote after them. I am not able to free the pointer of the union_set. Also I am not able to allocate memory to a new pointer common_pointer.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
void dict(char **str, int man, int MAY) {
    char temp[MAY];
    for (int i = 0; i < man; ++i) {
        for (int j = i + 1; j < man; ++j) {
            if (strcmp(str[i], str[j]) > 0) {
                strcpy(temp, str[i]);
                strcpy(str[i], str[j]);
                strcpy(str[j], temp);
            }
        }
    }
}
//int k = 0;
char **unique_set;
char **a_new_sumset;
int unique(char **s, int l, int d, int k) {
    if (d == l) {
        unique_set[k] = (char*)malloc((strlen(s[l]) + 1) * sizeof(char));
        strcpy(unique_set[k], s[l]);
        return k;
    } else {
        for (int i = l; i < d; i++) {
            if (strcmp(s[i], s[i + 1]) == 0 && i + 1 == d) {
                unique_set[k] = (char*)malloc((strlen(s[i + 1]) + 1) * sizeof(char));
                strcpy(unique_set[k], s[i + 1]);
                return k;
                //printf("demo: %s\n", unique_set[k]);
            } else
            if (strcmp(s[i], s[i + 1]) != 0) {
                unique_set[k] = (char*)malloc((strlen(s[i]) + 1) * sizeof(char));
                strcpy(unique_set[k], s[i]);
                //printf("demo1: %s\n", unique_set[k]);
                k++;
                unique(s, i + 1, d, k);
                break;
            }
        }
    }
}
char **common_set;
char **intersection(char **sum_set, int d) {
    int k = 0;
    for (int i = 0; i < d; i++) {
        if (strcmp(sum_set[i], sum_set[i + 1]) == 0 && strcmp(sum_set[i], sum_set[i + 2]) != 0 && i + 2 < d) {
            common_set[k] = (char*)malloc((strlen(sum_set[i]) + 1) * sizeof(char));
            strcpy(common_set[k], sum_set[i]);
            k++;
        } else
        if (strcmp(sum_set[i], sum_set[i + 1]) == 0 && i + 2 > d) {
            common_set[k] = (char*)malloc((strlen(sum_set[i]) + 1) * sizeof(char));
            strcpy(common_set[k], sum_set[i]);
            k++;
        }
    }
    return common_set;
}
#define maxx1  3   // number of words in grp 1
#define maxy1  10  // word limit for the first group of words
#define maxx2  3   // number of words in grp2
#define maxy2  10  // word limit for the next group of words
int main() {
    char **sum_set;
    char **str_g1;
    char **str_g2;
    char words1[maxy1];
    str_g1 = (char**)malloc(maxx1 * sizeof(char*));
    char words2[maxy2];
    str_g2 = (char**)malloc(maxx2 * sizeof(char*));
    printf("Enter the first group:\n");
    for (int i = 0; i < maxx1; i++) {
        gets(words1);
        str_g1[i] = (char*)malloc((strlen(words1) + 1) * sizeof(char));
        strcpy(str_g1[i], words1);
        //puts(ptr[i]);
        //printf("%d", i);
    }
    printf("Enter the second group:\n");
    for (int i = 0; i < maxx2; i++) {
        gets(words2);
        str_g2[i] = (char*)malloc((strlen(words2) + 1) * sizeof(char));
        strcpy(str_g2[i], words2);
        //puts(ptr[i]);
        //printf("%d", i);
    }
    dict(str_g1, maxx1, maxy1); //to lexicographically arrange the string 1 group
    dict(str_g2, maxx2, maxy2);
    sum_set = (char**)malloc((maxx1 + maxx2) * sizeof(char*));
    a_new_sumset = (char**)malloc((maxx1 + maxx2) * sizeof(char*));
    for (int i = 0; i < maxx1; i++) {
        sum_set[i] = (char*)malloc((strlen(str_g1[i]) + 1) * sizeof(char));
        strcpy(sum_set[i], str_g1[i]);
        //puts(sum_set[i]);
    }
    for (int i = 0; i < maxx2; i++) {
        sum_set[i + maxx1] = (char*)malloc((strlen(str_g2[i]) + 1) * sizeof(char));
        strcpy(sum_set[i + maxx2], str_g2[i]);
        //puts(sum_set[i + maxx1]);
    }
    dict(sum_set, maxx1 + maxx2, maxy1 + maxy2);
    unique_set = (char**)malloc((maxx1) * sizeof(char*));//allocating memory to next string group to compute its set
    int k = unique(str_g1, 0, maxx1 - 1, 0);
    printf("%d \n", k);
    printf("The set of the string A in arranged order is:\n");
    for (int i = 0; i <= k; i++) {
        a_new_sumset[i] = (char*)malloc((strlen(unique_set[i]) + 1) * sizeof(char));
        strcpy(a_new_sumset[i], unique_set[i]);
        puts(unique_set[i]);
        //
    }
    //printf("freed the pointers\n");
    for (int i = 0; i <= k; ++i) {//freeing the arrays
        free(unique_set[i]);
    }
    free(unique_set);
    //printf("freed the pointers\n");//freeing the top pointer
    int a = k;
    unique_set = (char**)malloc((maxx2) * sizeof(char*));//allocating memory to next string group to compute its set
    k = unique(str_g2, 0, maxx2 - 1, 0);
    int b = k;
    printf("The set of the string B in arranged order is:\n");
    for (int i = 0; i <= k; i++) {
        a_new_sumset[i + 1 + a] = (char*)malloc((strlen(unique_set[i]) + 1) * sizeof(char));
        strcpy(a_new_sumset[i + a + 1], unique_set[i]);
        puts(unique_set[i]);
        //strcpy(a_new_sumset[i + a + 1], unique_set[i]);
    }
    printf("%d \n", k);
    for (int i = 0; i <= k; ++i) {//freeing the arrays
        free(unique_set[i]);
    }
    free(unique_set);//freeing the top pointer
    printf("freed the pointers\n");
    unique_set = (char**)malloc((a + b) * sizeof(char*));//allocating memory to unique_set for computing the union of the sets
    k = unique(sum_set, 0, (maxx1 + maxx2) - 1, 0);
    printf("The set of the string A+B in arranged order is:\n");
    for (int i = 0; i <= k; i++)
        puts(unique_set[i]);
    for (int i = 0; i <= k; ++i) {//freeing the arrays
        free(unique_set[i]);
    }
    printf("freed the pointers\n");
    free(unique_set);
    printf("freed the pointers\n");
    printf("The intersection_set of the string A+B in arranged order is:\n");
    common_set = (char**)malloc((maxx1 + maxx2) * sizeof(char*));
    printf("The intersection_set of the string A+B in arranged order is:\n");
    char **p;
    p = intersection(a_new_sumset, (maxx1 + maxx2) - 1);
    printf("The intersection_set of the string A+B in arranged order is:\n");
    for (int i = 0; i <maxx1 + maxx2; i++) {
        puts(p[i]);
    }
}
 
     
    