g++ 有长度限制吗变量名?
看标题
See title
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
看标题
See title
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
另一个问题是,某些链接器对损坏名称的长度有限制。这往往是模板和嵌套类超过标识符长度的问题,但据我所知,两者都可能引发问题
an additional gotcha, some linkers have a limit on the length of the mangled name. this tends to be an issue with template and nested classes more than identifier length but either could trigger a problem afaik
我不知道限制是什么(或者是否有限制),但我认为应该有一个限制,以便捕获病态代码,例如由失控代码生成器创建的代码,这是一种很好的做法。无论如何,C++ 标准建议标识符长度最小值为 1K。
I don't know what the limit is (or if there is one), but I think it is good practice that there should be one, in order to catch pathological code, for example that created by a runaway code generator. For what it's worth, the C++ Standard suggests a minimum of 1K for identifier length.
简短的回答:
没有
长的回答:
是的,它必须足够小才能适合内存,但否则就不行,不是真的。如果有一个内置的限制(我不相信有),那么它太大了,你真的很难达到它。
实际上,你让我很好奇,所以我创建了以下 Python 程序来生成代码:
这会使用所需长度的变量名称生成 C++ 代码。使用以下内容:
上面的内容没有给我带来任何问题(变量名称的长度大约为 1MB)。我尝试了千兆字节,但我对字符串构造不太聪明,因此我决定在 gencpp.py 花费太长时间时中止。
不管怎样,我非常怀疑 gcc 为变量名预分配了 1MB。它纯粹受内存限制。
Short Answer:
No
Long Answer:
Yes, it has to be small enough that it will fit in memory, but otherwise no, not really. If there is a builtin limit (I don't believe there is) it is so huge you'd be really hard-pressed to reach it.
Actually, you got me really curious, so I created the following Python program to generate code:
This generates C++ code using the desired length variable name. Using the following:
The above gives me no problems (the variable name is roughly 1MB in length). I tried for a gigabyte, but I'm not being so smart with the string construction, and so I decided to abort when gencpp.py took too long.
Anyway, I very much doubt that gcc pre-allocates 1MB for variable names. It is purely bounded by memory.