2

This is just out of curiosity. I read an old joke somewhere that made reference to an inordinately huge number of variables being declared in a function and the VC++ compiler wouldn't compile.

I would assume it would depend on the size of the integer used to keep reference of the variables, no ? Or is this no longer the case and you can have as many local variables as you'd want ?

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
mGamma
  • 182
  • 2
  • 8
  • If it's an old joke, it's undoubtedly about a very old version of Visual C++... – Cody Gray - on strike Jun 13 '13 at 20:02
  • probably true. It was about some engineers trying to port linux code to VC++ and getting stuck because the compiler wouldn't allow for more than a few thousand variables in a function. – mGamma Jun 23 '13 at 21:46

1 Answers1

3

Without a reference it is hard to know what the original issue was but it looks like the default stack size in Visual Studio is 1 MB but you can set it using /F. This will indeed limit the number of local variables you can declare, this is usually an issue when someone wants to declare a really large array on the stack.

Community
  • 1
  • 1
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
  • Thanks for the answer Shafik, though my question was about the number of variables, not the total memory on the stack. That may sound weird but that was the point of the joke... – mGamma Jun 23 '13 at 21:44
  • @mGamma Well if you have a lot of arguments to pass to a function they will end up on the stack as well, so this will also effect that as well: http://unixwiz.net/techtips/win32-callconv.html – Shafik Yaghmour Jun 24 '13 at 00:03
  • Let me put it another way. If I have a type that takes up one bit of memory and I declared a million variables of that type (not an array, each individual one declared with a different name) in a function, Would the vc++ compiler (or for that matter any compiler) allow it ? assumption : there is enough stack space (i.e. >= 1 MB) I know this is a contrived situation but as I mentioned this is just out of curiosity. Thanks again for your reply. – mGamma Jun 24 '13 at 18:52
  • 1
    @mGamma The minimum is specified in the standard at 256: http://stackoverflow.com/questions/4582012/maximum-number-of-parameters-in-function-declaration and MS seems to conform at least as far back as I can find: http://msdn.microsoft.com/en-us/library/ft39hh4x(v=vs.71).aspx – Shafik Yaghmour Jun 25 '13 at 02:16