将unsigned int添加到unsigned int *

时间:2017-02-17 14:40:17

标签: c++ c pointer-arithmetic unsigned-integer

我的开发环境包含用于ARM OMAP Sitata的g ++交叉编译器。当将unsigned int添加到unsigned int *时,我发现了一个简单的指针算法的不寻常的细微差别,如下所示:

public class ExpandableListAdapter extends BaseExpandableListAdapter {
    private Context _context;
    private List<String> _listDataHeader; // header titles
    // child data in format of header title, child title
    private List<Integer> _group;

    private DrawerLayout _drawer;

    private static final int[] EMPTY_STATE_SET = {};
    private static final int[] GROUP_EXPANDED_STATE_SET =
            {android.R.attr.state_expanded};
    private static final int[][] GROUP_STATE_SETS = {
            EMPTY_STATE_SET, // 0
            GROUP_EXPANDED_STATE_SET // 1
    };

    private HashMap<String, List<String>> _listDataChild;
    ExpandableListView expandList;
    public ExpandableListAdapter(Context context, List<String> listDataHeader, List<Integer> group,
                                 HashMap<String, List<String>> listChildData, DrawerLayout drawer) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
        this._group=group;
        this._drawer=drawer;

    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosititon);
    }
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {

        final String childText = (String) getChild(groupPosition, childPosition);
        int childpositionaa= (int) getChildId(groupPosition,childPosition);
        int gropuposi= (int) getGroupId(groupPosition);
        Log.i("groupposi", String.valueOf(+groupPosition));
        Log.i("groupposicc", String.valueOf(+childPosition));
        Log.i("groupposiccee", String.valueOf(+childpositionaa));
        int child3position=(int)getChildId(1,childPosition);
        Log.i("groupposiccee33", String.valueOf(+child3position));

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item, null);
        }
Log.i("childposition",""+childPosition);
        Typeface roboto = Typeface.createFromAsset(_context.getAssets(),
                "font/Roboto-Light.ttf");
        AppCompatTextView txtListChild = (AppCompatTextView) convertView
                .findViewById(R.id.lblListItem);
        txtListChild.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:

                        _drawer.closeDrawer(GravityCompat.START);
                        switch (groupPosition) {
                            case 0:
                                break;
                            case 1:
                                switch (childPosition){
                                    case 0:
                                        BaseActivity.dointent(_context.getApplicationContext(),Handled_follow.class);

                                        break;
                                    case 1:
                                        BaseActivity.dointent(_context.getApplicationContext(), Unhanded_follow.class);
                                        break;
                                    case 2:
                                        BaseActivity.dointent(_context.getApplicationContext(), Navi_recyview.class);
                                                break;
                                    case 3:
                                        BaseActivity.dointent(_context.getApplicationContext(), Unhandled_appoint.class);
                                                                          break;
                                    case 4:
                                        BaseActivity.dointent(_context.getApplicationContext(), Cold_calls.class);
                                                                              break;
                                }
                                break;
                        }                        
                        break;
                }

                return false;
            }
        });
        AppCompatTextView txtchildcount= (AppCompatTextView) convertView
                .findViewById(R.id.lblistnavcount);
        txtListChild.setText(childText);
        txtListChild.setTypeface(roboto);


        txtchildcount.setText(myList.get(childpositionaa));
        if(gropuposi==2){
            if(childpositionaa==0){
                txtchildcount.setText(myList.get(5));

            }
            else{
                txtchildcount.setText(myList.get(4));
            }
            txtchildcount.setText(myList.get(childpositionaa+5));
        }
        else{
            txtchildcount.setText(myList.get(childpositionaa));
        }
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return this._listDataHeader.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        int headerimg = _group.get(groupPosition);
        Log.i("groupposigroupview", String.valueOf(+groupPosition));
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group, null);
        }
        AppCompatTextView lblListHeader = (AppCompatTextView) convertView
                .findViewById(R.id.lblListHeader);
        View ind = convertView.findViewById( R.id.indicicon);

        if( ind != null ) {
            ImageView indicator = (ImageView)ind;
            if( getChildrenCount( groupPosition ) == 0 ) {
                indicator.setVisibility( View.INVISIBLE );
            } else {
                indicator.setVisibility(View.VISIBLE);
                if (isExpanded) {
                    lblListHeader.setTextColor(_context.getResources().getColor(R.color.colorPrimary));
                    indicator.setImageResource(R.drawable.ic_expand_less_black_18dp);
                } else {
                    lblListHeader.setTextColor(_context.getResources().getColor(R.color.colorlisthead));

                    indicator.setImageResource(R.drawable.ic_expand_more_black_18dp);
                }
            }
        }
        Typeface roboto = Typeface.createFromAsset(_context.getAssets(),
                "font/Roboto-Regular.ttf"); //use this.getAssets if you are calling from an Activity


        lblListHeader.setTypeface(roboto);
        lblListHeader.setText(headerTitle);
        ImageView imgsListHeader = (ImageView) convertView
                .findViewById(R.id.imagview);

        imgsListHeader.setImageResource(headerimg);
lblListHeader.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        switch (motionEvent.getAction()) {
            case MotionEvent.ACTION_DOWN:

                switch (groupPosition) {
                    case 0:
                        _drawer.closeDrawer(GravityCompat.START);
                        BaseActivity.dointent(_context.getApplicationContext(),Dashboard.class);
                        break;
                    case 1:

                        break;
                    case 2:

                    case 3:
                        _drawer.closeDrawer(GravityCompat.START);

                        break;
                    case 4:
                        _drawer.closeDrawer(GravityCompat.START);

                        break;
                }
                break;
        }

        return false;
    }
});
        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {


        return true;
    }

}
`

只是尝试将(unsigned int)添加到(unsigned int *)

unsigned int* dst_base_addr;
unsigned int* dst_addr; 
unsigned int dst_offset;

上述内容并未被解释为人们可能会天真地想到,但实际上会产生以下等效结果

dst_addr = dst_base_addr + dst_offset;

补救措施当然是按照以下方式进行适当的类型转换

dst_addr = (unsigned int*)((unsigned int)dst_base_addr + (dst_offset << 2));

问题:为什么在这种情况下甚至需要正确的类型转换?

1 个答案:

答案 0 :(得分:7)

  

上述内容并未被解释为人们可能会天真地想到,但实际上会产生以下等效结果

C中的指针算法(可能是C ++)是以指向的东西的大小为单位完成的。如果将n添加到jQuery(document).ready(function ($) { $(".events_link").click(function () { var eventname = $(this).attr('id'); var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>"; jQuery.ajax({ type: 'POST', url: ajaxurl, data: { action: "all_event", eventname: eventname }, success: function (response) { $(".events-thumbs").html(response); $('.flexslider').flexslider({ animation: "slide", slideshow: "false", animationLoop: true, itemWidth: 192, itemMargin: 9, controlNav: true, directionNav: false }); } }); }); }); ,则生成的代码将添加表示n int* s所需的字节数,即如果int为32位,则可能为4 * n(大多数情况下为4个字节)理智的建筑)。

这是标准的C行为。