Is memcpy of a pointer the same as assignment?

前端 未结 9 1048
你的背包
你的背包 2020-12-03 16:23

Introduction: This question is part of my collection of C and C++ (and C/C++ common subset) questions regarding the cases where pointers object with strictly ide

9条回答
  •  感情败类
    2020-12-03 17:20

    I say no, without resorting to the UB tarpit. From the following code:

    extern int f(int x[3], int y[4]);
    
    ....
        int   a[7];
        return f(a, a) + f(a+4, a+3);
    ...
    

    The C standard should not prevent me from writing a compiler which performs bounds checking; there are several available. A bounds checking compiler would have to fatten the pointers by augmenting them with bounds information (*). So when we get to f():

    ....
        if (x == y) {
    ....
    

    F() would be interested in the C notion of equality, that is do they point at the same location, not do they have identical types. If you aren’t happy with this, suppose f() called g(int *s, int *t), and it contained a similar test. The compiler would perform the comparison without comparing the fat.

    The pointer size sizeof(int *), would have to include the fat, so memcmp of two pointers would compare it as well, thus providing a different result from the compare.

    • = Yes, you could store such info in a dynamic associative array; which could result in the program aborting because of resource shortfalls, and may introduce tracking problems with memcpy, alloc & free.

    PS: should we introduce a new tag for navel gazing?

提交回复
热议问题