简体   繁体   English

同一变量的不同内存地址

[英]Different memory address of same variable

Why is the address of two k different as shown in output of following code? 为什么两个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;
}

Input: 1 1 1 1 1 1 输入:1 1 1 1 1 1

Output: Address of k=-1074414672 0 address of final k=1072693248 输出:地址k = -1074414672 0地址最终k = 1072693248

When I build with clang++ (with as many warnings enabled as I could) I get these warnings: 当我使用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);}
^

This is because you don't include <cstdlib> which declares the integer version of abs . 这是因为你没有包含<cstdlib> ,它声明了abs的整数版本。 Without that include the compiler have to guess which function it should use, and it seems it chooses poorly as it picks one of the floating point variants from <cmath> . 如果没有这个包括编译器必须猜测它应该使用哪个函数,并且它似乎选择不好,因为它从<cmath>选择一个浮点变体。 This leads to you overwriting the next argument in printf call. 这会导致您覆盖printf调用中的下一个参数。

When building a program, I advise everyone to enable as many warnings as possible, they usually point out things like undefined behavior as in this case. 在构建程序时,我建议每个人尽可能多地发出警告,他们通常会指出像这种情况一样的未定义行为。

Without taking the address of the k variable, the compiler is allowed to use registers to hold the value of k . 如果不取k变量的地址,则允许编译器使用寄存器来保存k的值。

With most processors, registers do not have a physical address. 对于大多数处理器,寄存器没有物理地址。 They are not in the CPU address space. 它们不在CPU地址空间中。

By printing the address of the k variable, you are telling the compiler to either store the variable in memory or for the compiler to generate an address. 通过打印k变量的地址,您告诉编译器要么将变量存储在内存中,要么让编译器生成地址。

Not all variables need to be stored in addressable memory; 并非所有变量都需要存储在可寻址存储器中; they can be store in registers. 它们可以存储在寄存器中。

  1. You should print pointers with %p 你应该用%p打印指针
  2. abs() returns floating point number, casting it to int before printing solves the problem abs()返回浮点数,在打印前将其转换为int解决问题

The line would be correct with: 这条线是正确的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM