简体   繁体   中英

memory cells and c++ pointers

I am currently wondering something about pointers in 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.

When we say that size of one memory cell is 1 Byte, I would need 4 cells for one Integer (4x8-Bit = 32 Bit). So let's say I have a pointer-address like: 0x613c20 for one Integer ( 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? I would stil get the same value, since I am still within one of the 4 cells?

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++ ;

I was inside my debugger and manipulated the memory address of an Integer by just adding a +1, like:

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?

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).

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)). 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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