在这种情况下,C ++中* pointer + 2的值是多少?

时间:2016-03-02 22:42:14

标签: c++ pointers

给定变量long *ptr_long;

以下:

Memory Content

操作的价值是什么* ptr_long + 2?还怎么样?

我尝试过以下操作:* ptr现在指向0x00,其中包含内容' 0x04'。现在,你接受并增加2(4个字节,因为它是一个长类型?)。我不确定从哪里去。

2 个答案:

答案 0 :(得分:3)

请注意

*pointer + 2

被解析为

(*pointer) + 2

而不是

*(pointer + 2),

所以这意味着“取消引用指针,然后在结果中添加两个”,而不是“前进两个对象,然后取消引用你得到的指针”。在这种情况下,你应该得到6(0x04 + 2)。

如果你添加括号来获取*(pointer + 2),假设每个long是4个字节(我怀疑你只给了16个字节的内存),你会得到0x0c,因为pointer + 2表示“向前跳过2 * sizeof(long)个字节。”

答案 1 :(得分:0)

答案取决于您的系统, 在大多数情况下,答案是:

*ptr + 2 = 0x6 
*(ptr + 2) = 0xc 

对于32位系统,长类型编码为4个字节,对于64位系统,长编码为8个字节,

根据您的系统和CPU,您的内存状态可能如下所示:

0x00 : 04 00 00 00
0x04 : 0d 00 00 00
0x08 : 0c 00 00 00
0x0c : 0a 00 00 00

or

0x00 : 00 00 00 04
0x04 : 00 00 00 0d
0x08 : 00 00 00 0c
0x0c : 00 00 00 0d  

或(对于64位系统):

0x00 : 04 00 00 00 00 00 00 00
0x08 : 0d 00 00 00 00 00 00 00
0x10 : 0c 00 00 00 00 00 00 00
0x18 : 0a 00 00 00 00 00 00 00

您的示例更接近32位系统情况, 在这种情况下:

# if ptr is a long pointer :
*ptr + 2 = 0x6      // the ( content of the long value at the adress 0x00 ) + 2 = 0x04 + 0x02
*(ptr + 2) = 0xc    // the content of adress 0x10    ( 0x00 + 2 as long size ) = 0x00 + 0x10 ( 16 in decimal)

很高兴知道: *(ptr + 2)= * ptr [2]

ptr   -->0x00 : 04 00 00 00 
         0x04 : 0d 00 00 00 
ptr+2 -->0x08 : 0c 00 00 00  
         0x0C : 0a 00 00 00 

反之:

如果ptr是char指针:

*ptr + 2 = 0x6      // the ( content of the char/byte value at the adress 0x00 ) + 2
*(ptr + 2) = 0x0    // the content of adress 0x02   ( 0x00 + 2 as char size ) = 0x00 + 0x02 ( 2 in decimal)

                ptr    ptr+2=0x02
                  |     |
         0x00 : (04)00(00)00 
         0x04 :  0d 00 00 00 
         0x08 :  0c 00 00 00  
         0x0C :  0a 00 00 00 

在这种情况下: 例如,如果一个long(long *)ptr2类型的新指针由ptr + 2值初始化, 在32位中,由ptr2指向的地址的内容将是:00 00 0d 00

         0x00 : 04 00 (00 00 
         0x04 : 0d 00) 00 00 
         0x08 : 0c 00 00 00 
         0x0C : 0a 00 00 00 

在64位中,ptr2指向的地址内容为:00 00 00 00 00 00 0d 00

0x00 : 04 00 (00 00 00 00 00 00
0x08 : 0d 00 ) 00 00 00 00 00 00
0x10 : 0c 00 00 00 00 00 00 00 
0x18 : 0a 00 00 00 00 00 00 00

在两种情况下,值都相同:
* ptr2 + 2 = 0x0d02 //(地址0x02的长值内容)+ 2

但在32位系统中:
*(ptr2 + 2)= 0x0a00 //地址内容0x0a(0x02 + 2长(4字节)大小)= 0x02 + 0x08                         //值为0x0a00 =(00 00 00 0a)

               adress 0x0a
                       |
         0x08 : 0c 00(00 00
         0x0C : 0a 00)00 00

但在64位系统中:
*(ptr2 + 2)= 0x12 //地址内容0x12(0x02 + 2长(8字节)大小)= 0x02 + 0x10                       //值为0x0a00

               adress 0x12
                       |
         0x10 : 0c 00(00 00 00 00 00 00 
         0x18 : 0a 00)00 00 00 00 00 00          

我劝你编译并运行跟随C程序:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void long_hexa_print( long *p )
{
 char *ptr = (char *)p;
 int size=sizeof(*p);
 printf ("0x%x : ", p  );
 for ( int i=0; i<size; i++){printf ( "0x%x " ,  *(ptr + i) );}
 printf ("\n");
}

int main(void)
{
   // char * string = "REVERSE";
    //reverse(string);

    int j=4;
    long *long_ptr =  (long *)malloc((j*sizeof(long))+1);
    char *ptr = (char *)long_ptr;

    char *  char_ptr2;
    int *   int_ptr2;
    long *  long_ptr2;


     long_ptr[0]=0x4;  // 4
     long_ptr[1]=0xb;  // 11
     long_ptr[2]=0xc;  // 12
     long_ptr[3]=0xa;  // 10

    char_ptr2 = (char *)( ptr + 2 );  // or (char *) ptr[2] 
    int_ptr2  = (int *)( ptr + 2 );
    long_ptr2 = (long *)( ptr + 2 );

    printf ("\n=== Sizes of types ====\n\n");
    printf ("Size of char = %d\n", sizeof(char));
    printf ("Size of long = %d\n\n", sizeof(long));

    printf ("\n=== Memory status ====\n\n");
    long_hexa_print( long_ptr );
    long_hexa_print( long_ptr + 1);
    long_hexa_print( long_ptr + 2);
    long_hexa_print( long_ptr + 3);
    long_hexa_print( long_ptr + 4);

    printf ("\n=== Values of pointers ====\n\n");
    printf ("long_ptr     = 0x%x\n",  long_ptr );
    printf ("long_ptr + 1 = 0x%x\n",  long_ptr + 1 );
    printf ("long_ptr + 2 = 0x%x\n\n",  long_ptr + 2 );

    printf ("* long_ptr + 2  = 0x%x\n",  * long_ptr + 2 );
    printf ("*(long_ptr + 2) = 0x%x\n",  *(long_ptr + 2) );
    printf ("  long_ptr[2]   = 0x%x\n\n",  long_ptr[2] );


    printf ("&long_ptr[0] = 0x%x\n", & long_ptr[0] );
    printf ("&long_ptr[1] = 0x%x\n", & long_ptr[1] );
    printf ("&long_ptr[2] = 0x%x\n\n", & long_ptr[2] );

    printf ("\n##### char *ptr = (char *)long_ptr; ##### \n");
    for ( int i=0; i<=7; i++){printf ( "ptr+%d     = 0x%x\n",  i, *(ptr + i) );}


    printf ("\n=== Values of contents ====\n\n");
    printf ("*long_ptr    = 0x%x\n", *long_ptr );
    printf ("*(long_ptr) = 0x%x\n\n", *(long_ptr) );
    printf ("long_ptr[0]  = 0x%x\n",  long_ptr[0] );
    printf ("long_ptr[1]  = 0x%x\n",  long_ptr[1] );
    printf ("long_ptr[2]  = 0x%x\n\n",  long_ptr[2] );
    printf ("*(long_ptr+0) = 0x%x\n", *(long_ptr+0) );
    printf ("*(long_ptr+1) = 0x%x\n", *(long_ptr+1) );
    printf ("*(long_ptr+2) = 0x%x\n\n", *(long_ptr+2) );

    printf ("char_ptr2 = 0x%x\n", *char_ptr2 );
    printf ("int_ptr2  = 0x%x\n", *int_ptr2 );
    printf ("long_ptr2 = 0x%x\n\n", *long_ptr2 );

    printf ("*( ptr)   = 0x%x\n", *( ptr));
    printf ("*( ptr+1) = 0x%x\n", *( ptr + 1));
    printf ("*( ptr+2) = 0x%x\n", *( ptr + 2));
    printf ("*( ptr+3) = 0x%x\n", *( ptr + 3));
    printf ("*( ptr+4) = 0x%x\n", *( ptr + 4));
    printf ("*( ptr+5) = 0x%x\n", *( ptr + 5));
    printf ("*( ptr+6) = 0x%x\n", *( ptr + 6));
    printf ("*( ptr+7) = 0x%x\n", *( ptr + 7));
    printf ("*( ptr+8) = 0x%x\n", *( ptr + 8));
    printf ("*( ptr+9) = 0x%x\n\n", *( ptr + 9));

    printf ("(long *)( ptr + 4) = 0x%x\n", *( ptr + 4));
    printf ("(long *)( ptr + 4) = %d\n\n", *( ptr + 4));

    printf ("(long *)( ptr + 8) = 0x%x\n", (long *)( ptr + 8));
    printf ("(long *)( ptr + 8) = %d\n\n", (long *)( ptr + 8));

    return 0;
}

结果如下所示:

=== Sizes of types ====

Size of char = 1
Size of long = 8


=== Memory status ====

0x10350 : 0x4 0x0 0x0 0x0 0x0 0x0 0x0 0x0
0x10358 : 0xb 0x0 0x0 0x0 0x0 0x0 0x0 0x0
0x10360 : 0xc 0x0 0x0 0x0 0x0 0x0 0x0 0x0
0x10368 : 0xa 0x0 0x0 0x0 0x0 0x0 0x0 0x0
0x10370 : 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0

=== Values of pointers ====

long_ptr     = 0x10350
long_ptr + 1 = 0x10358
long_ptr + 2 = 0x10360

* long_ptr + 2  = 0x6
*(long_ptr + 2) = 0xc
  long_ptr[2]   = 0xc

&long_ptr[0] = 0x10350
&long_ptr[1] = 0x10358
&long_ptr[2] = 0x10360


##### char *ptr = (char *)long_ptr; #####
ptr+0     = 0x4
ptr+1     = 0x0
ptr+2     = 0x0
ptr+3     = 0x0
ptr+4     = 0x0
ptr+5     = 0x0
ptr+6     = 0x0
ptr+7     = 0x0

=== Values of contents ====

*long_ptr    = 0x4
*(long_ptr) = 0x4

long_ptr[0]  = 0x4
long_ptr[1]  = 0xb
long_ptr[2]  = 0xc

*(long_ptr+0) = 0x4
*(long_ptr+1) = 0xb
*(long_ptr+2) = 0xc

char_ptr2 = 0x0
int_ptr2  = 0x0
long_ptr2 = 0x0

*( ptr)   = 0x4
*( ptr+1) = 0x0
*( ptr+2) = 0x0
*( ptr+3) = 0x0
*( ptr+4) = 0x0
*( ptr+5) = 0x0
*( ptr+6) = 0x0
*( ptr+7) = 0x0
*( ptr+8) = 0xb
*( ptr+9) = 0x0

(long *)( ptr + 4) = 0x0
(long *)( ptr + 4) = 0

(long *)( ptr + 8) = 0x10358
(long *)( ptr + 8) = 66392

我希望这能帮助你更好地理解指针