In this case, double means a variable of type double.

double* means a pointer to a double variable.

double** means a pointer to a pointer to a double variable.

In the case of the function you posted, it is used to create a sort of two-dimensional array of doubles. That is, a pointer to an array of double pointers, and each of those pointers points to an array of pointers.

Answer from Jonathan Wood on Stack Overflow

In this case, double means a variable of type double.

double* means a pointer to a double variable.

double** means a pointer to a pointer to a double variable.

In the case of the function you posted, it is used to create a sort of two-dimensional array of doubles. That is, a pointer to an array of double pointers, and each of those pointers points to an array of pointers.

Answer from Jonathan Wood on Stack Overflow
🌐
Wikipedia
en.wikipedia.org › wiki › Stack_overflow
Stack overflow - Wikipedia
January 7, 2026 - In software, a stack overflow occurs if the call stack pointer exceeds the stack bound. The call stack may consist of a limited amount of address space, often determined at the start of the program. The size of the call stack depends on many factors, including the programming language, machine ...
🌐
PVS-Studio
pvs-studio.com › en › blog › terms › 0087
Stack Overflow
Stack Overflow is a fatal error which is most often found in programs containing recursive functions. It can also be caused by pushing too many local variables into the stack or manual allocation of stack memory.
🌐
Reddit
reddit.com › r/explainlikeimfive › eli5-what does a “stack overflow” mean?
r/explainlikeimfive on Reddit: Eli5-What does a “stack overflow” mean?
August 20, 2022 - When a function calls another function, you get another entry on the stack (and it has to be LIFO, or it would not work). Stack overflow usually means that a program entered infinite regression, which a function calling itself over and over again.
🌐
TutorialsPoint
tutorialspoint.com › heap-overflow-and-stack-overflow-in-c
Heap overflow and Stack overflow in C
C) If a function is called recursively by itself infinite times then stack will be unable to store large number of local variables, so stack overflow will occur −
Find elsewhere
🌐
Medium
medium.com › @lekushlev › understanding-and-demonstrating-stack-overflow-in-c-997975df7b59
Understanding and Demonstrating Stack Overflow in C | by Alef | Medium
January 18, 2024 - A stack overflow is an error that occurs when a program runs out of stack space. The stack is a special region of a computer’s memory that stores temporary variables created by each function (including the main function).
🌐
Javatpoint
javatpoint.com › stack-overflow-in-c
Stack Overflow in C - javatpoint
Stack Overflow in C with Tutorial, C language with programming examples for beginners and professionals covering concepts, c pointers, c structures, c strings etc.
🌐
Quora
quora.com › What-is-a-stack-overflow-in-C-What-causes-it-Can-it-be-avoided-with-the-help-of-any-compiler-option
What is a stack overflow in C? What causes it? Can it be avoided with the help of any compiler option? - Quora
Answer: Stacks are interesting beasts. A stack in simple terms is a region of memory in your program which has been reserved for a particular thread to serve as the “stack” for that thread’s local data and to keep track of arguments and return values for function calls made by the code.
🌐
Medium
medium.com › @maxnegi333 › what-is-stack-overflow-in-c-743a509ba72b
What is Stack Overflow in C?. In C programming, a stack overflow… | by Mayanknegi | Medium
December 13, 2023 - In C programming, a stack overflow occurs when the call stack’s size exceeds its upper bound. The call stack is a portion of memory used to…
Top answer
1 of 9
174

Stack

A stack, in this context, is the last in, first out buffer you place data while your program runs. Last in, first out (LIFO) means that the last thing you put in is always the first thing you get back out - if you push 2 items on the stack, 'A' and then 'B', then the first thing you pop off the stack will be 'B', and the next thing is 'A'.

When you call a function in your code, the next instruction after the function call is stored on the stack, and any storage space that might be overwritten by the function call. The function you call might use up more stack for its own local variables. When it's done, it frees up the local variable stack space it used, then returns to the previous function.

Stack overflow

A stack overflow is when you've used up more memory for the stack than your program was supposed to use. In embedded systems you might only have 256 bytes for the stack, and if each function takes up 32 bytes then you can only have function calls 8 deep - function 1 calls function 2 who calls function 3 who calls function 4 .... who calls function 8 who calls function 9, but function 9 overwrites memory outside the stack. This might overwrite memory, code, etc.

Many programmers make this mistake by calling function A that then calls function B, that then calls function C, that then calls function A. It might work most of the time, but just once the wrong input will cause it to go in that circle forever until the computer recognizes that the stack is overblown.

Recursive functions are also a cause for this, but if you're writing recursively (ie, your function calls itself) then you need to be aware of this and use static/global variables to prevent infinite recursion.

Generally, the OS and the programming language you're using manage the stack, and it's out of your hands. You should look at your call graph (a tree structure that shows from your main what each function calls) to see how deep your function calls go, and to detect cycles and recursion that are not intended. Intentional cycles and recursion need to be artificially checked to error out if they call each other too many times.

Beyond good programming practices, static and dynamic testing, there's not much you can do on these high level systems.

Embedded systems

In the embedded world, especially in high reliability code (automotive, aircraft, space) you do extensive code reviews and checking, but you also do the following:

  • Disallow recursion and cycles - enforced by policy and testing
  • Keep code and stack far apart (code in flash, stack in RAM, and never the twain shall meet)
  • Place guard bands around the stack - empty area of memory that you fill with a magic number (usually a software interrupt instruction, but there are many options here), and hundreds or thousands of times a second you look at the guard bands to make sure they haven't been overwritten.
  • Use memory protection (ie, no execute on the stack, no read or write just outside the stack)
  • Interrupts don't call secondary functions - they set flags, copy data, and let the application take care of processing it (otherwise you might get 8 deep in your function call tree, have an interrupt, and then go out another few functions inside the interrupt, causing the blowout). You have several call trees - one for the main processes, and one for each interrupt. If your interrupts can interrupt each other... well, there be dragons...

High-level languages and systems

But in high level languages run on operating systems:

  • Reduce your local variable storage (local variables are stored on the stack - although compilers are pretty smart about this and will sometimes put big locals on the heap if your call tree is shallow)
  • Avoid or strictly limit recursion
  • Don't break your programs up too far into smaller and smaller functions - even without counting local variables each function call consumes as much as 64 bytes on the stack (32 bit processor, saving half the CPU registers, flags, etc)
  • Keep your call tree shallow (similar to the above statement)

Web servers

It depends on the 'sandbox' you have whether you can control or even see the stack. Chances are good you can treat web servers as you would any other high level language and operating system - it's largely out of your hands, but check the language and server stack you're using. It is possible to blow the stack on your SQL server, for instance.

2 of 9
15

A stack overflow in real code occurs very rarely. Most situations in which it occurs are recursions where the termination has been forgotten. It might however rarely occur in highly nested structures, e.g. particularly large XML documents. The only real help here is to refactor the code to use an explicit stack object instead of the call stack.

