Invalid pointer in C - Codewars exercice

Viewed 32

I'm working on the "Reverse Word" exercice on codewars and the test works but when I try to Attempt I get different error messages:

Sometimes it will be : Caught unexpected signal: SIGSEGV (11). Invalid memory access.

Or: free(): invalid size

Or : double free or corruption (out)

Or : munmap_chunk(): invalid pointer

My first question would be what is the meaning of those errors and whats the difference between them ? My second question is why does my code work fine when I execute it myself ?

This is the code I submitted on codewars.

The instructions are : "Complete the function that accepts a string parameter, and reverses each word in the string. All spaces in the string should be retained."

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* reverseWords(const char* text)
{
    char *result;
    result = malloc (sizeof text);
    strcpy(result, text);
    int i, j, h, length, temp, index, count, tempo;
    count = 0;
    length = strlen(result);

    for (i=0; i < length ; i++)
    {
        if (result[i] == ' ' || i == length - 1)
        {
            count++;
            if (count == 1)
            {
                index = -1;
                tempo = i;
            }
            else if (count > 1)
            {
                index = tempo;
                tempo = i;
            }

            h = i;

            if (count >= 1 && i == length - 1 && result[i] != ' ')
            {
                h = length;
            }
            if (i != 0)
            {
                for (j=(index + 1); j < ((index +i)/2) + 1 ; j++)
                {
                    temp = result[j];
                    result[j] = result[h -1];
                    result[h -1] = temp;
                    h--;
                }
            }
        }
    }
    return (result);
}

And this is my main function:

int main(void)
{
    char * p; 
    const char text[] = " double  spaces";
    p = reverseWords(text);
    printf("%s\n", p);
    return EXIT_SUCCESS;
}

Also here is the test code of CW:

#include <criterion/criterion.h>
#include <stdlib.h>

char* reverseWords(const char* text);

Test(Sample_Tests, should_pass_all_the_tests_provided) {
  {
    const char* test_str = "apple";
    const char* expected = "elppa";
    char* submitted = reverseWords(test_str);
    cr_assert_str_eq(expected, submitted,
                    "Expected:\n\"%s\"\n\nSubmitted:\n\"%s\"\n\n", expected, submitted);
    free(submitted); submitted = NULL;
  }
  {
    const char* test_str = "a b c d";
    const char* expected = "a b c d";
    char* submitted = reverseWords(test_str);
    cr_assert_str_eq(expected, submitted,
                    "Expected:\n\"%s\"\n\nSubmitted:\n\"%s\"\n\n", expected, submitted);
    free(submitted); submitted = NULL;
  }
  {
    const char* test_str = "double  spaced  words";
    const char* expected = "elbuod  decaps  sdrow";
    char* submitted = reverseWords(test_str);
    cr_assert_str_eq(expected, submitted,
                    "Expected:\n\"%s\"\n\nSubmitted:\n\"%s\"\n\n", expected, submitted);
    free(submitted); submitted = NULL;
  }
  {
    const char* test_str = "The quick brown fox jumps over the lazy dog.";
    const char* expected = "ehT kciuq nworb xof spmuj revo eht yzal .god";
    char* submitted = reverseWords(test_str);
    cr_assert_str_eq(expected, submitted,
                    "Expected:\n\"%s\"\n\nSubmitted:\n\"%s\"\n\n", expected, submitted);
    free(submitted); submitted = NULL;
  }
}

I'm having a bit of trouble understanding pointers and dynamic memory allocation so would be nice if somebody could enlighten me ! :)

Cheers

0 Answers
Related