在gcc编译的c程序中,表达式可能太长了? [英] Can an expression be too long in a c program for gcc to compile?

查看:169
本文介绍了在gcc编译的c程序中,表达式可能太长了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我将一个很长的公式输入到一行c代码(.c或.h文件)中,该代码长度为数千(甚至数万)个字符;例如,

  y =(2 *(36 * pow(x,2)* pow(A [n] [j] ,5)* B [n] [j] 
+(数千以上这样的表达式));

(这里只需将x作为变量,A,B为双指针等)。在说出gcc之前,一行代码可以在.c或.h文件中存在多长时间的限制编译器无法正确编译代码?我已经阅读了关于#c这个问题的几个相关讨论,但不是仅仅用普通的c。我从来没有收到gcc关于我的代码中的行太长的错误,但是我'$ d
$ b

编辑:
为了回应下面的一些评论,我现在意识到我在问两个(我认为密切相关)的问题:
$ b $(1)在gcc编译器可能产生错误/加载之前,c有多长时间可以有限制?错误?



(2)gcc编译器之前的表达式有多复杂ld可能会导致错误/引发错误? (例如,我们可以将很长的一行分成几行,但它们都是同一个表达的一部分)。

解决方案

您已经询问了两件独立的事情:一行的最大长度和表达式的复杂性。一个任意复杂的表达式可以很容易地分割成多行 - 就像你在例子中所做的那样。



C标准要求实现在逻辑源中至少支持4095个字符线。它表达的要求是相当间接的。编译器必须能够处理符合所有指定限制的一个程序。理由是该标准以精确和可测试的方式规定了要求,但满足要求的最简单方法是避免施加任何固定限制。

详情请参阅 N1570 5.2.4.1,翻译限制。该部分中的相关限制是在函数调用中有63个嵌套级别的括号和127个参数 - 但是您可以创建一个任意复杂的表达式,而不会触及任何一个限制。



该标准对表达式的复杂性没有具体的限制。大多数编译器(包括gcc)会在处理源代码时动态分配资源(特别是内存)。表达式的内部表示很可能是一个动态分配的树结构,而不是一个固定大小的数组。



你可能构造一个对于gcc来说太复杂的表达式,句柄,并且它可能会通过在无法分配内存时打印致命错误消息或仅通过分段错误或类似事件窒息而作出响应。在一台具有千兆字节内存的现代计算机上,你需要一个非常大的表达式来触发这样的失败。



你不会去去解决这个问题,除非你自动生成C代码,并且你的生成器失控。


Let's say I input a very long equation on to a single line of c code (either a .c or .h file) that is thousands (perhaps tens of thousands) of characters long; for example

y = (2*(36*pow(x,2)*pow(A[n][j],5)*B[n][j]
  + (several thousand more such expressions) ) ;

(here just take x to be a variable, A, B to be double pointers, etc). Is there a limit for how long a line of code can be in a .c or .h file before say the gcc compiler is unable to correctly compile the code? I've read several related discussions about this issue for #c, but not for just plain c. I have never received any errors from gcc about having too long of lines in my code, but I'd like to be extra sure about this point.

EDIT: In response to some of the below comments, I now realize that I was asking two (I think closely related) questions:

(1) Is there any limit to how long a line can be in c before the gcc compiler could potentially make an error/raise an error?

(2) Is there any limit to how complex an expression can be before the gcc compiler could potentially make an error/raise an error? (e.g. we could break up a very long line into several lines but it's all a part of the same expression).

解决方案

You've asked about two separate things: the maximum length of a line, and the complexity of an expression. An arbitrarily complex expression can easily be split across multiple lines -- as you did in your example.

The C standard requires implementations to support at least 4095 characters in a logical source line. The way it expresses that requirement is rather indirect. A compiler must be able to process one program that hits all the specified limits. The rationale is that the standard specifies the requirement in a precise and testable way, but the easiest way to meet the requirement is to avoid imposing any fixed limits at all.

The details are in N1570 5.2.4.1, "Translation limits". The relevant limits in that section are 63 nesting levels of parentheses and 127 arguments in a function call -- but you can create an arbitrarily complex expression without hitting either of those limits.

The standard imposes no specific limits on the complexity of an expression. Most compilers, including gcc, will allocate resources (particularly memory) dynamically as they're processing source code. The internal representation of an expression is likely to be a dynamically allocated tree structure, not a fixed-size array.

You can probably construct an expression that's too complex for gcc to handle, and it will probably respond either by printing a fatal error message when it's unable to allocate memory, or just by choking with a segmentation fault or something similar. On a modern computer with gigabytes of memory, you'd need a very large expression to trigger such a failure.

You're not going to run into this issue unless you're generating C code automatically, and your generator gets out of hand.

这篇关于在gcc编译的c程序中,表达式可能太长了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