4

Eg: input: char *str1 = "the are all is well"; char *str2 = "is who the"; output: common words in two given strings, return 2D array of strings.

#define SIZE 31

char ** commonWords(char *str1, char *str2) {

    int i,j=0,count1,count2,k=0,a,b,m=0,n;
    char str3[100][100], str4[100][100];
    char **output;
    output = (char **)malloc(SIZE*sizeof(char*));
    if (str1 == NULL || str2 == NULL)
    {
        return NULL;
    }
    for (i = 0; str1[i] != '\0'; i++)
    {
        if (str1[i] != ' ')
        {
            str3[j][k++] = str1[i];
        }
        else
        {
            str3[j][k++] = '\0';
            j++;
            k = 0;
        }
    }
    str3[j][k++] = '\0';
    count1 = j > 0 ? j + 1 : j;
    j =  k = 0;
    for (i = 0; str2[i] != '\0'; i++)
    {
        if (str2[i] != ' ')
        {
            str4[j][k++] = str2[i];
        }
        else
        {
            str4[j][k++] = '\0';
            j++;
            k = 0;
        }
    }
    str4[j][k++] = '\0';
    count2 = j > 0 ? j + 1 : j;
    for (i = 0; i < count1; i++)
    {
        for (j = 0; j < count2; j++)
        {
            if (str3[i][k] == str4[j][k])
            {
                if (str3[i][k + 1] == str4[j][k + 1] && str3[i][k + 2] == str4[j][k + 2] == '\0')
                {
                    a = i;
                    b = k;
                    while (str3[a][b] != '\0')
                    {
                        output = (char **)malloc(SIZE*sizeof(char));
                        output[m][n] = str3[a][b];
                        n++;
                        b++;
                    }
                    output[m][n] = '\0';
                }
                else if (str3[i][k + 1] == str4[j][k + 1] && str3[i][k + 2] == str4[j][k + 2])
                {
                    a = i;
                    b = k;
                    while (str3[a][b] != '\0')
                    {
                        output = (char **)malloc(SIZE*sizeof(char));
                        output[m][n] = str3[a][b];
                        n++;
                        b++;
                    }
                    output[m][n] = '\0';
                    m++;
                }
            }
        }
    }
    return output;
    }

I am debugging this code in visual studios and the test is failed.Its showing this " message: Exception code: C0000005" .It means error related to memory space allocation.So where did i go wrong?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Srikanth
  • 41
  • 5
  • 3
    Welcome to Stack Overflow! [Please see this discussion on why not to cast the return value of `malloc()` and family in `C`.](http://stackoverflow.com/q/605845/2173917). – Sourav Ghosh Mar 21 '16 at 06:32
  • 5
    And the award for the most readable code goes to....no, not to you sorry. – ckruczek Mar 21 '16 at 06:32
  • 1
    Check this line `output = (char **)malloc(SIZE*sizeof(char));` – Sourav Ghosh Mar 21 '16 at 06:32
  • 1
    Why do you overwrite `output` constantly with new allocations? You'll lose the previously allocated memory. – M Oehm Mar 21 '16 at 06:37
  • 1
    If `output` is a `char **`, then it's an array that contains `char *` values. So the number of bytes is `SIZE*sizeof(char *)`, not `SIZE*sizeof(char)` as you have. You are only allocating a fraction of the memory you're trying to use. – Tom Karzes Mar 21 '16 at 06:39

1 Answers1

1

You have the statement

output = (char **)malloc(SIZE*sizeof(char));

at two lines of your program.

You have to modify this statement in order allocate memory for the double pointer output of type char**, but you also need to allocate memory for every element of output like this :

int i;
output = (char **)malloc(SIZE*sizeof(char*));
for (i = 0; i < SIZE; i++)
    output[i] = (char *)malloc(x*sizeof(char));

where x is the desired size.

Also check for NULL pointer return, for instance

if (output[i] == NULL)
    ....
Marievi
  • 4,951
  • 1
  • 16
  • 33