• Custom User Avatar

    I already fixed that problem with strdup, I got stuck on new problem after that. Thanks for that strdup, im using it in other katas as well. :D

  • Custom User Avatar

    Just in case the lack of informations in the crash report demotivated you, know that I updated the name of the random test cases to make it more transparent (basically, your static allocations aren't enough), it's possible to pass the tests with well-dosed static allocation, but I'd like to discourage it isn't viable through scaling. In real-cases application static arrays are quite often a good solution for time optimization (when a bound can be set without boring side effects), but in theorical katas dynamic programming is prefered, unless tests are bounded in the description.

  • Custom User Avatar

    Ok, thanks. Ill try get it to work.

  • Custom User Avatar

    Yes I noticed (just retagging you in case you didn't see my edit!)

  • Custom User Avatar

    As long as I know it should be C :D

  • Custom User Avatar

    Ah ah, well for some reason I can't see your hidden comment O_o
    I guess that means it was actually not a C code then, I'll let the right translator handle it. ^^

    EDIT: Nvm your answer is in C, but I couldn't see it for some reason.

    Well what you return is correct, but as I stated you can't return it from the stack. Try this sample case to understand why it's a bad practice:

    #include <criterion/criterion.h>
    #include <stdlib.h>
    
    char  *highestScoringWord(const char *str);
    
    void bigStackUser() {
        char b[20000];
        memset(b, 'x', 19999);
        b[19999] = '\0';
    }
    
    Test(highestScoringWord, should_pass_all_the_tests_provided) {
        char  *res;
    
        res = highestScoringWord("a b");
        cr_assert_str_eq(
            res,
            "b"
        );
        printf("Your answer returns: %s\n", res);
        bigStackUser();
        printf("After using the stack memory: %s\n", res);
    }
    

    On the stack, variable simply pile up so you can see it as an array that starts with [Test cases variables*] (the * marks the end of the stack)
    When you run your function, your stack variable are naturally added at the end of the stack: [Test cases variables|Your own stack variables*]
    But when your function returns, it's stack variables are freed, which means the stack is now: [Test cases variables|*Your own stack variables]
    And when I call another function (here bigStackUser()) it's variables are added at the end of the stack, over your old variables! So the stack is now: [Test cases variables|other variables overriding yours*]
    That's why you must use malloc to return a variable declared on the heap that does not risk being overwritten by other functions.
    EDIT2 Note: I say mallocbecause it's the underlaying function used, but you can call one of it's children, like for example strdup which might be more relevant in your case.

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    Hello, you're surely doing it in C. Well, you are asked to return a string (char*), so you need to allocate it with malloc (if you use a char[], its memory will be theorically released when you leave the function).

    The error you have is that I retrieve your answer, test it and then free it's content. Which is not possible because you didn't return a malloced string.
    If your solution actually works aside from my free() (and C tolerance on segfaults on freshly released memory), please comment your solution between triple ` and as a hidden comment, and I'll fix my testing accordingly!

  • Custom User Avatar

    Hi, can someone tell me what does this mean? "*** Error in `/home/codewarrior/solution': free(): invalid pointer: 0x00007ffe092588b8 *** "
    Thanks for help.

  • Custom User Avatar

    @TheDrw could you check it out?

  • Custom User Avatar

    Hello all. I believe the C++ solution may be broken.
    My solution always fails on the random-generated tests.
    However:

    I receive this random input:
    inrpahbtnftueiearpnkeras k oru nt ao atie ckaim ecrcssoheeaszieida oosba espeuipy dkbyttkteeozurar oasaattpsoc babaea stp gplr pkhh

    My scoring is this:

    inrpahbtnftueiearpnkeras - 271

    k - 11

    oru - 54

    nt - 34

    ao - 16

    atie - 35

    ckaim - 37

    ecrcssoheeaszieida - 174

    oosba - 52

    espeuipy - 116

    dkbyttkteeozurar - 222

    oasaattpsoc - 130

    babaea - 12

    stp - 55

    gplr - 53

    pkhh - 43

    The test result however tells me this:

    Expected: equal to pkhh
    Actual: inrpahbtnftueiearpnkeras

    Am i doing something wrong or the test cases are broken.