Subroutines:
In computer science, a subroutine (also called procedure, function, routine, method, or subprogram) is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code.
Most modern implementations use a call stack, a special case of the stack data structure, to implement subroutine calls and returns. Each procedure call creates a new entry, called a stack frame, at the top of the stack; when the procedure returns, its stack frame is deleted from the stack, and its space may be used for other procedure calls. Each stack frame contains the private data of the corresponding call, which typically includes the procedure's parameters and internal variables, and the return address.
The call stack is usually implemented as a contiguous area of memory. It is an arbitrary design choice whether the bottom of the stack is the lowest or highest address within this area, so that the stack may grow forwards or backwards in memory; however, many architectures chose the latter.[citation needed]
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. This is usually the result of creating local array variables that are far too large. For this reason arrays larger than a few kilobytes should be allocated dynamically instead of as a local variable.[5]
An example of a very large stack variable in C:
int foo() { double x[1000000]; }
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, a stack overflow will occur.[further explanation needed]
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.[6][7]
Possible solution:
http://stackoverflow.com/questions/3718528/how-to-allocate-more-memory-to-your-program-gcc
under GCC:
C dynamic memory allocation
http://en.wikipedia.org/wiki/C_dynamic_memory_allocation
Type Safety, Casting?
Possible solution:
http://stackoverflow.com/questions/3718528/how-to-allocate-more-memory-to-your-program-gcc
under GCC:
C dynamic memory allocation
http://en.wikipedia.org/wiki/C_dynamic_memory_allocation
/* Allocate space for an array with ten elements of type int. */ int *ptr = (int *) malloc(10 * sizeof (int)); if (ptr == NULL) { /* Memory could not be allocated, the program should handle the error here as appropriate. */ } else { /* Allocation succeeded. Do something. */ free(ptr); /* We are done with the int objects, and free the associated pointer. */ ptr = NULL; /* The pointed-to-data must not be used again, unless re-assigned by using malloc again. */ }
Type Safety, Casting?
No comments:
Post a Comment