Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
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 tounsigned 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 :
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 :
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:
or for array-like pointers:
This will still work if you change the type of
pointer
to something else later on (sometype
becomingsome_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!
In
array_minmax_create
you are dereferencing an array that you just declared without allocating memory for it. This is a dangling pointer problem ;-)That was it! Thank you so much, it was driving me nuts.
I got it!
Your string chr_try is to small.
It is of size 5 but we need 5 chars + \0 as terminator.
Change
to
(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?
This comment is hidden because it contains spoiler information about the solution
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.
.
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!
Thanks and good job for passing!
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?
You are right, I found the error in my numbering of array elements. Thanks for comment.
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...