Memset Definition and use

后端 未结 6 657
失恋的感觉
失恋的感觉 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条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-12 16:44

    I guess that serv_addr is some local or global variable of some struct type -perhaps struct sockaddr- (or maybe a class).

    &serv_addr is taking the address of that variable. It is a valid address, given as first argument to memset. The second argument to memset is the byte to be used for filling (zero byte). The last argument to memset is the size, in bytes, of that memory zone to fill, which is the size of that serv_addr variable in your example.

    So this call to memset clears a global or local variable serv_addr containing some struct.

    In practice, the GCC compiler, when it is optimizing, will generate clever code for that, usually unrolling and inlining it (actually, it is often a builtin, so GCC can generate very clever code for it).

提交回复
热议问题