简体   繁体   English

内存单元和c ++指针

[英]memory cells and c++ pointers

I am currently wondering something about pointers in c++. 我目前想知道有关c ++中的指针的一些信息。 A pointer returns me the memory address of variable for instance. 例如,一个指针向我返回变量的内存地址。 So if we say, we want a memory address of a Integer, we normally have a 32-Bit length. 所以如果说,我们想要一个整数的内存地址,通常我们有32位长度。

When we say that size of one memory cell is 1 Byte, I would need 4 cells for one Integer (4x8-Bit = 32 Bit). 当我们说一个存储单元的大小为1字节时,我需要4个存储单元来存储一个整数(4x8位= 32位)。 So let's say I have a pointer-address like: 0x613c20 for one Integer ( int a = 10; ). 假设我有一个指针地址,如: 0x613c20表示一个整数( int a = 10; )。

When this address points to one of the 4 cells in the whole memory, why do I get another value when I increase the cell by just +1? 当此地址指向整个内存中的4个单元之一时,为什么我将单元格仅增加+1时又得到另一个值? I would stil get the same value, since I am still within one of the 4 cells? 我仍然会得到相同的值,因为我仍处于4个单元格之一内?

Thank you very much. 非常感谢你。

EDIT: 编辑:

I think I was not so clear in the first posting: I didn't mean that I increased the pointer by pointer++ ; 我想我在第一篇文章中不太清楚:我并不是说我用pointer++增加了指针;

I was inside my debugger and manipulated the memory address of an Integer by just adding a +1, like: 我在调试器内部,仅通过添加+1来操纵Integer的内存地址,例如:

0x1 -> 0x2. 0x1-> 0x2。

So that means that: 因此,这意味着:

int a = 10;
*int b = &a; // 0x1

*0x1 -> 10
*0x2 -> 1203104

When the Integer size is 4 byte long, why do I not get 10 as well here? 当Integer大小为4字节长时,为什么在这里我也不能得到10个字节?

When you increment/decrement a pointer, the memory address gets "increased/decreased" by the length of the object type the pointer is declared as (eg it would point to the next/previous object of the same type if it would be a sequence of objects of the same type) 当您递增/递减指针时,内存地址将被声明为该指针的对象类型的长度“增加/减少”(例如,如果它是一个序列,它将指向相同类型的下一个/上一个对象相同类型的对象)

A little example: 一个小例子:

#include <iostream>
using std::cout;
using std::endl;

int main()
{
    int i = 5;
    int* p_i = &i;

    short c = 'a';
    short* p_c = &c;

    cout << "\npointer to int (4 bytes)\n";

    cout << "adress: " << p_i << "   ";
    cout << "value: " << *p_i << endl;
    ++p_i;
    cout << "adress: " << p_i << "   ";
    cout << "value: " << *p_i << endl;
    ++p_i;
    cout << "adress: " << p_i << "   ";
    cout << "value: " << *p_i << endl;
    ++p_i;
    cout << "adress: " << p_i << "   ";
    cout << "value: " << *p_i << endl;
    ++p_i;

    cout << "\npointer to short (2 bytes)\n";

    cout <<  "adress: " << p_c << "   ";
    cout <<  "value: " << *p_c << endl;
    ++p_c;
    cout <<  "adress: " << p_c << "   ";
    cout <<  "value: " << *p_c << endl;
    ++p_c;
    cout <<  "adress: " << p_c << "   ";
    cout <<  "value: " << *p_c << endl;
    ++p_c;
    cout <<  "adress: " << p_c << "   ";
    cout <<  "value: " << *p_c << endl;
    ++p_c;

    return 0;
}

above code outputs: 以上代码输出:

pointer to int (4 bytes)
adress: 0x28ff24   value: 5
adress: 0x28ff28   value: 2686754
adress: 0x28ff2c   value: 2686764
adress: 0x28ff30   value: 2686800

pointer to short (2 bytes)
adress: 0x28ff22   value: 97
adress: 0x28ff24   value: 5
adress: 0x28ff26   value: 0
adress: 0x28ff28   value: -216

As you can see, the pointer to short gets "increased" in steps of two as a short occupies 2 bytes of memory as opposed to int, which is longer and takes up four bytes (on my system, that is). 如您所见,short的指针以2的步长“增加”,因为short占用2个字节的内存,而int则更长,占用4个字节(在我的系统上)。

I would suggest referring to documentation regarding lengths of data types (they can vary between systems and compilers) and pointers for further information. 我建议参考有关数据类型长度的文档(它们在系统和编译器之间可能会有所不同)和指针,以获取更多信息。

What you are doing is pointer arithmetic. 您正在做的是指针运算。 The result of increasing the pointer (not the value) by one depends on the type of the pointer. 将指针(不是值)加一的结果取决于指针的类型。 In your example the actual address is not incremented by 1, but by 4 (or sizeof(int)). 在您的示例中,实际地址不会增加1,而是增加4(或sizeof(int))。 Also, there is no such a thing as 而且,没有这样的事情

I would stil get the same value, since I am still within one of the 4 cells 我仍然会得到相同的值,因为我仍然处于4个单元格之一内

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

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