解决GCC警告:"取消引用类型惩罚指针将破坏严格别名规则"通过临时指针

时间:2017-04-25 07:50:11

标签: c pointers gcc strict-aliasing type-punning

在功能

public class RVAdapterStats extends RecyclerView.Adapter<RecyclerView.ViewHolder> { 
Activity activity; 
private static final String TAG = "VainLog"; 
JSONObject values;

public RVAdapterStats(Activity activity, JSONObject values){
this.activity = activity;
this.values = values;
}

@Override
public int getItemViewType(int position){
int solve = 0;
switch (position){
    case 0 : solve = 0;
        break;
    case 1 : solve = 1;
        break;
    case 2 : solve = 2;
        break;
    case 3 : solve = 3;
}
return solve;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

RecyclerView.ViewHolder vh = null;
View itemLayoutView;

switch (viewType)
{
    case 0:
        itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.stats_header, parent, false);
        vh = new ViewHolder0(itemLayoutView);
        break;
    case 1:
        itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.stats_second, parent, false);
        vh = new ViewHolder1(itemLayoutView);
        break;
    case 2: itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.stats_third, parent, false);
        vh = new ViewHolder2(itemLayoutView);
        break;
    case 3: itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.stats_fourth, parent, false);
        vh = new ViewHolder3(itemLayoutView);
}

return vh;
}

@Override

  public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {

switch (this.getItemViewType(position)){

    case 0:
        ViewHolder0 holder0 = (ViewHolder0)holder;

        try {
            holder0.player_games_played.setText(values.getString("playerGamesPlayed"));
            holder0.player_games_won.setText(values.getString("playerwinStreak"));
            holder0.player_lifetimegold.setText(values.getString("lifetimeGold"));
            Picasso.with(activity).load(values.getInt("playerSkillTier")).into(holder0.player_skilltier);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        break;

    case 1:
        ViewHolder1 holder1 = (ViewHolder1)holder;

        try {
        holder1.wins_defeats.setText(values.getString("playerWinDefeat"));
        holder1.winPercent.setText(values.getString("playerWinPercent"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        break;

    case 2:
        ViewHolder2 holder2 = (ViewHolder2)holder;
        try {
        List<SliceValue> sliceValue = new ArrayList<>();
        if (values.getInt("blitz")>0) sliceValue.add(new SliceValue(values.getInt("blitz"), ChartUtils.pickColor()).setLabel(activity.getString(R.string.blitz)));
        if (values.getInt("ranked")>0)sliceValue.add(new SliceValue(values.getInt("ranked"), ChartUtils.pickColor()).setLabel(activity.getString(R.string.ranked)));
        if (values.getInt("casual")>0)sliceValue.add(new SliceValue(values.getInt("casual"), ChartUtils.pickColor()).setLabel(activity.getString(R.string.casual)));
        if (values.getInt("royal")>0)sliceValue.add(new SliceValue(values.getInt("royal"), ChartUtils.pickColor()).setLabel(activity.getString(R.string.royal)));

        PieChartData dataPie = new PieChartData();
        dataPie.setHasLabels(true);
        dataPie.setValues(sliceValue);

        holder2.chartPie.setPieChartData(dataPie);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        break;

    case 3:
        ViewHolder3 holder3 = (ViewHolder3)holder;

        List<Line> lines = new ArrayList<Line>();
        Axis axisX = new Axis().setHasLines(true);
        Axis axisY = new Axis().setHasLines(true).setHasTiltedLabels(false);
        try {
        Line line = new Line((ArrayList) values.get("valuesLine"));

        line.setHasPoints(false);
        line.setColor(ChartUtils.pickColor());
        lines.add(line);
        axisX.setName("Games");
        axisY.setName("Progress");
        LineChartData dataLine = new LineChartData();
        dataLine.setAxisYLeft(axisY);
        dataLine.setAxisXBottom(axisX);
        dataLine.setLines(lines);

        holder3.chartLine.setLineChartData(dataLine);
        holder3.chartLine.setZoomEnabled(false);
        holder3.chartLine.setInteractive(true);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        break;
}

}

@Override
public int getItemCount() {
return 4;
}

public static class ViewHolder0 extends RecyclerView.ViewHolder {

ImageView player_skilltier;
TextView player_lifetimegold;
TextView player_games_played;
TextView player_games_won;

ViewHolder0(View itemView) {
    super(itemView);
    player_skilltier = (ImageView)itemView.findViewById(R.id.imageView_player_skilltier);
    player_lifetimegold = (TextView)itemView.findViewById(R.id.textView_player_lifetimegold);
    player_games_played = (TextView)itemView.findViewById(R.id.textView_player_gamesplayed);
    player_games_won = (TextView)itemView.findViewById(R.id.textView_player_gameswon);

}

}

public static class ViewHolder1 extends RecyclerView.ViewHolder{
TextView winPercent;
TextView wins_defeats;
public ViewHolder1(View itemView) {
    super(itemView);
    winPercent = (TextView)itemView.findViewById(R.id.textView_win_percent);
    wins_defeats = (TextView)itemView.findViewById(R.id.textView_win_defeat);
}
}
 public static class ViewHolder2 extends RecyclerView.ViewHolder{
  PieChartView chartPie;

  public ViewHolder2(View itemView) {
    super(itemView);
    chartPie = (PieChartView) itemView.findViewById(R.id.chartGameMode);
}
}
public static class ViewHolder3 extends RecyclerView.ViewHolder{
LineChartView chartLine;
public ViewHolder3(View itemView) {
    super(itemView);
    chartLine = (LineChartView)itemView.findViewById(R.id.chartStreak);
}
}

我收到警告:&#34; 解除引用类型惩罚指针会破坏严格别名规则[-Wstrict-aliasing] &#34;在以下行:

size_t csps_socket_read(csps_socket_t *s, csps_packet_wrapper_t *packet, size_t sz)

如果我这样改写:

packet_size = ((csps_packet_full_header_t *)s->receive_mem)->size + header_size;

我没有得到任何警告。为什么?问题仍然存在,但是gcc无法看到它吗?

这里涉及的结构:

csps_packet_full_header_t *packet_full_header = (csps_packet_full_header_t *)s->receive_mem;
packet_size = packet_full_header->size + header_size;

1 个答案:

答案 0 :(得分:2)

基本上这是GCC -Wstrict-aliasing机器中的一个错误。众所周知,它们都会产生错误警告并错过实际的别名违规行为(请参阅开发人员的this comment)。

在两种情况下都存在问题,强制不相关的结构会违反别名规则,并可能导致GCC产生意外的代码。