Behaviour of sizeof() in C (GCC)

While answering a question on sizeof(), just to see how GCC handles, I wrote the following code: #include<stdio.h> #include<stddef.h> #include<limits.h> int main(int ac, char *argv[]) { printf("%zun", sizeof(9999999999999999999999999999999999999999999999999999) ); printf("%zu %zu n", sizeof(int), sizeof(long long)); return 0; } When compiled, GCC (4.1.2) issued a

C(GCC)中sizeof()的行为

在回答关于sizeof()的问题时,为了看看GCC如何处理,我写了下面的代码: #include<stdio.h> #include<stddef.h> #include<limits.h> int main(int ac, char *argv[]) { printf("%zun", sizeof(9999999999999999999999999999999999999999999999999999) ); printf("%zu %zu n", sizeof(int), sizeof(long long)); return 0; } 编译后,GCC(4.1.2)发出警告(如预期): t.c:8:24: warning: in

Why does sizeof(x++) not increment x?

Here is the code compiled in dev c++ windows: #include <stdio.h> int main() { int x = 5; printf("%d and ", sizeof(x++)); // note 1 printf("%dn", x); // note 2 return 0; } I expect x to be 6 after executing note 1 . However, the output is: 4 and 5 Can anyone explain why x does not increment after note 1 ? From the C99 Standard (the emphasis is mine) 6.5.3.4/2 The

为什么sizeof(x ++)不增加x?

以下是在dev c ++ windows中编译的代码: #include <stdio.h> int main() { int x = 5; printf("%d and ", sizeof(x++)); // note 1 printf("%dn", x); // note 2 return 0; } 执行注1后,我预计x为6。 但是,输出是: 4 and 5 任何人都可以解释为什么在注1后x不增加? 从C99标准(重点是我的) 6.5.3.4/2 sizeof运算符产生其操作数的大小(以字节为单位),该操作数可以是表达式或类型的括号

sizeof(""+0) != sizeof(char *) Bug or undefined behaviour?

The following C program: #include <stdio.h> int main(void) { printf("%u %u %un",sizeof "",sizeof(""+0),sizeof(char *)); return 0; } outputs 1 4 4 when compiled with GCC on Linux, but outputs 1 1 4 when compiled with Microsoft Visual C++ on Windows. The GCC result is what I would expect. Do they differ because MSVC has a bug or because sizeof(""+0) is undefined? For b

sizeof(“”+ 0)!= sizeof(char *)错误或未定义的行为?

以下C程序: #include <stdio.h> int main(void) { printf("%u %u %un",sizeof "",sizeof(""+0),sizeof(char *)); return 0; } 在Linux上使用GCC编译时输出1 4 4,但在Windows上使用Microsoft Visual C ++编译时输出1 1 4。 海湾合作委员会的结果是我所期望的。 它们是不同的,因为MSVC有一个bug或者因为sizeof(“”+ 0)是未定义的? 对于两种编译器而言,无论使用什么字符串文字或整数常量,行为(即打印的

How using try catch for exception handling is best practice

while maintaining my colleague's code from even someone who claims to be a senior developer, I often see the following code: try { //do something } catch { //Do nothing } or sometimes they write logging information to log files like following try catch block try { //do some work } catch(Exception exception) { WriteException2LogFile(exception); } I am just wondering if what they h

如何使用try catch进行异常处理是最佳实践

