我们可以有多少级指针?

时间:2022-04-03 00:03:05

How many pointers (*) are allowed in a single variable?

一个变量中允许有多少个指针(*)?

Let's consider the following example.

让我们考虑下面的例子。

int a = 10;
int *p = &a;

Similarly we can have

同样我们可以

int **q = &p;
int ***r = &q;

and so on.

等等。

For example,

例如,

int ****************zz;

14 个解决方案

#1


382  

The C standard specifies the lower limit:

C标准规定了下限:

5.2.4.1 Translation limits

276 The implementation shall be able to translate and execute at least one program that contains at least one instance of every one of the following limits: [...]

该实现应能够翻译并执行至少一个程序,该程序包含以下任何一个限制的至少一个实例:[…]

279 — 12 pointer, array, and function declarators (in any combinations) modifying an arithmetic, structure, union, or void type in a declaration

279 - 12指针、数组和函数声明符(任何组合)修改声明中的算术、结构、联合或空类型

The upper limit is implementation specific.

上限是特定于实现的。

#2


145  

Actually, C programs commonly make use of infinite pointer indirection. One or two static levels are common. Triple indirection is rare. But infinite is very common.

实际上,C程序通常使用无限指针间接。一个或两个静态级别是常见的。三重间接是罕见的。但无穷大是很常见的。

Infinite pointer indirection is achieved with the help of a struct, of course, not with a direct declarator, which would be impossible. And a struct is needed so that you can include other data in this structure at the different levels where this can terminate.

无限指针间接是在结构体的帮助下实现的,当然不是通过直接的声明者,这是不可能的。需要一个结构体,这样你就可以在这个结构中包含其他的数据,在不同的层次上,这些数据可以终止。

struct list { struct list *next; ... };

now you can have list->next->next->next->...->next. This is really just multiple pointer indirections: *(*(..(*(*(*list).next).next).next...).next).next. And the .next is basically a noop when it's the first member of the structure, so we can imagine this as ***..***ptr.

现在你可以列出->next->next->next->…->next。这实际上是多个指针指向:*(*(.(*(*(* * *名单).next).next. next.).next).next).next。下一个基本上是noop当它是结构的第一个元素时,我们可以把它想象成***。***ptr。

There is really no limit on this because the links can be traversed with a loop rather than a giant expression like this, and moreover, the structure can easily be made circular.

这是没有限制的,因为链接可以通过一个循环而不是像这样一个巨大的表达式来访问,而且结构很容易被做成圆形。

Thus, in other words, linked lists may be the ultimate example of adding another level of indirection to solve a problem, since you're doing it dynamically with every push operation. :)

因此,换句话说,链表可能是为解决问题而添加另一个间接层次的最终示例,因为您在每次推操作中都动态地执行它。:)

#3


79  

Theoretically:

从理论上讲:

You can have as many levels of indirections as you want.

你可以有你想要的任何层次的间接方向。

Practically:

实际:

Of course, nothing that consumes memory can be indefinite, there will be limitations due to resources available on the host environment. So practically there is a maximum limit to what an implementation can support and the implementation shall document it appropriately. So in all such artifacts, the standard does not specify the maximum limit, but it does specify the lower limits.

当然,任何消耗内存的东西都可以是不确定的,由于主机环境上可用的资源,所以会有一些限制。因此,实际上,实现可以支持的内容有一个最大限度,实现应该适当地记录它。因此,在所有这些工件中,标准没有指定最大限度,但它确实指定了最低限度。

Here's the reference:

参考:

C99 Standard 5.2.4.1 Translation limits:

C99标准5.2.4.1翻译限制:

— 12 pointer, array, and function declarators (in any combinations) modifying an arithmetic, structure, union, or void type in a declaration.

- 12个指针、数组和函数声明符(任何组合)修改声明中的算术、结构、联合或空类型。

This specifies the lower limit that every implementation must support. Note that in a footenote the standard further says:

