zeroing out memory

后端 未结 12 2039
無奈伤痛
無奈伤痛 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:11

    The worst that can happen by not doing it is that you write some data in character by character and later interpret it as a string (and you didn't write a null terminator). Or you end up failing to realise a section of it was uninitialised and read it as though it were valid data. Basically: all sorts of nastiness.

    Memset should be fine (provided you correct the sizeof typo :-)). I prefer that to your first example because I think it's clearer.

    For dynamically allocated memory, I use calloc rather than malloc and memset.

提交回复
热议问题