• 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.

  • 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 ;-) .

  • Yes, that was it - thanks!

  • It would be nice it the description mentioned the term opaque pointer. I believe it is the name of the technique used here.

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

  • I posted an explanation of this solution to evarx78's comment. I hope it will help you too.

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

  • I start to understand it. Wonderful kata. Thanks Bubbler, you made my day.
    Well I have not solved it yet, but soon :)

  • Rust: The following 2 unnecessary warnings appear in the main test:

    warning: unused import: `rand::prelude::*`
      --> src/lib.rs:11:5
       |
    11 | use rand::prelude::*;
       |     ^^^^^^^^^^^^^^^^
       |
       = note: `#[warn(unused_imports)]` on by default
    
    warning: unused variable: `i`
      --> src/lib.rs:26:13
       |
    26 |         for i in 0..100 {
       |             ^ help: if this is intentional, prefix it with an underscore: `_i`
       |
       = note: `#[warn(unused_variables)]` on by default
    
    warning: 2 warnings emitted
    
  • I don't see any such issue. The persistence function there has the correct type signature (at least now).

  • Rust: The sample tests and main tests give warnings about unnecessary parentheses.

  • Loading more items...