在保持我的同事代码的同时,即使是自称是高级开发人员的人,我也经常看到以下代码: try { //do something } catch { //Do nothing } 或者有时他们会将日志记录信息写入日志文件,如下面的try catch块 try { //do some work } catch(Exception exception) { WriteException2LogFile(exception); } 我只是想知道他们所做的是否是最佳做法? 这让我感到困惑,因为我认为用户应该知道系统会发生什么。 请给我一些建

try/catch + using, right syntax

Which one: using (var myObject = new MyClass()) { try { // something here... } catch(Exception ex) { // Handle exception } } OR try { using (var myObject = new MyClass()) { // something here... } } catch(Exception ex) { // Handle exception } I prefer the second one. May as well trap errors relating to the creation of the object as well. 由于使

尝试/ catch +使用,正确的语法

哪一个: using (var myObject = new MyClass()) { try { // something here... } catch(Exception ex) { // Handle exception } } 要么 try { using (var myObject = new MyClass()) { // something here... } } catch(Exception ex) { // Handle exception } 我更喜欢第二个。 也可以捕捉与创建对象有关的错误。 由于使用块只是try / finally(MSDN)的语法简化,因此我

Will code in a Finally statement fire if I return a value in a Try block?

I'm reviewing some code for a friend and say that he was using a return statement inside of a try-finally block. Does the code in the Finally section still fire even though the rest of the try block doesn't? Example: public bool someMethod() { try { return true; throw new Exception("test"); // doesn't seem to get executed } finally { //code in question } } 简单

如果我在Try块中返回一个值,会在Finally语句中编写代码吗?

我正在检查一些朋友的代码,并说他在try-finally块中使用了return语句。 即使try块的其余部分没有,Finally部分中的代码是否仍然会启动? 例: public bool someMethod() { try { return true; throw new Exception("test"); // doesn't seem to get executed } finally { //code in question } } 简单的回答:是的。 通常,是的。 finally节保证执行任何事情,包括异常或返回语句。 此规则的一

Entity Framework Provider type could not be loaded?

I am trying to run my tests on TeamCity which is currently installed on my machine. System.InvalidOperationException : The Entity Framework provider type ' System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer , Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' for the ' System.Data.SqlClient ' ADO.NET provider could not be loaded. Make

实体框架提供程序类型无法加载?

我试图在我的机器上安装的TeamCity上运行我的测试。 System.InvalidOperationException : “ System.Data.SqlClient ”ADO.NET提供程序的实体框架提供程序类型“ System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer , Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'无法加载。 确保提供者程序集对正在运行的应用程序可用。 请参阅http://go.microsoft.com/fwlink/?L

The entity cannot be constructed in a LINQ to Entities query

There is an entity type called product that is generated by entity framework. I have writen this query public IQueryable<Product> GetProducts(int categoryID) { return from p in db.Products where p.CategoryID== categoryID select new Product { Name = p.Name}; } The code below throws the following error : "The entity or complex type Shop.Product cannot be co

实体不能在LINQ to Entities查询中构造

有一个实体类型称为产品,由实体框架生成。 我写了这个查询 public IQueryable<Product> GetProducts(int categoryID) { return from p in db.Products where p.CategoryID== categoryID select new Product { Name = p.Name}; } 下面的代码会引发以下错误: “实体或复杂类型Shop.Product不能在LINQ to Entities查询中构建” var products = productRepository.GetProducts(1).Tolist();

What does the C# CoClass attribute do?

I found code something like the following in a 3rd party library we're using. [CoClass(typeof(BlahClass))] public interface Blah : IBlah { } What is this doing exactly? The msdn documentation didn't illuminate the subject sufficiently for me to follow. It declares that the interface Blah is intended to be implemented by a specific class. It means that you can conveniently say new Bl

C#CoClass属性有什么作用?

我在我们使用的第三方库中发现了类似以下的代码。 [CoClass(typeof(BlahClass))] public interface Blah : IBlah { } 这是干什么的? msdn文档没有充分说明这个主题让我遵循。 它声明接口Blah旨在由特定类实现。 这意味着您可以方便地说出new Blah ,并且运行时会知道要创建什么对象 - 通常不可能通过接口实现。 如果您查看为BlahClass生成的声明,它可能会有一个与它关联的Guid ,它可以用来创建一个COM对象的实例。

Why no linq for byte, uint?

Enumerable.Sum() is only defined for IEnumerable where T in double, double? int, int? decimal, decimal? long, long? float, float? Is there a reason for not defining for byte, short or uint? Is it to avoid overflow problems? Or does it make type inference difficult for compiler? (Most Likely) Because when you add a short and a short the outcome can be handled by an int. Could be so

为什么没有linq的字节,uint?

Enumerable.Sum()仅为IEnumerable定义,其中T in 双,双? int,int? 十进制,十进制? 很久很久以前? 浮动,浮动? 是否有没有定义字节,短或uint的原因? 是否避免溢出问题? 还是它使编译器难以进行类型推断? (最有可能的) 因为当你添加一个short和short时,结果可以由int处理。 也可能是一些CLS兼容的问题。 这被张贴为一个类似的问题: 为什么没有用于IEnumerable <uint>的Sum()扩展