1

I was doing an exercise from K & R which requires me to sort lines in an array supplied by the main function. I declared an array like so: char line[MAXLINES * MAXLEN], where MAXLINES and MAXLEN are constants 5000 and 1000 respectively. I declared this inside the main function. Apparently this just crashes the program. I am using the Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x86 on Windows 10, 64 bit. However, when I declare the array with the static keyword, like so: static char line[MAXLINES * MAXLEN], it works. From what I know about the static keyword, when applied to internal variables, it provides permanent storage within a function. But from that definition of the static keyword, it is unclear to me why the character array declared without the static keyword would crash. Note that an array of smaller size works fine without the static keyword. Is there a limit to what the size of the array can be?

user1720897
  • 1,216
  • 3
  • 12
  • 27

1 Answers1

2

Is there a limit to what the size of the array can be?

There is no fixed limit set by C standard, but there is always a practical limit since memory is not infinite. And that practical limit is normally much less for local variables than global or static or dynamically allocated memory.

Local variables and parameters of a function are created on memory location called call stack. Exactly how the call stack is implemented is platform/architecture/compiler dependent, but normally it can't store very large amount of data. When you declare a variable static, it doesn't get stored in stack memory.

Now since 5000 * 1000 = 5000000 is a large amount of data, the stack on your system is not able to hold it and thus leading to a crash. Since static are stored in separate location you were able to create it without crashing the program.

Further details on exactly where static variables are stored can be found in this SO question: Where are static variables stored (in C/C++)?

Edit: as pointed in the comment, this call stack is not a requirement from C language, i.e. C doesn't enforce an implementation to use call stack. This is a matter of implementation. Though call stack is common in implementations, there might be systems which handles memory differently.

Community
  • 1
  • 1
taskinoor
  • 45,586
  • 12
  • 116
  • 142
  • There is no stack in the C language. – too honest for this site Apr 23 '17 at 11:41
  • @Olaf what do you mean? Just to clarify, I was talking about call stack. – taskinoor Apr 23 '17 at 11:42
  • 2
    I know what oyu mean. There is no requirement for an implementation to use a specific structure to store automatic or static variables. There are implementations which don't use a stack for instance. – too honest for this site Apr 23 '17 at 11:55
  • @Olaf thanks for clarifying. Yes, I got your point too. Edited the answer to make this clear. – taskinoor Apr 23 '17 at 12:01
  • Noticed something (maybe unique with some systems or OS/Compiler - I don't know) , but I am using visual studio on widows 10 64-bit and if I don't use static to declare a char array, if I manipulate the array, and return it outside the function, it shows 'garbage'. This appears to have nothing to do with array size as I only have a 9 char string (8 + terminator. It appears the static keyword ensures that the memory space allocated remains available and is not overwritten somehow at some point outside the function. Looks like I have no choice but to always use static for declaring arrays – blessedk Jan 09 '22 at 19:31