Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the C++ standard say about stack overflow?

I took a look at the draft C++0x standard, and as far as I can tell there is nothing about stack overflow in there. Searching for "stack overflow" yields no results, and searching for "stack" I've only gotten references to stack unwinding and std::stack. Does that mean that there cannot be a conforming implementation of the C++ standard, since there is no mechanism allowed for handling the error when memory is exhausted by a local object such as a huge local array?

The answers to this question indicate that at least the C standard does not mention stack overflow.

To make the question concrete, consider this program

// Program A
int identity(int a) {
  if (a == 0)
    return 0;
  char hugeArray[1024 * 1024 * 1024]; // 1 GB
  return identity(a - 1) + 1;
}
int main() {
  return f(1024 * 1024 * 1024);
}

and this program

// program B
int main() {
  return 1024 * 1024 * 1024;
}

I think the C++ standard does not allow any C++ implementation to do something observably different on these two programs. In reality program A won't run on any modern machine as it is allocating an exabyte of memory on the stack (imagine the function actually used the huge array so the compiler can't silently remove it to no ill effect). Does the C++ standard allow program A to fail?

Edit: The question is not whether the standard should define what happens on stack overflow, the question is what it says, if anything.

like image 661
Bjarke H. Roune Avatar asked Jul 05 '11 23:07

Bjarke H. Roune


People also ask

Does C have stack overflow?

On a C implementation with 8 byte double-precision floats, the declared array consumes 8 megabytes of data; if this is more memory than is available on the stack (as set by thread creation parameters or operating system limits), a stack overflow will occur.

What is stack overflow condition in C?

What is stack overflow? A stack overflow is a type of buffer overflow error that occurs when a computer program tries to use more memory space in the call stack than has been allocated to that stack.

What is stack underflow in C?

As stated in the comments: Stack underflow means having the stack pointer to point to an address below the beginning of the stack ("below" for architectures where the stack grows from low to high). c.

Does C have truthy values?

C does not have boolean data types, and normally uses integers for boolean testing. Zero is used to represent false, and One is used to represent true. For interpretation, Zero is interpreted as false and anything non-zero is interpreted as true.


2 Answers

I'm not sure if this is what you're looking for, but in Appendix B of the C++03 ISO standard there's the following notice:

  1. Because computers are finite, C++ implementations are inevitably limited in the size of the programs they can successfully process. Every implementation shall document those limitations where known. This documentation may cite fixed limits where they exist, say how to compute variable limits as a function of available resources, or say that fixed limits do not exist or are unknown.
  2. The limits may constrain quantities that include those described below or others.

(My emphasis) I take this to mean it is perfectly legal for the compiler to allow one of those functions to work while failing another, provided that the compiler states what limitations are in place and how they are computed from the resources the system has available.

like image 117
templatetypedef Avatar answered Oct 14 '22 00:10

templatetypedef


Behavior is undefined because the Standard does not define what happens with a program that exceeds resource limits. Note that there are recommended limits in Annex B of the spec. That annex is non-normative though and an implementation can ignore that annex, including having different limits than specified there. In 1.4 [intro.compliance], the spec says

If a program contains no violations of the rules in this International Standard, a conforming implementation shall, within its resource limits, accept and correctly execute that program.

There is nothing that says what shall happen with a program that contains no violation of the rules in the IS but that can't be accepted and correctly executed within the resource limits of the implementation. Hence behavior is undefined for such a case.

like image 22
Johannes Schaub - litb Avatar answered Oct 14 '22 01:10

Johannes Schaub - litb