g++ 有长度限制吗变量名?

发布于 2024-08-29 12:05:32 字数 10 浏览 5 评论 0原文

看标题

See title​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凡尘雨 2024-09-05 12:05:34

另一个问题是,某些链接器对损坏名称的长度有限制。这往往是模板和嵌套类超过标识符长度的问题,但据我所知,两者都可能引发问题

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

热风软妹 2024-09-05 12:05:34

我不知道限制是什么(或者是否有限制),但我认为应该有一个限制,以便捕获病态代码,例如由失控代码生成器创建的代码,这是一种很好的做法。无论如何,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.

离线来电— 2024-09-05 12:05:33

简短的回答:

没有

长的回答:

是的,它必须足够小才能适合内存,但否则就不行,不是真的。如果有一个内置的限制(我不相信有),那么它太大了,你真的很难达到它。

实际上,你让我很好奇,所以我创建了以下 Python 程序来生成代码:

#! /usr/bin/env python2.6
import sys;
cppcode="""
#include <iostream>
#include <cstdlib>

int main(int argc, char* argv[])
{
     int %s = 0;
     return 0;
}
"""

def longvarname(n):
    str="x";
    for i in xrange(n):
        str = str+"0";
    return str;

def printcpp(n):
    print cppcode % longvarname(n);

if __name__=="__main__":
    if len(sys.argv)==2:
        printcpp(int(sys.argv[1]));

这会使用所需长度的变量名称生成 C++ 代码。使用以下内容:

./gencpp.py 1048576 > main.cpp
g++ main.cpp -o main

上面的内容没有给我带来任何问题(变量名称的长度大约为 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:

#! /usr/bin/env python2.6
import sys;
cppcode="""
#include <iostream>
#include <cstdlib>

int main(int argc, char* argv[])
{
     int %s = 0;
     return 0;
}
"""

def longvarname(n):
    str="x";
    for i in xrange(n):
        str = str+"0";
    return str;

def printcpp(n):
    print cppcode % longvarname(n);

if __name__=="__main__":
    if len(sys.argv)==2:
        printcpp(int(sys.argv[1]));

This generates C++ code using the desired length variable name. Using the following:

./gencpp.py 1048576 > main.cpp
g++ main.cpp -o main

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文