Memset Definition and use

后端 未结 6 672
失恋的感觉
失恋的感觉 2020-12-12 16:24

What\'s the usefulness of the function memset()?.

Definition: Sets the first num bytes of the block of memory pointed by

6条回答
  •  Happy的楠姐
    2020-12-12 17:01

    memset is a common way to set a memory region to 0 regardless of the data type. One can say that memset doesn't care about the data type and just sets all bytes to zero.

    IMHO in C++ one should avoid doing memset when possible since it circumvents the type safety that C++ provides, instead one should use constructor or initialization as means of initializing. memset done on a class instance may also destroy something unintentionally:

    e.g.

    class A
    {
    public:
      shared_ptr _p;
    };
    

    a memset on an instance of the above would not do a reference counter decrement properly.

提交回复
热议问题