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

前端 未结 7 1481
悲哀的现实
悲哀的现实 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:27

    It’s “legal” to use memset. The issue is whether it produces a bit pattern where array[x] == 0.0 is true. While the basic C standard doesn’t require that to be true, I’d be interested in hearing examples where it isn’t!

    It appears that setting to zero via memset is equivalent to assigning 0.0 on IBM-AIX, HP-UX (PARISC), HP-UX (IA-64), Linux (IA-64, I think).

    Here is a trivial test code:

    double dFloat1 = 0.0;
    double dFloat2 = 111111.1111111;
    
    memset(&dFloat2, 0, sizeof(dFloat2));
    
    if (dFloat1 == dFloat2) {
        fprintf(stdout, "memset appears to be equivalent to = 0.0\n");
    } else {
        fprintf(stdout, "memset is NOT equivalent to = 0.0\n");
    }
    
    0 讨论(0)
提交回复
热议问题