简体   繁体   中英

Initializing itimerspec struct - is memset a good choice?

Years ago, I asked a question about how to initialize a timespec struct to a zeroed state (nsec and sec equal to zero) with the minimum lines of code.

Many of the responses told me not to use memset , as this was not cross platform compatible and numerous other issues could occur. As a result, I adopted the following way:

struct timeval freshStruct = struct timeval){0};

Years later, I now want to initialize a itimerspec struct, which has within it two timespec structs. One way is for me to do the following:

struct itimerspec alarmTimeFormatted;
alarmTimeFormatted.it_interval = (struct timespec){0};
alarmTimeFormatted.it_value = (struct timespec){0};

I see other answers here on StackOverflow which show using memset to initialize the struct itimerspec this in one line.

So now I'm curious -- is there something different about the itimerspec struct, or are these just bad answers? Is there any eloquent way to initialize the outer / inner structs with just one line?

Thanks

It's still bad practice. Erik gave a possible solution in the comments, but usually in C++ we use constructors : itimerspec::itimerspec() : it_interval { 0 }, it_value { 0 } { /***/ }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM