1. 11

I was actually surprised that compilers will get rid of the memset call.

I can see how this might provide a performance boost, but I’d rather get a warning (that I can suppress) when memset could be dropped.

  1.  

  2. 2

    Sadly it seems to happen if you make your own functions too, so it possibly happens with any case where the compiler thinks that that buffer won’t be used anymore.

    I see that the article provides a solution at the end. But it seems to me a warning should be made by the compiler, instead of having to adapt all functions to prevent this from happening. Or be able to disable this specific optimization.

    1. 1

      Yeah, after some reading it seems to be due to dead store optimization.

      If the memory is not accessed after you store something there (i.e., memset), the compiler will reorder the instructions and the dead store is removed.

      There was some interesting discussion on GCC’s bugzilla about this. Is that a bug or not? I think it is.

      After some Googling I found the explicit_bzero function that @kristapsdz mentioned, but the glibc version. The trick it uses is to add asm volatile ("" ::: "memory") after the memset call. That prevents any stores prior to that point to be reordered.

    2. 2

      The tag should be C++, not C.

      1. 2

        Do you know how I can edit the tags? Or do the mods have to? Couldn’t find the option :(

        1. 1

          Apparently you click suggest