2022. 7. 8. 22:14ㆍCoding - C
Memory Structure
Program should be loaded to the memory, which is consisted of Code Area, Data Area, Stack Area, Heap Area
The program is ran from the lowest memory to the highest memory.
Code Area: Where the code itself is stored in the memory. The CPU takes one command by one and runs it.
Data Area: Where the Global and Static variables are stored in the memory. The area is distributed as the program starts, and deleted when program ends.
Stack Area: Where the local variables, temporary variables(due to functions), return address for functions, and local variables in functions are stored. Same with the data area, the area is distributed as the program starts, and deleted when program ends.
The called information of functions is called stack frame, and due to it, when all functions have been called, it can return to its original state.
Stack area stores information using push action, and taken out using pop action. This stacks are ran in the form of LIFO(Last-In-First-Out), so the last stored data will be ran first.
For this code, it will be in this sequence:
1) As program starts, the main() function will be called so the stack frame of main() will be saved in the stack area.
2) Then func1() will be called so the stack frame (temporary variable, return value, local variable) will be stacked on top of stack frame of main().
3) Same process with the last sequences occur for func2().
4) As all functions are called and returned, only the stack frame of func2() will be deleted from stack area.
5) Then func1()'s stack frame will be removed.
6) If all programs end for main(), the main() function's stack frame will finally be deleted.
For recursive calls, if the function itself is endlessly called, adding stack frame each time it is called, if the function is called again when there is no more stack area, it will cause a stack overflow. So it will cause serious errors.
Heap Area: Area which the user can modify the memory's size. Same with stack area, it is ran from low memory to high memory.
Dynamic Allocation: Stack area and Data area's memory size is already determined by compile time. However, in Heap area, the user decides during the code's run time. So getting memory allocated is called dynamic allocation.
Malloc(): To do dynamic allocation, we use the malloc() function. The basic format is datatype *malloc(size_t* size). We get the address of the first byte of the memory size. If there isn't area to give, then it gives the NULL pointer. We need to use pointer since it accesses the address.
*size_t is unsigned interger type of the result of sizeof(). Ex) sizeof(int), sizeof(char)
free(): free() function allows the allocated area to be returned. If we don't return the allocated area, we might be in lack of memory space. This situation is called memory leak.
Calloc(): This is same function as malloc(), but just different way of initializing it. The initial format is datatype calloc(size_t(number of memory blocks), size_t(number of bytes in each block)).
This example returns the same value of 4, but just different it ways of initializing.
Realloc(): This is just reallocating the allocated data. The initial format is datatype *realloc( name_of_the_pointer_you_want_to_change_allocated_area *ptr, size_t size_of_reallocation).
'Coding - C' 카테고리의 다른 글
Structure Type (0) | 2022.07.16 |
---|---|
Character and Strings (0) | 2022.07.09 |
Pointers and Arrays (0) | 2022.07.02 |
Pointers (0) | 2022.07.02 |
Practice Problem Part 2 & Practice Problem #2 (0) | 2022.06.25 |