这指定了每个实现必须支持的下限。注意,在footenote中,标准进一步规定:

18) Implementations should avoid imposing fixed translation limits whenever possible.

18)实现应该尽量避免强加固定的翻译限制。

#4


72  

As people have said, no limit "in theory". However, out of interest I ran this with g++ 4.1.2, and it worked with size up to 20,000. Compile was pretty slow though, so I didn't try higher. So I'd guess g++ doesn't impose any limit either. (Try setting size = 10 and looking in ptr.cpp if it's not immediately obvious.)

正如人们所说,“理论上”没有极限。但是,出于兴趣,我使用了g++ 4.1.2,它的大小为20,000。编译非常慢,所以我没有尝试更高的版本。所以我猜g+也没有任何限制。(尝试设置大小= 10,查看ptr。如果不是很明显的话。

g++ create.cpp -o create ; ./create > ptr.cpp ; g++ ptr.cpp -o ptr ; ./ptr

g++创造。cpp - o创建;/创建> ptr。cpp;g++ ptr。cpp - o ptr;/ ptr。

create.cpp

create.cpp

#include <iostream>

int main()
{
    const int size = 200;
    std::cout << "#include <iostream>\n\n";
    std::cout << "int main()\n{\n";
    std::cout << "    int i0 = " << size << ";";
    for (int i = 1; i < size; ++i)
    {
        std::cout << "    int ";
        for (int j = 0; j < i; ++j) std::cout << "*";
        std::cout << " i" << i << " = &i" << i-1 << ";\n";
    }
    std::cout << "    std::cout << ";
    for (int i = 1; i < size; ++i) std::cout << "*";
    std::cout << "i" << size-1 << " << \"\\n\";\n";
    std::cout << "    return 0;\n}\n";
    return 0;
}

#5


60  

Sounds fun to check.

听起来有趣的检查。

  • Visual Studio 2010 (on Windows 7), you can have 1011 levels before getting this error:

    Visual Studio 2010(在Windows 7上),在得到这个错误之前,您可以拥有1011个级别:

    fatal error C1026: parser stack overflow, program too complex

    致命错误C1026:解析器栈溢出,程序太复杂

  • gcc (Ubuntu), 100k+ * without a crash ! I guess the hardware is the limit here.

    gcc (Ubuntu), 100k+ *没有崩溃!我想硬件是这里的极限。

(tested with just a variable declaration)

(仅用变量声明进行测试)

#6


26  

There is no limit, check example here.

这里没有限制,这里有一个例子。

The answer depends on what you mean by "levels of pointers." If you mean "How many levels of indirection can you have in a single declaration?" the answer is "At least 12."

答案取决于你所说的“指针水平”。如果你的意思是“在一个声明中你能有多少层间接指向?”

int i = 0;

int *ip01 = & i;

int **ip02 = & ip01;

int ***ip03 = & ip02;

int ****ip04 = & ip03;

int *****ip05 = & ip04;

int ******ip06 = & ip05;

int *******ip07 = & ip06;

int ********ip08 = & ip07;

int *********ip09 = & ip08;

int **********ip10 = & ip09;

int ***********ip11 = & ip10;

int ************ip12 = & ip11;

************ip12 = 1; /* i = 1 */

If you mean "How many levels of pointer can you use before the program gets hard to read," that's a matter of taste, but there is a limit. Having two levels of indirection (a pointer to a pointer to something) is common. Any more than that gets a bit harder to think about easily; don't do it unless the alternative would be worse.

如果您的意思是“在程序难以阅读之前,您可以使用多少级别的指针”,这是一个味道问题,但这是有限度的。有两个层次的间接(指向某物的指针)是很常见的。比这更让人难以理解;不要这样做,除非有更好的选择。

If you mean "How many levels of pointer indirection can you have at runtime," there's no limit. This point is particularly important for circular lists, in which each node points to the next. Your program can follow the pointers forever.

如果您的意思是“在运行时可以有多少级别的指针间接指向”,则没有限制。这一点对于循环列表尤其重要,其中每个节点指向下一个节点。你的程序可以永远遵循指针。

#7


22  

It's actually even funnier with pointer to functions.

它甚至更有趣的指针指向函数。

#include <cstdio>

typedef void (*FuncType)();

static void Print() { std::printf("%s", "Hello, World!\n"); }

int main() {
  FuncType const ft = &Print;
  ft();
  (*ft)();
  (**ft)();
  /* ... */
}

As illustrated here this gives:

如下图所示:

Hello, World!
Hello, World!
Hello, World!

你好,世界!你好,世界!你好,世界!

And it does not involve any runtime overhead, so you can probably stack them as much as you want... until your compiler chokes on the file.

而且它不涉及任何运行时开销,所以您可以尽可能多地堆叠它们……直到你的编译器在文件上阻塞。

#8


18  

There is no limit. A pointer is a chunk of memory whose contents are an address.
As you said

没有限制。指针是一个内存块,其内容是一个地址。像你说的

int a = 10;
int *p = &a;

A pointer to a pointer is also a variable which contains an address of another pointer.

指向指针的指针也是一个变量,它包含另一个指针的地址。

int **q = &p;

Here q is pointer to pointer holding the address of p which is already holding the address of a.

这里q是指向指针的指针,它包含p的地址,p已经包含了a的地址。

There is nothing particularly special about a pointer to a pointer.
So there is no limit on chain of poniters which are holding the address of another pointer.
ie.

指针指向指针没有什么特别的地方。因此,对于持有另一个指针地址的poniters链没有限制。ie。

 int **************************************************************************z;

is allowed.

是被允许的。

#9


13  

Note that there are two possible questions here: how many levels of pointer indirection we can achieve in a C type, and how many levels of pointer indirection we can stuff into a single declarator.

注意,这里有两个可能的问题:在C类型中可以实现多少级别的指针间接指向,以及可以将多少级别的指针间接指向放入一个声明符中。

The C standard allows a maximum to be imposed on the former (and gives a minimum value for that). But that can be circumvented via multiple typedef declarations:

C标准允许对前者施加最大值(并给出最小值)。但这可以通过多个类型定义声明来规避:

typedef int *type0;
typedef type0 *type1;
typedef type1 *type2; /* etc */

So ultimately, this is an implementation issue connected to the idea of how big/complex can a C program be made before it is rejected, which is very compiler specific.

所以最终,这是一个实现问题与C程序在被拒绝之前的大小/复杂程度有关,这是一个特定于编译器的问题。

#10


11  

Every C++ developer should have heard of the (in)famous Three star programmer

每个c++开发人员都应该听说过著名的三星级程序员

And there really seems to be some magic "pointer barrier" that has to be camouflaged

而且似乎真的有某种神奇的“指针屏障”必须加以伪装

Quote from C2:

C2的报价:

Three Star Programmer

三个明星程序员

A rating system for C-programmers. The more indirect your pointers are (i.e. the more "*" before your variables), the higher your reputation will be. No-star C-programmers are virtually non-existent, as virtually all non-trivial programs require use of pointers. Most are one-star programmers. In the old times (well, I'm young, so these look like old times to me at least), one would occasionally find a piece of code done by a three-star programmer and shiver with awe. Some people even claimed they'd seen three-star code with function pointers involved, on more than one level of indirection. Sounded as real as UFOs to me.

c程序员的评级系统。你的指针越间接(例如,变量前的“*”越多),你的声誉就越高。无星c程序员实际上是不存在的,因为几乎所有的非平凡程序都需要使用指针。大多数都是一星的程序员。在过去(好吧,我还年轻,至少在我看来,这些都是过去时了),人们偶尔会发现一段由一个三星级程序员完成的代码,然后敬畏地颤抖起来。一些人甚至声称他们已经看到了包含有函数指针的三星级代码,在一个以上的间接的水平上。听起来像ufo一样真实。

#11


2  

Rule 17.5 of the 2004 MISRA C standard prohibits more than 2 levels of pointer indirection.

2004年MISRA C标准第17.5条禁止超过2级的指针间接指向。

#12


2  

I'd like to point out that producing a type with an arbitrary number of *'s is something that can happen with template metaprogramming. I forget what I was doing exactly, but it was suggested that I could produce new distinct types that have some kind of meta maneuvering between them by using recursive T* types.

我想指出的是,使用任意数量的*'来生成一个类型的类型是可以通过模板元编程实现的。我忘记我在做什么了,但是有人建议我可以通过使用递归T*类型来产生新的不同类型,它们之间有某种元机动。

Template Metaprogramming is a slow descent into madness, so it is not necessary to make excuses when generating a type with several thousand level of indirection. It's just a handy way to map peano integers, for example, onto template expansion as a functional language.

模板元编程是一种缓慢地陷入疯狂的过程,因此,在生成具有数千级间接的类型时,没有必要找借口。它只是一种方便的方法,可以将peano整数映射到作为函数语言的模板展开。

#13


1  

There isn't such a thing like real limit but limit exists. All pointers are variables that are usually storing in stack not heap. Stack is usually small (it is possible to change its size during some linking). So lets say you have 4MB stack, what is quite normal size. And lets say we have pointer which is 4 bytes size (pointer sizes are not the same depending on architecture, target and compiler settings).

没有真正的极限,但是有极限。所有指针都是通常存储在堆栈中的变量,而不是堆。堆栈通常很小(可以在某些链接期间更改其大小)。假设你有4MB的堆栈,这是正常的大小。假设我们有一个4字节大小的指针(根据体系结构、目标和编译器设置,指针大小不同)。

In this case 4 MB / 4 b = 1024 so possible maximum number would be 1048576, but we shouldn't ignore the fact that some other stuff is in stack.

在这种情况下,4 MB / 4 b = 1024,所以可能的最大数量是1048576,但是我们不应该忽略一些其他东西在堆栈中的事实。

However some compilers may have maximum number of pointer chain, but the limit is stack size. So if you increase stack size during linking with infinity and have machine with infinity memory which runs OS which handles that memory so you will have unlimited pointer chain.

但是,有些编译器可能具有最大的指针链数,但是限制是堆栈大小。如果你在连接到无穷时增加堆栈大小并且有一台机器有无限的内存运行操作系统来处理这些内存那么你就会有无限的指针链。

If you use int *ptr = new int; and put your pointer into heap, that is not so usual way limit would be heap size, not stack.

如果你使用int *ptr = new int;将指针放入堆中,通常情况下,限制是堆大小,而不是堆栈。

EDIT Just realize that infinity / 2 = infinity. If machine has more memory so the pointer size increases. So if memory is infinity and size of pointer is infinity, so it is bad news... :)

编辑只要意识到无限/ 2 =无穷大。如果机器有更多的内存,那么指针的大小就会增加。如果内存是无穷大,指针的大小是无穷大,那么这是个坏消息。:)

