I know there is a standard behind all C compiler implementations, so there should be no hidden features. Despite that, I am sure all C developers have hidden/secret tricks they use all the time. Function pointers. You can use a table of function pointers to implement, eg, fast indirect-threaded code interpreters (FORTH) or byte-code dispatchers, or to simulate OO-like virtual methods. Then
我知道所有C编译器实现背后都有一个标准,所以不应该有隐藏的特性。 尽管如此,我确信所有C开发人员都有他们始终使用的隐藏/秘密技巧。 函数指针。 您可以使用一个函数指针表来实现例如快速间接线程代码解释器(FORTH)或字节码调度器,或模拟类OO虚拟方法。 然后在标准库中存在隐藏的宝石,例如qsort(),bsearch(),strpbrk(),strcspn()[后两者对于实现strtok()替换有用)。 C的错误特征是带符号的算术溢出
In our code we used to have something like this: *(controller->bigstruct) = ( struct bigstruct ){ 0 }; This used to work great, and then we upgraded versions of GCC and suddenly started seeing stack overflows. Looking at the assembly, the old GCC code (2.x) was basically doing this: memset(controller->bigstruct, 0, sizeof(struct bigstruct)); The new GCC (3.4.x) was doing this st
在我们的代码中,我们曾经有过这样的东西: *(controller->bigstruct) = ( struct bigstruct ){ 0 }; 这曾经效果很好,然后我们升级了GCC版本,并突然开始看到堆栈溢出。 看着程序集,旧的GCC代码(2.x)基本上是这样做的: memset(controller->bigstruct, 0, sizeof(struct bigstruct)); 新的GCC(3.4.x)正在这样做 struct bigstruct temp = { 0 }; controller->bigstruct = temp; 在回顾了C99规范后
在C(而不是C ++)中实现编译时静态断言的最好方法是什么,特别强调GCC? C-1x adds the _Static_assert keyword. This seems to be implemented in gcc-4.6: _Static_assert (0, "assert1"); /* { dg-error "static assertion failed: "assert1"" } */ The first slot needs to be an integral constant expression. The second slot is a constant string literal which can be long ( _Static_assert(0, L"assertion
在C(而不是C ++)中实现编译时静态断言的最好方法是什么,特别强调GCC? C-1x添加_Static_assert关键字。 这似乎是在gcc-4.6中实现的: _Static_assert (0, "assert1"); /* { dg-error "static assertion failed: "assert1"" } */ 第一个插槽需要是一个整数常量表达式。 第二个插槽是一个可以很长的常量字符串文字( _Static_assert(0, L"assertion of doom!") )。 我应该注意到这也是在最近版本的clang中实
I would like to force a functions parameters to accept only specific definitions. For example, consider #define OUTPUT 1 , #define INPUT 0 and void restrictedFunction(int parameter); . How would I force restrictedFunction(int parameter) to accept only OUTPUT or INPUT ? I would also like to take into consideration that another definition may have the same value, for example, #define LEFT 1 a
我想强制一个函数参数只接受特定的定义。 例如,考虑#define OUTPUT 1 ,# #define INPUT 0和void restrictedFunction(int parameter); 。 我将如何强制restrictedFunction(int parameter)只接受OUTPUT或INPUT ? 我还想考虑另一个定义可能具有相同的值,例如#define LEFT 1和#define RIGHT 0 。 所以在这种情况下,我希望restrictedFunction(int parameter)能够仅接受OUTPUT和INPUT 。 typedef enum { INPUT = 0, OUTPU
Using only the features of C89, given typedef [unspecified token sequence] T1; typedef [another unspecified token sequence] T2; exhibit a language construct which will compile without error if and only if T1 and T2 are the same type (not just compatible). The limitation to C89 is because this is going into an autoconf probe. EDIT: I need a solution which works even if T1 or T2 or both are in
给出仅使用C89的功能 typedef [unspecified token sequence] T1; typedef [another unspecified token sequence] T2; 展示了一种语言结构,当且仅当T1和T2是相同类型(不仅仅是兼容)时才会编译出错。 对C89的限制是因为这将进入autoconf探测器。 编辑:我需要一种解决方案,即使T1或T2或两者都是不完整的类型。 对不起,以前没有提到这一点。 编辑之子:所有三个当前的答案只检测兼容的类型。 事实证明,这与我记忆中
Why and where does C standard allow this code compile? where is it useful? struct foo { int : 12; }; That would be in §6.7.2.1 Structure and union specifiers 12) A bit-field declaration with no declarator, but only a colon and a width, indicates an unnamed bit-field.126 The footnote explains why such things exist: 126 An unnamed bit-field structure member is useful for padding to confo
C标准为何以及在哪里允许编译此代码? 它有用吗? struct foo { int : 12; }; 这将在§6.7.2.1结构和联合说明符 12)没有声明符的位域声明,但只有冒号和宽度,表示未命名的位域.126 脚注解释了为什么存在这样的事情: 126未命名的位域结构成员对于填充符合外部施加的布局非常有用。 话虽如此,标准的同一部分(第8段)也指出: 如果struct-declaration-list不包含任何指定成员,直接或通过匿名结构或匿名联合,行
Is there any possible way to make the compiler bail out if the sizeof (struct Astruct) is uneven? Background information: We have a 16-bit microprocessor which will give processor alignment errors if a 16-bit value is mis-aligned. That might happen in the following scenario: typedef struct { U8BIT u8BitValue1; U8BIT u8BitValue2; U8BIT u8BitValue3; } unevenAmountOf8BitValues; type
如果sizeof (struct Astruct)不均匀,是否有任何可能的方式让编译器sizeof (struct Astruct)出来? 背景信息:我们有一个16位微处理器,如果16位值不对齐,会导致处理器对齐错误。 在以下情况下可能会发生这种情况: typedef struct { U8BIT u8BitValue1; U8BIT u8BitValue2; U8BIT u8BitValue3; } unevenAmountOf8BitValues; typedef struct { U16BIT u16BitValue1; U16BIT u16BitValue2; } my16BitVal
Linux kernel code uses "statement-expression" and typeof extension that makes it only compilable under gcc. More I think about it, more it doesn't make sense. It defeats the purpose of portability and standard C. (now linux kernel code needs a specific compiler that supports gcc extensions). Was it a bad design choice or was there a specific reason for making linux kernel code
Linux内核代码使用“statement-expression”和typeof扩展,使其只能在gcc下编译。 更多的是我想的,更多的是没有意义的。 它违背了可移植性和标准C的目的(现在,linux内核代码需要一个支持gcc扩展的特定编译器)。 这是一个糟糕的设计选择,还是有特定的原因让linux内核代码专用于gcc? 编辑:当我说它击败可移植性时,我在不同的上下文中使用它。 我认为,通过符合标准C,任何支持标准C的编译器(这正是创建一个标准的
Following This tutorial i am doing a sample program of speech recognition in WP8.I code like: public async void SpeechToText_Click(object sender, RoutedEventArgs e) { SpeechRecognizerUI speechRecognition=new SpeechRecognizerUI(); SpeechRecognitionUIResult recoResult=await speechRecognition.RecognizeWithUIAsync(); if (recoResult.ResultStatus == SpeechRecognitionUIStatus.Succeeded) { Me
关于本教程我正在做一个WP8中的语音识别示例程序,如下所示: public async void SpeechToText_Click(object sender, RoutedEventArgs e) { SpeechRecognizerUI speechRecognition=new SpeechRecognizerUI(); SpeechRecognitionUIResult recoResult=await speechRecognition.RecognizeWithUIAsync(); if (recoResult.ResultStatus == SpeechRecognitionUIStatus.Succeeded) { MessageBox.Show(string.Format("You sai
This question already has an answer here: How do I properly clean up Excel interop objects? 36 answers Here is an interesting knowledge base on the subject of office apps staying open after a .NET app disconnects from them. Office application does not quit after automation from Visual Studio .NET client The code examples are all in the link (vb.net sorry). Basically it shows you how to
这个问题在这里已经有了答案: 如何正确清理Excel互操作对象? 36个答案 这是一个关于办公应用程序在.NET应用程序断开连接后保持打开状态的有趣知识库。 从Visual Studio .NET客户端自动化后Office应用程序不会退出 代码示例都在链接中(vb.net抱歉)。 基本上它会告诉你如何正确设置和拆卸办公室应用程序,以便在完成后关闭它。 System.Runtime.InteropServices.Marshal.FinalReleaseComObject是魔术发生的地方。