zeroing out memory

后端 未结 12 2016
無奈伤痛
無奈伤痛 2021-01-31 09:52

gcc 4.4.4 C89

I am just wondering what most C programmers do when they want to zero out memory.

For example, I have a buffer of 1024 bytes. Sometimes I do this:<

相关标签:
12条回答
  • 2021-01-31 10:03

    I vastly prefer

    char buffer[1024] = { 0 };
    

    It's shorter, easier to read, and less error-prone. Only use memset on dynamically-allocated buffers, and then prefer calloc.

    0 讨论(0)
  • 2021-01-31 10:07

    I prefer using memset to clear a chunk of memory, especially when working with strings. I want to know without a doubt that there will be a null delimiter after my string. Yes, I know you can append a \0 on the end of each string and some functions do this for you, but I want no doubt that this has taken place.

    A function could fail when using your buffer, and the buffer remains unchanged. Would you rather have a buffer of unknown garbage, or nothing?

    0 讨论(0)
  • 2021-01-31 10:07

    This post has been heavily edited to make it correct. Many thanks to Tyler McHenery for pointing out what I missed.

    char buffer[1024] = {0};
    

    Will set the first char in the buffer to null, and the compiler will then expand all non-initialized chars to 0 too. In such a case it seems that the differences between the two techniques boil down to whether the compiler generates more optimized code for array initialization or whether memset is optimized faster than the generated compiled code.

    Previously I stated:

    char buffer[1024] = {0};

    Will set the first char in the buffer to null. That technique is commonly used for null terminated strings, as all data past the first null is ignored by subsequent (non-buggy) functions that handle null terminated strings.

    Which is not quite true. Sorry for the miscommunication, and thanks again for the corrections.

    0 讨论(0)
  • 2021-01-31 10:09

    When you define char buffer[1024] without initializing, you're going to get undefined data in it. For instance, Visual C++ in debug mode will initialize with 0xcd. In Release mode, it will simply allocate the memory and not care what happens to be in that block from previous use.

    Also, your examples demonstrate runtime vs. compile time initialization. If your char buffer[1024] = { 0 } is a global or static declaration, it will be stored in the binary's data segment with its initialized data, thus increasing your binary size by about 1024 bytes (in this case). If the definition is in a function, it's stored on the stack and is allocated at runtime and not stored in the binary. If you provide an initializer in this case, the initializer is stored in the binary and an equivalent of a memcpy() is done to initialize buffer at runtime.

    Hopefully, this helps you decide which method works best for you.

    0 讨论(0)
  • 2021-01-31 10:10

    One of the things that can happen if you don't initialize is that you run the risk of leaking sensitive information.

    Uninitialized memory may have something sensitive in it from a previous use of that memory. Maybe a password or crypto key or part of a private email. Your code may later transmit that buffer or struct somewhere, or write it to disk, and if you only partially filled it the rest of it still contains those previous contents. Certain secure systems require zeroizing buffers when an address space can contain sensitive information.

    0 讨论(0)
  • 2021-01-31 10:10

    yup, calloc() method defined in stdlib.h allocates memory initialized with zeros.

    0 讨论(0)
提交回复
热议问题