Why no compiler appears able to optimize this code?

Consider the following C code (assuming 80-bit long double ) (note, I do know of memcmp , this is just an experiment): enum { sizeOfFloat80=10 }; // NOTE: sizeof(long double) != sizeOfFloat80 _Bool sameBits1(long double x, long double y) { for(int i=0;i<sizeOfFloat80;++i) if(((char*)&x)[i]!=((char*)&y)[i]) return 0; return 1; } All compilers I checked (gcc

为什么没有编译器能够优化此代码?

考虑下面的C代码(假设80位long double )(注意,我知道memcmp ,这只是一个实验): enum { sizeOfFloat80=10 }; // NOTE: sizeof(long double) != sizeOfFloat80 _Bool sameBits1(long double x, long double y) { for(int i=0;i<sizeOfFloat80;++i) if(((char*)&x)[i]!=((char*)&y)[i]) return 0; return 1; } 我检查过的所有编译器(gcc,clang,icc on gcc.godbolt.org)都会生

Size of stack and heap memory

Possible Duplicate: What and where are the stack and heap? With regard to the basic concepts of memory layout in ac program, I understand the that: The language uses two primary data structures stack and heap . Stack is created to store the local variables and book keeping data of subroutines Heap is created to store the dynamically allocated variables of the program Heap is of variab

堆栈和堆内存的大小

可能重复: 什么和堆栈和堆在哪里? 关于ac程序中内存布局的基本概念,我了解到: 该语言使用两个主要数据结构堆栈和堆 。 创建堆栈以存储局部变量和子程序的簿记数据 创建堆以存储程序的动态分配的变量 堆本质上是可变长度的(堆栈中不太确定) 通常,编译器/语言的职责是请求OS在执行前创建这些数据结构。 问题 什么是堆栈/堆创建的初始大小? 谁来决定呢? 其中物理记忆是他们创造的? 我看到一个一般

Case insensitive 'Contains(string)'

Is there a way to make the following return true? string title = "ASTRINGTOTEST"; title.Contains("string"); There doesn't seem to be an overload that allows me to set the case sensitivity.. Currently I UPPERCASE them both, but that's just silly. UPDATE The sillyness I refer to is the i18n issues that come with up- and down casing. UPDATE This question is ancient and since then I

不区分大小写'包含(字符串)'

有没有办法让以下回报成真? string title = "ASTRINGTOTEST"; title.Contains("string"); 似乎没有超负荷允许我设置区分大小写。目前,我大写他们两个,但这只是愚蠢的。 UPDATE 我提到的愚蠢是上下套管带来的国际问题。 UPDATE 这个问题是古老的,从那以后,我意识到我要求一个简单的答案,如果你关心的是一个非常广泛和困难的话题,那么这个问题应该是完全的。 对于大多数情况下,在单语言,英语代码基础上,这个

What does the C ??!??! operator do?

I saw a line of C that looked like this: !ErrorHasOccured() ??!??! HandleError(); It compiled correctly and seems to run ok. It seems like it's checking if an error has occurred, and if it has, it handles it. But I'm not really sure what it's actually doing or how it's doing it. It does look like the programmer is trying express their feelings about errors. I have never see

什么是C ??!??! 运营商呢?

我看到一行C看起来像这样: !ErrorHasOccured() ??!??! HandleError(); 它编译正确,似乎运行正常。 它似乎在检查是否发生了错误,如果有,它会处理它。 但我不确定它究竟在做什么或者它是如何做的。 它看起来像程序员正在试图表达他们对错误的感受。 我从来没有见过??!??! 之前使用任何编程语言,我无法在任何地方找到文档。 (Google对搜索字词没有帮助,例如??!??! )。 它做什么以及代码示例如何工作? ??! 是翻

What does "yield break;" do in C#?

I have seen this syntax in MSDN: yield break , but I don't know what it does. Does anyone know? It specifies that an iterator has come to an end. You can think of yield break as a return statement which does not return a value. For example, if you define a function as an iterator, the body of the function may look like this: for (int i = 0; i < 5; i++) { yield return i; } Conso

什么是“产量突破”? 在C#中做?

我在MSDN中看到过这样的语法: yield break ,但我不知道它的作用。 有人知道吗? 它指定迭代器已结束。 您可以将yield break视为不返回值的return语句。 例如,如果您将函数定义为迭代器,则函数的主体可能如下所示: for (int i = 0; i < 5; i++) { yield return i; } Console.Out.WriteLine("You will see me"); 请注意,在循环完成所有循环后,最后一行会被执行,您将在控制台应用程序中看到消息。 或者像

What is the yield keyword used for in C#?

In the How Can I Expose Only a Fragment of IList<> question one of the answers had the following code snippet: IEnumerable<object> FilteredList() { foreach( object item in FullList ) { if( IsItemInPartialList( item ) yield return item; } } What does the yield keyword do there? I've seen it referenced in a couple places, and one other question, bu

在C#中使用的yield关键字是什么?

在我如何公开只有一个IList <>的问题问题之一的答案有以下代码片段: IEnumerable<object> FilteredList() { foreach( object item in FullList ) { if( IsItemInPartialList( item ) yield return item; } } yield关键字在那里做什么? 我已经在几个地方看到了它的引用,还有一个问题,但我还没有弄清楚它的实际功能。 我习惯于从一个线程产生另一个线程的意义上考虑产量,但

Is there a reason for C#'s reuse of the variable in a foreach?

When using lambda expressions or anonymous methods in C#, we have to be wary of the access to modified closure pitfall. For example: foreach (var s in strings) { query = query.Where(i => i.Prop == s); // access to modified closure ... } Due to the modified closure, the above code will cause all of the Where clauses on the query to be based on the final value of s . As explained here

是否有理由让C#在foreach中重用该变量?

在C#中使用lambda表达式或匿名方法时,我们必须警惕修改后的闭包陷阱。 例如: foreach (var s in strings) { query = query.Where(i => i.Prop == s); // access to modified closure ... } 由于修改了闭包,上面的代码将导致查询中的所有Where子句基于s的最终值。 正如这里所解释的,这是因为在foreach循环中声明的s变量在编译器中被如此转换: string s; while (enumerator.MoveNext()) { s = enumerator.C

Can someone explain it to me what closure is in real simple language ?

Possible Duplicate: What are 'closures' in .NET? I am currently looking at lambda expression and the word closure keeps coming. Can someone explain it to me in real simple language. I'd say this is a duplicate of: What are 'closures' in .NET? "In essence, a closure is a block of code which can be executed at a later time, but which maintains the environment in wh

有人可以向我解释什么是真正简单的语言?

可能重复: 什么是.NET中的“闭包”? 我目前正在研究lambda表达式,并且闭包一直在继续。 有人能用真正简单的语言向我解释吗? 我会说这是一个重复的:.NET中的'闭包'是什么? “从本质上讲,闭包是一段代码,可以在稍后执行,但它保留了它最初创建的环境,即它仍然可以使用创建它的方法的局部变量等,即使在该方法已经完成执行。“ 你的鞋子在大厅里; 你的夹克在厨房里。 当他们走出去时,戴上他们的手套(他

What is the difference between String and string in C#?

Example (note the case): string s = "Hello world!"; String s = "Hello world!"; What are the guidelines for the use of each? And what are the differences ? string is an alias in C# for System.String . So technically, there is no difference. It's like int vs. System.Int32 . As far as guidelines, it's generally recommended to use string any time you're referring to an object.

String和C#中的字符串有什么区别?

示例(注意案例): string s = "Hello world!"; String s = "Hello world!"; 什么是使用每个指导原则 ? 有什么区别 ? string是C#中用于System.String的别名。 从技术上讲,没有区别。 这就像int与System.Int32 。 就指导原则而言,通常建议您在引用某个对象时使用string 。 例如 string place = "world"; 同样,我认为一般建议使用String如果你需要特别提到该类。 例如 string greet = String.Format("Hello

Why can't (or doesn't) the compiler optimize a predictable addition loop into a multiplication?

This is a question that came to mind while reading the brilliant answer by Mysticial to the question: why is it faster to process a sorted array than an unsorted array? Context for the types involved: const unsigned arraySize = 32768; int data[arraySize]; long long sum = 0; In his answer he explains that the Intel Compiler (ICC) optimizes this: for (int i = 0; i < 100000; ++i) for (in

为什么不能(或不)编译器将可预测的加法循环优化为乘法?

在阅读Mysticial关于这个问题的杰出答案时,想到这个问题:为什么处理排序后的数组比未排序的数组更快? 涉及类型的上下文: const unsigned arraySize = 32768; int data[arraySize]; long long sum = 0; 在他的回答中他解释说,英特尔编译器(ICC)优化了这一点: for (int i = 0; i < 100000; ++i) for (int c = 0; c < arraySize; ++c) if (data[c] >= 128) sum += data[c]; ...变成