![]() If you don’t have a terminating case for the recursive example above, the stack will continue to grow in size adding additional stack frames on-top of each other, moving the stack pointer upward on each call, against the heap, which will be explained in the next section. When we hit the terminating condition, we execute our logic, then start to unwind the stack or pop off stack frames in the last-in-first-out manner until we reach out return(0) logic in the int main(void) function in our first stack frame. A stack pointer is a small register that stores the address of the last program request in a frame. Each time the _puts_recursion function is called, a new stack frame is put on the stack until we hit the terminating condition which is if (*s = '\0').Įvery time a new stack frame is created, the stack pointer moves with it until it reaches the terminating condition. The argument s moves the memory address of the *s one byte because the size of a char is 1 byte on our machine, and now _puts_recursion is calling the function as _puts_recrusion('o'). Because 'H' does not equal '\0', we will continue with our function calls and execute the _putchar('H') function and enter into the same function _puts_recursion( s). We are now in our second stack frame in our program and have entered in the _puts_recursion(char *s) function where *s is equal to 'H' and is only accessible in that stack frame. When the compiler runs into the _puts_recursion() function, it calls that function and creates another stack frame on top of the previous stack frame where int main(void) lives. When we compile our code using gcc _putchar.c 0-puts_recursion.c 0-main.c, the compiler enters our program through int main(void) and creates a frame with the function int main(void) and _puts_recursion("Holberton") living on that frame as illustrated on the image above. Yes, I have a whiteboard on the back of my door at my house. The main function and all the local variables are stored in an initial frame. A frame, also known as an activation record is the collection of all data on the stack associated with one subprogram call. When you compile a program, the compiler enters through the main function and a stack frame is created on the stack. The stack is a segment of memory where data like your local variables and function calls get added and/or removed in a last-in-first-out (LIFO) manner. The text segment is often read-only and prevents a program from accidentally modifying its instructions. The text segment, also known as the code segment, contains the machine instructions which make up your program. Having unused variables in our programs gets flagged and is not a best practice. Most of the time I’m not concerned about the uninitialized data segment because when we compile our programs with gcc, we use the flags, -Wall -Wextra -pedantic -Werror and we use an internal stylistic checker called betty which treats warning as errors when uninitialized variables are present. The uninitialized data segment consists of all global and static variables that are initialized to zero or do not have explicit initialization in source code. ![]() ![]() The initialized data segment consists of all the global and static variables that are initialized when a file gets compiled. The amount of memory that get’s assigned to an application depends on the computer’s architecture and will vary across most devices, but the variable that remains constant is the five parts of an application’s memory which are the heap, stack, initialized data segment, uninitialized data segment, and the text segment. The memory that is assigned to a program or application in a computer can be divided into five parts. When we write applications, files, or any logic that is typed in an editor and executed on the computer, the computer has to allocate memory for the program to run. How does understanding the two make you a better software engineer? What are the five segments of memory? What better way to build a solid foundation of how memory gets allocated then to write a technical post on the stack versus the heap? This article explains in depth: I’m four months into the self study and I’ve solved multiple problems using the malloc, realloc, calloc and free functions in the C programming language.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |