C ++指针访问类似于汇编 [英] C++ Pointer access similar to assembly

查看:117
本文介绍了C ++指针访问类似于汇编的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时我看到不同的数组访问风格在C + +,并认为它可能是相对于一个程序集寻址模式:



C ++:

  int * aa = new int [2]; 
0 [aa] = 15; // a a different different aa [0]
1 [aa] = 15;
aa [0] = 15;
aa [1] = 15;
printf(%d%d \\\
,aa [0],aa [1]);

装配:

  __ asm 
{
mov aa [0],ebx
mov aa [1],eax
mov 0 [aa],ebx
mov 1 [aa],eax
}

这个C ++数组访问符号a

当我尝试 [aa] 1 = 5;

/ code>,编译器给出




  • 未找到aa属性,

  • 缺少';'之前'常量'

  • left operand必须为l-value。



    当我尝试指针运算时,



    *(aa + 1)= 0 //没有错误



    (aa + 0)= 0 //没有错误:)






谢谢。

解决方案

这是一个众所周知的C和C ++的怪癖,他们并不真正关心表达式的哪一半在方括号 [] 。他们都评价相同的等效表达式:

  *(0 + aa)= 15; 


Sometimes i see different array-access styles in C++ and thought it could be relative to an assembly addressing mode:

C++:

int * aa=new int[2];
0[aa]=15; //a little different than aa[0]
1[aa]=15;
aa[0]=15;
aa[1]=15;
printf("%d %d \n",aa[0],aa[1]);

Assembly:

__asm
{
    mov aa[0],ebx
    mov aa[1],eax
    mov 0[aa],ebx
    mov 1[aa],eax
}

Is this C++ array-access notation a standard and if yes, was it derived from an assembly addressing mode?

When i try [aa]1=5; , compiler gives

  • "aa attribute not found",
  • "missing ';' before 'constant'"
  • "left operand must be l-value".

    //When i try pointer arithmetic,

    *(aa+1)=0//gives no error

    *(aa+0)=0//gives no error :)

Is this rule same for the operator [] overloading?

MSVC++ 2010

Thank you.

解决方案

This is a well-known quirk of C and C++, they don't really care which half of an expression goes in the square brackets []. They both evaluate to the same equivalent expression:

*(0+aa)=15;

这篇关于C ++指针访问类似于汇编的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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