#14


0  

It depends on the place where you store pointers. If they are in stack you have quite low limit. If you store it in heap, you limit is much much much higher.

这取决于存储指针的位置。如果他们在堆栈,你有相当低的极限。如果您将它存储在堆中,那么您的限制要高得多。

Look at this program:

看看这个程序:

#include <iostream>

const int CBlockSize = 1048576;

int main() 
{
    int number = 0;
    int** ptr = new int*[CBlockSize];

    ptr[0] = &number;

    for (int i = 1; i < CBlockSize; ++i)
        ptr[i] = reinterpret_cast<int *> (&ptr[i - 1]);

    for (int i = CBlockSize-1; i >= 0; --i)
        std::cout << i << " " << (int)ptr[i] << "->" << *ptr[i] << std::endl;

    return 0;
}

It creates 1M pointers and at the shows what point to what it is easy to notice what the chain goes to the first variable number.

它创建了1M个指针,在这里可以很容易地看到链走向第一个变量。

BTW. It uses 92K of RAM so just imagine how deep you can go.

顺便说一句。它使用了92K的RAM,想象一下你能走多深。

#1


382  

The C standard specifies the lower limit:

C标准规定了下限:

5.2.4.1 Translation limits

276 The implementation shall be able to translate and execute at least one program that contains at least one instance of every one of the following limits: [...]

