Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a limit to the number of #defines that the gcc and VC++ preprocessors can handle?

In discussing design possibilities for a project that has a very large number of constants and bit patterns to be defined, the question came up about how many #defines can a standard compiler handle? I assume it is a very large number, but we were curious to know if there is an actual upper bound.

like image 990
Shannon Nelson Avatar asked Jan 14 '11 22:01

Shannon Nelson


People also ask

What is the limit of the number?

In mathematics, a limit is the value that a function (or sequence) approaches as the input (or index) approaches some value.

What do you mean by limit?

limit, restrict, circumscribe, confine mean to set bounds for. limit implies setting a point or line (as in time, space, speed, or degree) beyond which something cannot or is not permitted to go.

Has no limit or have no limit?

“He has no limits.” or “They have no limits.” You use “have” when the subject of the verb (either “he” or “they” in the examples above) is plural, and also when the subject is “I” or “you” (“I have no limits.”, “You have no limits.”) You have “has” when the subject is singular, unless the subject is “I” or “you”.


2 Answers

For a "standard compiler":

5.2.4.1: "Translation limits"

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

...

4095 macro identifiers simultaneously defined in one preprocessing translation unit

Note the slightly odd way of phrasing the requirement. Implementations could satisfy it by having a single "golden program" which they recognise and compile as a special case, although that would be akin to rigging benchmarks. In practice you can read the standard as saying that if your implementation imposes a limit other than available memory, then that limit should be at least 4095. Beyond 4095 you are relying on implementation-specific behavior to an extent.

Some compilers (Microsoft) impose some implementation limits which are less than the standard says. These are listed somewhere on MSDN I think, but possibly only for C++. As far as C goes, since I'm quoting C99 it might not be relevant to MSVC anyway.

For GCC and MSVC in particular, it shouldn't be too hard to test whether a given implementation imposes an arbitrary limit, perhaps easier than finding it documented :-) Auto-generate files containing nothing but great long lists of #define, see what the preprocessor makes of them.

like image 91
Steve Jessop Avatar answered Nov 03 '22 02:11

Steve Jessop


I have never heard of anyone running out. Ever.

like image 45
Carl Norum Avatar answered Nov 03 '22 02:11

Carl Norum