Ad
  • Custom User Avatar

    I appreciate your comments. I should know this. I got confused because I was recently learning linked lists where I am allocating simple flat pointer to the address of the head, then proceeding to allocate memory for the nodes.

  • Default User Avatar

    Note that your code still is not correct.
    You have a pointer to a structure array_minimax_t.
    This structure has two members : a pointer to int, and a pointer to unsigned int.
    On 64-bit architectures, pointers are 64 bits = 8 bytes wide. Thus the structure array_minimax_t has a size of 8 + 8 = 16 bytes.
    But you are allocating it this way :

      array_minmax_t* array = malloc(sizeof(array_minmax_t*));
    

    What this is doing is allocating the size of a pointer to a structure array_minimax_t , not the size of the structure itself.
    Since that pointer has a size of 8 bytes too, you are not allocating enough memory - you should be allocating this way :

    array_minimax_t *array = malloc(sizeof(array_minimax_t));
    

    Notice the lack of * after the type name: we are allocating the size of the structure, not the size of a pointer to that structure (on most architectures, pointers effectively always have the same size, independently of the objects they point to).

    A common idiom to avoid that kind of errors is to use this syntax:

    sometype *pointer = malloc(sizeof (*pointer));
    

    or for array-like pointers:

    sometype *array = malloc(array_size * sizeof(*array));
    

    This will still work if you change the type of pointer to something else later on (sometype becoming some_other_type), reducing the number of things that you have to review when refactoring your code.

    As to why your code works at all, it is not very important to know. This is undefined behavior which is the nightmare of C programmers. You are not lucky that it worked : you are unlucky not to have had a segmentation fault which would have made you find the bug ;-) .

  • Custom User Avatar

    Yes, that was it - thanks!

  • Default User Avatar

    In array_minmax_create you are dereferencing an array that you just declared without allocating memory for it. This is a dangling pointer problem ;-)

  • Custom User Avatar

    That was it! Thank you so much, it was driving me nuts.

  • Custom User Avatar

    I got it!
    Your string chr_try is to small.
    It is of size 5 but we need 5 chars + \0 as terminator.

    Change

    char chr_try[5];
    

    to

    char chr_try[6];
    
  • Custom User Avatar

    (in C) I get the right answer, store it in string "expected" which is passed from main. All works fine while I'm in the crack function (i.e. I print "expected" to screen and confirm it is correct). In main, the value in "expected" is different. I am starring at my code for an hour now, with no idea what the problem may be. Any suggestions?

  • Custom User Avatar

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

  • Default User Avatar

    You code times out because it is too slow.
    2nd question: I don't think it makes it slower but in some cases can cause undefined behavior.

  • Default User Avatar

    .

  • Custom User Avatar

    Very nice kata, but testing seems to be excessive. After passing 12000 tests, my solution fails due to alleged memory violation, apparently at 1st dual glider test. How can I get a clue what I am doing wrong?

    Edit: I understood the testing mechanism (it wasn't excessive). I found the error in my code. Great kata, thank you!

  • Default User Avatar

    Thanks and good job for passing!

  • Custom User Avatar

    Very nice kata, thank you! I post this as satisfaction rate (currently 88%) appears low - kata made me study how to return pointer to array of structs, and how to control limits of parameters to avoid segmentation errors. I don't think my algorithm was particularly efficient, but no timeout problem; perpahs it is because I used c?

  • Custom User Avatar

    You are right, I found the error in my numbering of array elements. Thanks for comment.

  • Custom User Avatar

    My solution fails Attempt at 4th test, with error "Test Crashed Caught unexpected signal: SIGSEGV (11). Invalid memory access". Is the issue likely to be with my code, or could it be caused by memory leak mentioned in this thread?

  • Loading more items...