同一变量的不同内存地址

Jas*_*wat 4 c++ memory-management

为什么两个地址k不同,如下面的代码输出所示?

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
using namespace std;
int anu[1000000];
int calc(int a,int b,int c,int d)
{
    long long int k;
    k=(long long int)a*d*d+b*d+c;
    return k%1000000;
}
int main()
{
    int t,n,i,a,b,c,d,k;
    scanf("%d",&t);
    while(t--)
    {   
        scanf("%d %d %d %d %d",&n,&a,&b,&c,&d);
        memset(anu,0,sizeof(int)*1000000);
        anu[d]=1;
        vector<int> anu1;
        anu1.push_back(d);
        for(i=1;i<n;i++)
        {
            k=calc(a,b,c,anu1[i-1]);
            anu1.push_back(k);
            anu[k]=anu[k]?0:1;
        }
        d=0;k=0;
        printf("address of k=%d ",&k);
        for(i=0;i<1000000;i++)
        {
            if(anu[i])
                {
                if(d%2)
                k-=i;
                else
                k+=i;
                d++;
            }
        }
        printf("%d address of final k=%d\n",abs(k),&k);
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输入:1 1 1 1 1 1

输出:地址k = -1074414672 0地址最终k = 1072693248

Som*_*ude 5

当我使用clang ++(尽可能多地启用警告)构建时,我收到以下警告:

k.cpp:45:45: error: call to 'abs' is ambiguous
        printf("%d address of final k=%d\n",abs(k),&k);
                                            ^~~
/usr/local/include/c++/v1/cmath:660:1: note: candidate function
abs(float __x) _NOEXCEPT {return fabsf(__x);}
^
/usr/local/include/c++/v1/cmath:664:1: note: candidate function
abs(double __x) _NOEXCEPT {return fabs(__x);}
^
/usr/local/include/c++/v1/cmath:668:1: note: candidate function
abs(long double __x) _NOEXCEPT {return fabsl(__x);}
^

这是因为你没有包含<cstdlib>声明整数版本的abs.如果没有这个包括编译器必须猜测它应该使用哪个函数,并且它似乎选择不好,因为它从中选择一个浮点变体<cmath>.这会导致您覆盖printf调用中的下一个参数.

在构建程序时,我建议每个人尽可能多地发出警告,他们通常会指出像这种情况一样的未定义行为.