

Stack overflow happens when all the memory in the stack has been allocated, and further allocations begin overflowing into other sections of memory. If a program tries to put too much information on the stack, stack overflow will occur. Note that there is generally a limit on the size of the stack. Then when a function finishes running, all the variables associated with that function on the stack are deleted, and the memory they use is freed up. Every time a function declares a new variable it is “pushed” onto the stack. It’s a LIFO, “Last-In,-First-Out”, structure. The stack is used to store variables used on the inside of a function (including the main() function). For example, the same variable created inside a function using the static clause would allow it to be stored in static memory.

The variable j s value is set to the address returned by malloc. Its also allocating space on the stack for a pointer ( j ). So in this code: int j malloc (sizeof (int)) This is allocating space on the heap for an integer. If you want to put the bits on the heap, then call a runtime library heap allocation function and store the bits into that location. If you want the bits to be a particular thing, then store the bits you want to that location. It is also possible to force a variable to be static using the static clause. The dynamic memory is allocated on the heap, and the pointer itself is allocated on the stack. As far as LLVM is concerned, a struct is just a bunch of bits, and all memory locations are more-or-less the same. Inside a function the variable is allocated on the stack. Static memory persists throughout the entire life of the program, and is usually used to store things like global variables, or variables created with the static clause. Global variables are static, and there is only one copy for the entire program.
