Is it legal to use memset(…, 0, …) on an array of doubles?

前端 未结 7 1492
悲哀的现实
悲哀的现实 2020-12-09 07:37

Is it legal to zero the memory of an array of doubles (using memset(…, 0, …)) or struct containing doubles?

The question implies two different things:

7条回答
  •  庸人自扰
    2020-12-09 08:16

    As Matteo Italia says, that’s legal according to the standard, but I wouldn’t use it. Something like

    double *p = V, *last = V + N;  // N is count
    while (p != last) *(p++) = 0;
    

    is at least twice faster.

提交回复
热议问题