使用COUT打印对象指针值时的不同地址长度? [英] Different address length when printing object pointer value using cout?

查看:147
本文介绍了使用COUT打印对象指针值时的不同地址长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

lI'm运行64位计算机上的C ++程序。该程序动态地创建不同类的对象。当打印出使用的cout指针(未取消引用)到这些对象的值,一些对象的地址是48位,而其他对象的地址是28位!我已阅读,有些处理器目前只支持48位,但为什么那么做,我得到了28位在某些情况下的输出?这是什么意思,甚至?!

编辑:

这是一个大项目的一部分。所以,我将无法发布确切的code。但这里是基本发生。我意识到,对象需要被删除,以避免内存泄漏。

  ClassA的* PA =新ClassA的();
ClassB的* PB =新ClassB的();
COUT<< &LTClassA的对象=地址;< pA的<< ,地址ClassB的对象=的<< PB<< ENDL;

PA =新ClassA的();
PB =新ClassB的();
COUT<< &LTClassA的对象=地址;< pA的<< ,地址ClassB的对象=的<< PB<< ENDL;
 

现在,我得到的是输出如下:

  

ClassA的对象= 0x7fcda0001150,ClassB的目标地址的地址= 0x19f1160

     

ClassA的对象= 0x26c77c0,ClassB的目标地址的地址= 0x1aba400

解决方案

我想,你只是观察的对象被分配在不同的存储区域:

  1. 使用静态链接的对象通常位于数据段旁与节目code中的段,并且通常具有相对小的值。
  2. 栈上的对象通常位于与堆栈正常生长向下存储器的远端。他们,因此,通常相当大的价值。
  3. 在堆上分配的对象是数据段和栈之间的典型地方。根据如何内存分配决定分配内存,该值很可能会从小事做起,也就是说,仅仅是数据段还是相当大的,也就是上面,只是在堆栈下面。

如何系统但是真正奠定了自己的内存完全取决于系统。有些系统可能有完全不同的内存布局,有栈向上生长,等下面是一个简单的程序表现出这样的效果:

 的#include<的iostream>

诠释的main()
{
    静态INT一个(0);
    静态INT常量B(0);
    INT C(0);
    INT * D(新INT(0));
    性病::法院<< 一个=&其中;&其中; &功放; A<< '\ N'
              << B =&其中;&其中;和b<< '\ N'
              << C =<< &安培;℃下;&其中; '\ N'
              << D =<< D<< '\ N';
    删除D组;
}
 

在我的系统,这个程序打印

  A = 0x103504064
B = 0x103503f7c
C = 0x7fff5c6fca8c
D = 0x7f92204000e0
 

我想,这样的差异导致你的假设,指针是不同的,但它仅仅是格式化的值需要不同数量的数字:所有的 INT * š用于将具有相同的尺寸。

lI'm running a C++ program on a 64-bit machine. The program dynamically creates objects from different classes. When I print out the value of the pointers (not dereferencing) to those objects using cout, the address of some objects is 48-bits while the address of other objects is 28-bits! I have read that some processors currently only support 48-bits, but why then do I get an output of 28-bits in some cases? What does that even mean?!

Edit:

This is part of a big project. So, I won't be able to post the exact code. But here is what is basically happening. I do realize that the objects need to be deleted to avoid memory leaks.

ClassA *pA = new ClassA();
ClassB *pB = new ClassB();
cout << "Address of ClassA object = " << pA << " , Address of ClassB object = " << pB << endl;

pA = new ClassA();
pB = new ClassB();
cout << "Address of ClassA object = " << pA << " , Address of ClassB object = " << pB << endl;

Now the output that I get is the following:

Address of ClassA object = 0x7fcda0001150 , Address of ClassB object = 0x19f1160

Address of ClassA object = 0x26c77c0 , Address of ClassB object = 0x1aba400

解决方案

I guess, you just observed that objects are allocated in different memory regions:

  1. Objects with static linkage are generally located in the data segment next to the segment with the program code and typically have relatively small values.
  2. Objects on the stack are typically located at the far end of the memory with the stack normally growing downwards. They have, thus, normally fairly large values.
  3. Objects allocated on the heap are typically somewhere between the data segment and the stack. Depending on how the memory allocation decides to allocate memory, the value are likely to start small, i.e., just above the data segment or fairly large, i.e., just below the stack.

How systems really lay out their memory is entirely up to the system, however. Some systems may have entirely different memory layouts, have stacks growing upwards, etc. Here is a simple program demonstrating this effect:

#include <iostream>

int main() 
{
    static int       a(0);
    static int const b(0);
    int              c(0);
    int*             d(new int(0));
    std::cout << "a=" << &a << '\n'
              << "b=" << &b << '\n'
              << "c=" << &c << '\n'
              << "d=" << d  << '\n';
    delete d;
}

On my system, this program prints

a=0x103504064
b=0x103503f7c
c=0x7fff5c6fca8c
d=0x7f92204000e0

I guess, something like this difference led you to the assumption that pointers are different but it is just that the value formatted require a different number of digits: all int*s used will have the same size.

这篇关于使用COUT打印对象指针值时的不同地址长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