该实现应能够翻译并执行至少一个程序,该程序包含以下任何一个限制的至少一个实例:[…]

279 — 12 pointer, array, and function declarators (in any combinations) modifying an arithmetic, structure, union, or void type in a declaration

279 - 12指针、数组和函数声明符(任何组合)修改声明中的算术、结构、联合或空类型

The upper limit is implementation specific.

上限是特定于实现的。

#2


145  

Actually, C programs commonly make use of infinite pointer indirection. One or two static levels are common. Triple indirection is rare. But infinite is very common.

实际上,C程序通常使用无限指针间接。一个或两个静态级别是常见的。三重间接是罕见的。但无穷大是很常见的。

Infinite pointer indirection is achieved with the help of a struct, of course, not with a direct declarator, which would be impossible. And a struct is needed so that you can include other data in this structure at the different levels where this can terminate.

无限指针间接是在结构体的帮助下实现的,当然不是通过直接的声明者,这是不可能的。需要一个结构体,这样你就可以在这个结构中包含其他的数据,在不同的层次上,这些数据可以终止。

struct list { struct list *next; ... };

now you can have list->next->next->next->...->next. This is really just multiple pointer indirections: *(*(..(*(*(*list).next).next).next...).next).next. And the .next is basically a noop when it's the first member of the structure, so we can imagine this as ***..***ptr.

