错误代码+ 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
最满意答案
为什么不在
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);
您也可以不使用
CoordinatorLayout
和AppBarLayout
,因为它们不会发挥作用。layout_scrollFlags
&layout_behavior
也会出现。编辑:
我使用UI Automator Viewer来检查LinkedIn在布局方面做了什么。 他们没有使用
ActionBar
/Toolbar
。 相反,他们有一个水平的LinearLayout
,TabLayout
作为第一个孩子,一些独立的图标作为第二个和第三个孩子。接下来,我检查了
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 theToolbar
? This is what I get by doing so: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 withAppBarLayout
, 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 horizontalLinearLayout
withTabLayout
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 byTabLayout.TabView
which is aLinearLayout
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>
相关问答
更多-
如何用里面的Tabs创建新的LinkedIn应用程序样式ActionBar?(How to create new LinkedIn app style ActionBar with Tabs inside it?)[2022-04-27]
为什么不在Toolbar TabLayout放置TabLayout的第一个选项? 这就是我这样做的结果: 布局方面:ActionBar中的选项卡(Tabs in the ActionBar)[2023-03-25]
尝试使用PagerTitleStrip由android提供,如评论中所建议,但要解决问题,请尝试: 将其添加到清单文件中 android:value="splitActionBarWhenNarrow" /> 并使用setDisplayShowHomeEnabled(false);禁用操作栏图标和标题setDisplayShowHomeEnabled(false); 和setDisplayShowTitleEnabled(false); 这是一个让你从Android开发人员开始的链接: Try to us ...跟踪说:“无法在活动类com.androidbegin.absfragtabhost.MainActivity中找到方法addButtonClicked(View),用于视图类android.widget.Button上的onClick处理程序,其id为'addButtonClicked'” 该方法需要在不在片段中的活动类中,要么放在那里,要么使用onClickListener(这就是我要做的) 喜欢这个: Button addButtonClicked = (Button)rootView.findVie ...如何使用自定义视图和选项卡创建Android ActionBar(How to create Android ActionBar with custom view and tabs)[2022-03-26]
顶行中的按钮必须添加为optionMenu项目: @Override public boolean onCreateOptionsMenu(Menu menu) { for (int i = 0; i < menuItems; i++) { if (i == 0) { menu.add(R.string.home).setIcon(R.drawable.button_home).setShowAsAction(MenuItem.SHOW ...可以在原生ActionBar中同时使用制表符和微调器吗? 假设您的意思是“操作栏标签”,则它们不能同时直接使用,因为它们都被视为导航模式。 此外,由于动作栏选项卡可以通过框架转换为下拉列表(即,微调器)导航,因此具有两个微调器可能会令人困惑。 我建议使用替代选项卡实现,例如带有选项卡式指示器的ViewPager 。 Is possible to use tabs and spinners at the same time in native ActionBar? Assuming that you mean ...使用这些方法隐藏操作栏标题栏 getActionBar().setDisplayShowTitleEnabled(false); getActionBar().setDisplayShowHomeEnabled(false); Use these methods to hide the action bar title bar getActionBar().setDisplayShowTitleEnabled(false); getActionBar().setDisplayShowHomeEnabled ...我认为以下参考链接可以帮助您 http://developer.android.com/guide/topics/ui/actionbar.html http://www.androidhive.info/2013/11/android-working-with-action-bar/ i think the following reference links can help you http://developer.android.com/guide/topics/ui/actionbar.html h ...Android从行动栏中的标签创建的样式下拉菜单(Styling dropdown menu that Android creates from tabs in the actionbar)[2023-03-11]
不幸的是,显示ScrollingTabContainerView创建的Spinner每个选项卡的TextView 继承了actionBarTabTextStyle 。 因此,您无法单独修改它们。 您最好的选择是收听方向更改,然后在横向时将ActionBar的导航模式设置为ActionBar.NAVIGATION_MODE_LIST ,并使用ActionBar.setListNavigationCallbacks绑定您自己的SpinnerAdapter 。 Unfortunately, the TextVie ...更新到android-support-library v19解决了这个问题。 Updating to android-support-library v19 solved the problem.如果你想要像Googles更新的应用程序,如Play商店的Android标签,那么你将不得不使用Googles开源I / O 2014应用程序中的ViewPager和两个java文件。 如果你是初学者,我建议不要使用android开发者网站来处理像android标签这样的常见内容,因为它们只是过于复杂而且它几乎就像计算机科学学位,你只使用你学到的40%。 有数百个教程网站和博客,将向您展示如何做同样的事情,但以一种更易消化的方式:D If you want android tabs that look li ...相关文章
更多- Hadoop错误代码查询
- 试玩 PHP 5.4 的新特性
- 公众号大利好!深度解读微信5.4版本升级
- 【第五章】Spring表达式语言 之 5.4在Bean定义中使用EL—跟我学spring3
- iPad版微信终于来临了 微信5.4版搜索更智能 转账就是发消息
- 微信会员注册开发【带源码】:网页授权,得到code后在当前页面获取openid,js+php实现跨域请求
- reading notes for solr source code
- English,The Da Vinci Code,Chapter 1-3
- 用Hadoop中的libhdfs和fuse-dfs构建快速云存储
- C程序访问Hadoop
最新问答
更多- 您如何使用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)