🌐
GeeksforGeeks
geeksforgeeks.org › c++ › heap-overflow-stack-overflow
Heap overflow and Stack overflow - GeeksforGeeks
March 10, 2023 - There are two cases in which stack overflow can occur: If we declare large number of local variables or declare an array or matrix or any higher dimensional array of large size can result in overflow of stack.
🌐
Quora
quora.com › What-is-your-way-to-generate-a-stack-overflow-in-a-C-C-program
What is your way to generate a 'stack overflow' in a C/C++ program? - Quora
Answer (1 of 4): I used to use [code ]alloca(~0)[/code] but when I tried that today with Clang the compiler livelocked and nearly took down my computer with it. Turns out that anything above 2^{30} has problems. Looking at the assembly, it appears that the Clang builtin [code ]alloca()[/code] tak...
🌐
All About Circuits
forum.allaboutcircuits.com › home › forums › embedded & programming › programming & languages
Detecting Stack Overflow in C Functions | All About Circuits
May 20, 2024 - I want to share my understanding that the term "stack overflow" has a specific meaning related to the stack memory of a program being exhausted due to excessive recursion or large stack allocations. Writing beyond the end of a stack-allocated buffer in C language doesn't directly cause a stack overflow; instead, it can lead to memory corruption or undefined behavior.
🌐
TechTarget
whatis.techtarget.com › definition › stack-overflow
What is a stack overflow error?
Learn about stack overflow, a buffer error that occurs when programs try to use more memory than has been allocated, which can cause programs to terminate.
🌐
GNU
gnu.org › software › guile › manual › html_node › Stack-Overflow.html
Stack Overflow (Guile Reference Manual)
Every time a Scheme program makes ... and as nobody has an infinite amount of memory, deep recursion could cause Guile to run out of memory. Running out of stack memory is called stack overflow....
🌐
Reddit
reddit.com › r/learnprogramming › question about stack overflow in c++
r/learnprogramming on Reddit: Question about Stack Overflow in C++
April 21, 2023 -
#include <iostream>
using namespace std;

void main_menu();
void sub_menu();

void main_menu() {
    int choice;
    cout << "Main Menu" << endl;
    cout << "1. Sub Menu" << endl;
    cout << "2. Exit" << endl;
    cout << "Enter your choice: ";
    cin >> choice;
    switch (choice) {
        case 1:
            sub_menu();
            break;
        case 2:
            exit(0);
        default:
            cout << "Invalid choice. Please try again." << endl;
            main_menu();
    }
}

void sub_menu() {
    int choice;
    cout << "Sub Menu" << endl;
    cout << "1. Go Back" << endl;
    cout << "Enter your choice: ";
    cin >> choice;
    switch (choice) {
        case 1:
            main_menu();
            break;
        default:
            cout << "Invalid choice. Please try again." << endl;
            sub_menu();
    }
}

int main() {
    main_menu();
    return 0;
}

This code shows the user a menu with different options to choose from. Each sub-menu has a "Go back" option. Now let's say a user keeps going back and forth between selecting the option in the menu and going back to the menu by selecting the "Go back" option.

To my knowledge, every time a function is called, its stack frame is created on the stack, and the stack frame will pop off when that function returns something back to the previous function that called it. In the example above, will the stack overflow if the user keeps going back and forth (keep calling the function)? What will happen to the menu and sub-menu functions on the stack if it is a void function, and it doesn't return any value back?

Top answer
1 of 2
3
So what you've effectively created is tail recursion. A compiler can optimize tail recursion to avoid stack overflows. So it's really up to the compiler you are using and the compiler settings if it'll convert this to the proper code. Remember that the code you write isn't what gets run, the compiler is at liberty to change the code to something equivalent and some versions of valid conversions will have stack overflows and others won't.
2 of 2
2
What will happen to the menu and sub-menu functions on the stack if it is a void function, and it doesn't return any value back? other people aready posted a solution but to answer your question you can crash the program if you keep calling the same function until you stack-overflow. programs are allocated in memory into chunks for example if you view your program you might see: [low address] .text section (code, marked as read-execute only) .data (global variables, marked as read-write) .data? (malloc stuff goes here, grows down) stack (stack frames of functions goes here, grows up) [high address] there migth be some holes of not allocated memory and some other mem for libraries but the idea is that if you keep increasing the stack eventually it will collide with "data?" section and crash the program. there migth be a page-guard allocated memory that crash the program if you try to access it so they don't collide but to keep it simple "bad things happen" about void funtion: a stack frame is created for pretty much every function (unless you mark them "naked" in C). the stack frame is used to access local variables of the function since they are on the stack too. even if you mark the function as "naked" the return address of the function is allocated on the stack. return value of a function is stored by convention in the EAX register of the cpu so in a void function it's value might be undefined and it's never checked by the caller (but eax will hold a value for sure, it can't be "empty")