Stack Overflow - Very Large Stack Variables

Very Large Stack Variables

The other major cause of a stack overflow results from an attempt to allocate more memory on the stack than will fit, for example by creating local array variables that are too large. For this reason some authors recommend that arrays larger than a few kilobytes should be allocated dynamically instead of as a local variable.

An example of a very large stack variable in C:

int foo { double x; }

The declared array consumes 8 megabytes of data (assuming each double is 8 bytes); 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.

Stack overflows are made worse by anything that reduces the effective stack size of a given program. For example, the same program being run without multiple threads might work fine, but as soon as multi-threading is enabled the program will crash. This is because most programs with threads have less stack space per thread than a program with no threading support. Similarly, people new to kernel development are usually discouraged from using recursive algorithms or large stack buffers.

Read more about this topic:  Stack Overflow

Famous quotes containing the words large, stack and/or variables:

    The best hopes of any community rest upon that class of its gifted young men who are not encumbered with large possessions.... I now speak of extensive scholarship and ripe culture in science and art.... It is not large possessions, it is large expectations, or rather large hopes, that stimulate the ambition of the young.
    Rutherford Birchard Hayes (1822–1893)

    What is a farm but a mute gospel? The chaff and the wheat, weeds and plants, blight, rain, insects, sun—it is a sacred emblem from the first furrow of spring to the last stack which the snow of winter overtakes in the fields.
    Ralph Waldo Emerson (1803–1882)

    The variables are surprisingly few.... One can whip or be whipped; one can eat excrement or quaff urine; mouth and private part can be meet in this or that commerce. After which there is the gray of morning and the sour knowledge that things have remained fairly generally the same since man first met goat and woman.
    George Steiner (b. 1929)