Different memory address of the same variable

Why is the address of the two kdifferent, as shown in the output of the following code?

#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;
}

Input: 1 1 1 1 1 1

Conclusion: Address k = -1074414672 0 address of the final k = 1072693248

+4
source share
3 answers

When I create using clang ++ (with as many warnings as I could), I get the following warnings:

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.

, undefined, .

+5

k, k.

. .

k, , .

; .

+2
  • %p
  • abs() , int, .

:

printf("%d address of final k=%d\n",(int)abs(k),&k);
+1

Source: https://habr.com/ru/post/1531630/


All Articles