现在你可以列出->next->next->next->…->next。这实际上是多个指针指向:*(*(.(*(*(* * *名单).next).next. next.).next).next).next。下一个基本上是noop当它是结构的第一个元素时,我们可以把它想象成***。***ptr。

There is really no limit on this because the links can be traversed with a loop rather than a giant expression like this, and moreover, the structure can easily be made circular.

这是没有限制的,因为链接可以通过一个循环而不是像这样一个巨大的表达式来访问,而且结构很容易被做成圆形。

Thus, in other words, linked lists may be the ultimate example of adding another level of indirection to solve a problem, since you're doing it dynamically with every push operation. :)

因此,换句话说,链表可能是为解决问题而添加另一个间接层次的最终示例,因为您在每次推操作中都动态地执行它。:)

#3


79  

Theoretically:

从理论上讲:

You can have as many levels of indirections as you want.

你可以有你想要的任何层次的间接方向。

Practically:

实际:

Of course, nothing that consumes memory can be indefinite, there will be limitations due to resources available on the host environment. So practically there is a maximum limit to what an implementation can support and the implementation shall document it appropriately. So in all such artifacts, the standard does not specify the maximum limit, but it does specify the lower limits.

当然,任何消耗内存的东西都可以是不确定的,由于主机环境上可用的资源,所以会有一些限制。因此,实际上,实现可以支持的内容有一个最大限度,实现应该适当地记录它。因此,在所有这些工件中,标准没有指定最大限度,但它确实指定了最低限度。

Here's the reference:

参考:

C99 Standard 5.2.4.1 Translation limits:

C99标准5.2.4.1翻译限制:

— 12 pointer, array, and function declarators (in any combinations) modifying an arithmetic, structure, union, or void type in a declaration.

- 12个指针、数组和函数声明符(任何组合)修改声明中的算术、结构、联合或空类型。

This specifies the lower limit that every implementation must support. Note that in a footenote the standard further says:

这指定了每个实现必须支持的下限。注意,在footenote中,标准进一步规定:

18) Implementations should avoid imposing fixed translation limits whenever possible.

18)实现应该尽量避免强加固定的翻译限制。

#4


72  

