首页 \ 问答 \ 错误代码+ GCC 5.4优化导致无限循环(Erroneous Code + GCC 5.4 Optimisation Causes Infinite Loop)

错误代码+ GCC 5.4优化导致无限循环(Erroneous Code + GCC 5.4 Optimisation Causes Infinite Loop)

免责声明:

  • 我在这里分享的代码是真实代码的独立版本,但是重现了相同的行为。

以下使用gcc 5.4.0编译并在Ubuntu 16.04上启用优化的代码在执行时生成无限循环:

#include <stdio.h>


void *loop(char *filename){

    int counter = 10;
    int level = 0;
    char *filenames[10];
    filenames[0] = filename;

    while (counter-- > 0) {

        level++;
        if (level > 10) {
            break;
        }

        printf("Level %d - MAX_LEVELS %d\n", level, 10);
        filenames[level] = filename;

    }

    return NULL;
}

int main(int argc, char *argv[]) {
    loop(argv[0]);
}

编译器版本:

gcc --version
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.

使用的编译命令:

gcc infinite.c -O2  -o infinite

我知道它是由优化标志“-02”引起的,因为没有它就不会发生。 我也知道将volatile添加到变量“level”也可以修复错误。 但我无法将此关键字添加到我的所有变量中。

我的问题是,为什么会发生这种情况,以及我可以做些什么来避免将来呢?

是否有任何gcc标志仍然在类似的-O2级别优化代码而没有这种问题?


Disclaimer:

  • The code I'm sharing here is an isolated version of the real code, nevertheless reproduce the same behaviour.

The following code compiled using gcc 5.4.0 with optimisation enabled on Ubuntu 16.04, when executed, generates a infinite loop:

#include <stdio.h>


void *loop(char *filename){

    int counter = 10;
    int level = 0;
    char *filenames[10];
    filenames[0] = filename;

    while (counter-- > 0) {

        level++;
        if (level > 10) {
            break;
        }

        printf("Level %d - MAX_LEVELS %d\n", level, 10);
        filenames[level] = filename;

    }

    return NULL;
}

int main(int argc, char *argv[]) {
    loop(argv[0]);
}

The compiler versions:

gcc --version
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.

The compilation command used:

gcc infinite.c -O2  -o infinite

I know that it is caused by the optimisation flag "-02" because it doesn't happen without it. I also Know that adding volatile to the variable "level" also fix the error. But I can't add this keyword to all my variables.

My question is, why this happen and what can I do to avoid it in the future?

Is there any gcc flag that still optimise the code at a similar level of -O2 without this kind of problem?


原文:https://stackoverflow.com/questions/43905249
更新时间:2023-05-27 19:05

最满意答案

为什么不在Toolbar TabLayout放置TabLayout的第一个选项? 这就是我这样做的结果:

在此处输入图像描述

布局方面:

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize" />

    </android.support.v7.widget.Toolbar>

活动方面:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled(false);

TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);

tabLayout.getTabAt(0).setIcon(R.drawable.icon1);
....
....
tabLayout.getTabAt(5).setIcon(R.drawable.icon6);

您也可以不使用CoordinatorLayoutAppBarLayout ,因为它们不会发挥作用。 layout_scrollFlagslayout_behavior也会出现。

编辑:

我使用UI Automator Viewer来检查LinkedIn在布局方面做了什么。 他们没有使用ActionBar / Toolbar 。 相反,他们有一个水平的LinearLayoutTabLayout作为第一个孩子,一些独立的图标作为第二个和第三个孩子。

接下来,我检查了TabLayout's源代码,以确定选项卡使用的确切布局。 TabLayout's选项卡由TabLayout.TabView定义,它是以编程方式创建的LinearLayout

class TabView extends LinearLayout implements OnLongClickListener {
    ....
}

我把它翻译成XML:

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:paddingStart="12dp"
    android:paddingEnd="12dp"
    android:orientation="vertical"
    android:gravity="center">

    <ImageView
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:src="@drawable/icon5"/>

</LinearLayout>

注意:上面指定的尺寸不是任意的。 它们在设计支持库中定义。

因此,最终的ActionBar布局将是:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:orientation="horizontal"
    android:background="?attr/colorPrimary">

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="0dp"
        android:layout_height="?attr/actionBarSize"
        android:layout_weight="4"/>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:paddingStart="12dp"
        android:paddingEnd="12dp"
        android:orientation="vertical"
        android:gravity="center"
        android:layout_weight="1">

        <ImageView
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:src="@drawable/icon5"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:paddingStart="12dp"
        android:paddingEnd="12dp"
        android:orientation="vertical"
        android:gravity="center"
        android:layout_weight="1">

        <ImageView
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:src="@drawable/icon6"/>

    </LinearLayout>

</LinearLayout>

Why not use the first option with the TabLayout placed inside the Toolbar? This is what I get by doing so:

enter image description here

Layout side:

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize" />

    </android.support.v7.widget.Toolbar>

Activity side:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled(false);

TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);

tabLayout.getTabAt(0).setIcon(R.drawable.icon1);
....
....
tabLayout.getTabAt(5).setIcon(R.drawable.icon6);

You can also do without CoordinatorLayout, along with AppBarLayout, since they do not come into play. layout_scrollFlags & layout_behavior will go as well.

Edit:

I used UI Automator Viewer to check what LinkedIn was doing with their layout. They are not using an ActionBar/Toolbar. Instead, they have a horizontal LinearLayout with TabLayout as the first child, and a couple of independent icons as the second and third.

Next, I checked TabLayout's source to determine the exact layout that tabs use. TabLayout's tabs are defined by TabLayout.TabView which is a LinearLayout created programmatically:

class TabView extends LinearLayout implements OnLongClickListener {
    ....
}

I translated this to XML:

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:paddingStart="12dp"
    android:paddingEnd="12dp"
    android:orientation="vertical"
    android:gravity="center">

    <ImageView
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:src="@drawable/icon5"/>

</LinearLayout>

Note: The dimensions assigned above are not arbitrary. They are defined in the design support library.

So, the final ActionBar layout will be:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:orientation="horizontal"
    android:background="?attr/colorPrimary">

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="0dp"
        android:layout_height="?attr/actionBarSize"
        android:layout_weight="4"/>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:paddingStart="12dp"
        android:paddingEnd="12dp"
        android:orientation="vertical"
        android:gravity="center"
        android:layout_weight="1">

        <ImageView
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:src="@drawable/icon5"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:paddingStart="12dp"
        android:paddingEnd="12dp"
        android:orientation="vertical"
        android:gravity="center"
        android:layout_weight="1">

        <ImageView
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:src="@drawable/icon6"/>

    </LinearLayout>

</LinearLayout>

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)