Ad
  • Custom User Avatar

    Not a best practice, for sure.

  • Custom User Avatar

    Yes, but no.
    Please use decimal "ones" or "zeros" when counting in decimal - binary "ones" and "zeros", though technically the same, don't like it very much to be misgendered that way - because they identify as binary :D

  • Custom User Avatar

    Thats so nice and clean - tried similar, but didn't knew you could use a for-loop as a lazy while-loop like that and I
    also wasn't sure if bitshift-equals is a legal expression.

  • Default User Avatar

    why the use of size_t insead of unsigned?

  • Default User Avatar

    0 is true

    == 0 is false

    < 0 is true ?

  • Default User Avatar

    how the loop stops when value == 0? can you explain?

  • Custom User Avatar

    Subtracting a size_t from a size_t is not well-defined when the result is negative. You should compare them first to ensure that the result is positive before subtracting them. Also, the use of the ternary operator here is just not necessary.

  • Default User Avatar

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

  • Default User Avatar

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

  • Custom User Avatar
  • Custom User Avatar

    I made such a dreadful solution in comparison to this one

  • Custom User Avatar

    I think it's much better to start the loop with the index 1, because you're already calculate the index 0 and 1, sorry for bad english:)

  • Custom User Avatar

    Hmmm - If you ever find yourself using 'true' or 'false' in a ternary operator statement like this, you should probably reconsider did you need to use a ternary operator in the first place ;-)

  • Custom User Avatar

    Putting the string on the heap is important since we need writable memory. If you were to use so-called VLAs (e.g., a string allocated on the stack you could get non-writable memory). In C stack-allocated strings should be considered immutable.

    ?!

    You can absolutely have mutable strings on the stack. char buf[11]; would work just fine for both your solutions.

    Seems like you got pointers and arrays confused. A pointer to a string literal (stored in the data segment) is not a "stack-allocated string". There's a significant difference between const char *foo = "bar"; and char foo[] = "bar";.

    VLAs don't particularly factor into this, but they are absolutely mutable (seeing as they cannot be initialized, they would be useless otherwise). There is no such thing as "could" when dealing with well defined behaviours.

  • Custom User Avatar

    I think that making it static would not be a good idea.
    Putting the string on the heap is important since we need writable memory. If you were to use so-called VLAs (e.g., a string allocated on the stack you could get non-writable memory). In C stack-allocated strings should be considered immutable.
    Thus, going with malloc/calloc is a good idea here. Also freeing usually is implemented to be almost constant time, so there's no need to worry about that.
    I hope I didn't misunderstand what you meant by static :'D

  • Loading more items...