As people have said, no limit "in theory". However, out of interest I ran this with g++ 4.1.2, and it worked with size up to 20,000. Compile was pretty slow though, so I didn't try higher. So I'd guess g++ doesn't impose any limit either. (Try setting size = 10 and looking in ptr.cpp if it's not immediately obvious.)

正如人们所说,“理论上”没有极限。但是,出于兴趣,我使用了g++ 4.1.2,它的大小为20,000。编译非常慢,所以我没有尝试更高的版本。所以我猜g+也没有任何限制。(尝试设置大小= 10,查看ptr。如果不是很明显的话。

g++ create.cpp -o create ; ./create > ptr.cpp ; g++ ptr.cpp -o ptr ; ./ptr

g++创造。cpp - o创建;/创建> ptr。cpp;g++ ptr。cpp - o ptr;/ ptr。

create.cpp

create.cpp

#include <iostream>

int main()
{
    const int size = 200;
    std::cout << "#include <iostream>\n\n";
    std::cout << "int main()\n{\n";
    std::cout << "    int i0 = " << size << ";";
    for (int i = 1; i < size; ++i)
    {
        std::cout << "    int ";
        for (int j = 0; j < i; ++j) std::cout << "*";
        std::cout << " i" << i << " = &i" << i-1 << ";\n";
    }
    std::cout << "    std::cout << ";
    for (int i = 1; i < size; ++i) std::cout << "*";
    std::cout << "i" << size-1 << " << \"\\n\";\n";
    std::cout << "    return 0;\n}\n";
    return 0;
}

#5


60  

Sounds fun to check.

听起来有趣的检查。

  • Visual Studio 2010 (on Windows 7), you can have 1011 levels before getting this error:

    Visual Studio 2010(在Windows 7上),在得到这个错误之前,您可以拥有1011个级别:

    fatal error C1026: parser stack overflow, program too complex

    致命错误C1026:解析器栈溢出,程序太复杂

  • gcc (Ubuntu), 100k+ * without a crash ! I guess the hardware is the limit here.

    gcc (Ubuntu), 100k+ *没有崩溃!我想硬件是这里的极限。

(tested with just a variable declaration)

(仅用变量声明进行测试)

#6


26  

There is no limit, check example here.

这里没有限制,这里有一个例子。

The answer depends on what you mean by "levels of pointers." If you mean "How many levels of indirection can you have in a single declaration?" the answer is "At least 12."

答案取决于你所说的“指针水平”。如果你的意思是“在一个声明中你能有多少层间接指向?”

int i = 0;

int *ip01 = & i;

int **ip02 = & ip01;

int ***ip03 = & ip02;

int ****ip04 = & ip03;

int *****ip05 = & ip04;

int ******ip06 = & ip05;

int *******ip07 = & ip06;

int ********ip08 = & ip07;

int *********ip09 = & ip08;

int **********ip10 = & ip09;

int ***********ip11 = & ip10;

int ************ip12 = & ip11;

************ip12 = 1; /* i = 1 */

If you mean "How many levels of pointer can you use before the program gets hard to read," that's a matter of taste, but there is a limit. Having two levels of indirection (a pointer to a pointer to something) is common. Any more than that gets a bit harder to think about easily; don't do it unless the alternative would be worse.

如果您的意思是“在程序难以阅读之前,您可以使用多少级别的指针”,这是一个味道问题,但这是有限度的。有两个层次的间接(指向某物的指针)是很常见的。比这更让人难以理解;不要这样做,除非有更好的选择。

If you mean "How many levels of pointer indirection can you have at runtime," there's no limit. This point is particularly important for circular lists, in which each node points to the next. Your program can follow the pointers forever.

如果您的意思是“在运行时可以有多少级别的指针间接指向”,则没有限制。这一点对于循环列表尤其重要,其中每个节点指向下一个节点。你的程序可以永远遵循指针。

#7


22  

It's actually even funnier with pointer to functions.

它甚至更有趣的指针指向函数。

#include <cstdio>

typedef void (*FuncType)();

static void Print() { std::printf("%s", "Hello, World!\n"); }

int main() {
  FuncType const ft = &Print;
  ft();
  (*ft)();
  (**ft)();
  /* ... */
}

As illustrated here this gives:

如下图所示:

Hello, World!
Hello, World!
Hello, World!

你好,世界!你好,世界!你好,世界!

And it does not involve any runtime overhead, so you can probably stack them as much as you want... until your compiler chokes on the file.

而且它不涉及任何运行时开销,所以您可以尽可能多地堆叠它们……直到你的编译器在文件上阻塞。

#8


18  

There is no limit. A pointer is a chunk of memory whose contents are an address.
As you said

没有限制。指针是一个内存块,其内容是一个地址。像你说的

int a = 10;
int *p = &a;

A pointer to a pointer is also a variable which contains an address of another pointer.

指向指针的指针也是一个变量,它包含另一个指针的地址。

int **q = &p;

Here q is pointer to pointer holding the address of p which is already holding the address of a.

这里q是指向指针的指针,它包含p的地址,p已经包含了a的地址。

There is nothing particularly special about a pointer to a pointer.
So there is no limit on chain of poniters which are holding the address of another pointer.
ie.

指针指向指针没有什么特别的地方。因此,对于持有另一个指针地址的poniters链没有限制。ie。

 int **************************************************************************z;

is allowed.

是被允许的。

#9


13  

Note that there are two possible questions here: how many levels of pointer indirection we can achieve in a C type, and how many levels of pointer indirection we can stuff into a single declarator.

注意,这里有两个可能的问题:在C类型中可以实现多少级别的指针间接指向,以及可以将多少级别的指针间接指向放入一个声明符中。

The C standard allows a maximum to be imposed on the former (and gives a minimum value for that). But that can be circumvented via multiple typedef declarations:

C标准允许对前者施加最大值(并给出最小值)。但这可以通过多个类型定义声明来规避:

typedef int *type0;
typedef type0 *type1;
typedef type1 *type2; /* etc */

So ultimately, this is an implementation issue connected to the idea of how big/complex can a C program be made before it is rejected, which is very compiler specific.

所以最终,这是一个实现问题与C程序在被拒绝之前的大小/复杂程度有关,这是一个特定于编译器的问题。

#10


11  

Every C++ developer should have heard of the (in)famous Three star programmer

每个c++开发人员都应该听说过著名的三星级程序员

And there really seems to be some magic "pointer barrier" that has to be camouflaged

而且似乎真的有某种神奇的“指针屏障”必须加以伪装

Quote from C2:

C2的报价:

Three Star Programmer

三个明星程序员

A rating system for C-programmers. The more indirect your pointers are (i.e. the more "*" before your variables), the higher your reputation will be. No-star C-programmers are virtually non-existent, as virtually all non-trivial programs require use of pointers. Most are one-star programmers. In the old times (well, I'm young, so these look like old times to me at least), one would occasionally find a piece of code done by a three-star programmer and shiver with awe. Some people even claimed they'd seen three-star code with function pointers involved, on more than one level of indirection. Sounded as real as UFOs to me.

c程序员的评级系统。你的指针越间接(例如,变量前的“*”越多),你的声誉就越高。无星c程序员实际上是不存在的,因为几乎所有的非平凡程序都需要使用指针。大多数都是一星的程序员。在过去(好吧,我还年轻,至少在我看来,这些都是过去时了),人们偶尔会发现一段由一个三星级程序员完成的代码,然后敬畏地颤抖起来。一些人甚至声称他们已经看到了包含有函数指针的三星级代码,在一个以上的间接的水平上。听起来像ufo一样真实。

#11


2  

Rule 17.5 of the 2004 MISRA C standard prohibits more than 2 levels of pointer indirection.

2004年MISRA C标准第17.5条禁止超过2级的指针间接指向。

#12


2  

I'd like to point out that producing a type with an arbitrary number of *'s is something that can happen with template metaprogramming. I forget what I was doing exactly, but it was suggested that I could produce new distinct types that have some kind of meta maneuvering between them by using recursive T* types.

我想指出的是,使用任意数量的*'来生成一个类型的类型是可以通过模板元编程实现的。我忘记我在做什么了,但是有人建议我可以通过使用递归T*类型来产生新的不同类型,它们之间有某种元机动。

Template Metaprogramming is a slow descent into madness, so it is not necessary to make excuses when generating a type with several thousand level of indirection. It's just a handy way to map peano integers, for example, onto template expansion as a functional language.

模板元编程是一种缓慢地陷入疯狂的过程,因此,在生成具有数千级间接的类型时,没有必要找借口。它只是一种方便的方法,可以将peano整数映射到作为函数语言的模板展开。

#13


1  

There isn't such a thing like real limit but limit exists. All pointers are variables that are usually storing in stack not heap. Stack is usually small (it is possible to change its size during some linking). So lets say you have 4MB stack, what is quite normal size. And lets say we have pointer which is 4 bytes size (pointer sizes are not the same depending on architecture, target and compiler settings).

没有真正的极限,但是有极限。所有指针都是通常存储在堆栈中的变量,而不是堆。堆栈通常很小(可以在某些链接期间更改其大小)。假设你有4MB的堆栈,这是正常的大小。假设我们有一个4字节大小的指针(根据体系结构、目标和编译器设置,指针大小不同)。

In this case 4 MB / 4 b = 1024 so possible maximum number would be 1048576, but we shouldn't ignore the fact that some other stuff is in stack.

在这种情况下,4 MB / 4 b = 1024,所以可能的最大数量是1048576,但是我们不应该忽略一些其他东西在堆栈中的事实。

However some compilers may have maximum number of pointer chain, but the limit is stack size. So if you increase stack size during linking with infinity and have machine with infinity memory which runs OS which handles that memory so you will have unlimited pointer chain.

但是,有些编译器可能具有最大的指针链数,但是限制是堆栈大小。如果你在连接到无穷时增加堆栈大小并且有一台机器有无限的内存运行操作系统来处理这些内存那么你就会有无限的指针链。

If you use int *ptr = new int; and put your pointer into heap, that is not so usual way limit would be heap size, not stack.

如果你使用int *ptr = new int;将指针放入堆中,通常情况下,限制是堆大小,而不是堆栈。

EDIT Just realize that infinity / 2 = infinity. If machine has more memory so the pointer size increases. So if memory is infinity and size of pointer is infinity, so it is bad news... :)

编辑只要意识到无限/ 2 =无穷大。如果机器有更多的内存,那么指针的大小就会增加。如果内存是无穷大,指针的大小是无穷大,那么这是个坏消息。:)

#14


0  

It depends on the place where you store pointers. If they are in stack you have quite low limit. If you store it in heap, you limit is much much much higher.

这取决于存储指针的位置。如果他们在堆栈,你有相当低的极限。如果您将它存储在堆中,那么您的限制要高得多。

Look at this program:

看看这个程序:

#include <iostream>

const int CBlockSize = 1048576;

int main() 
{
    int number = 0;
    int** ptr = new int*[CBlockSize];

    ptr[0] = &number;

    for (int i = 1; i < CBlockSize; ++i)
        ptr[i] = reinterpret_cast<int *> (&ptr[i - 1]);

    for (int i = CBlockSize-1; i >= 0; --i)
        std::cout << i << " " << (int)ptr[i] << "->" << *ptr[i] << std::endl;

    return 0;
}

It creates 1M pointers and at the shows what point to what it is easy to notice what the chain goes to the first variable number.

它创建了1M个指针,在这里可以很容易地看到链走向第一个变量。

BTW. It uses 92K of RAM so just imagine how deep you can go.

顺便说一句。它使用了92K的RAM,想象一下你能走多深。

相关文章