Pointers - Part 1 Flashcards
1 Pass By Value
Func(x, y)
func recieves copies of parameters
2 pass by reference
func(&x, &y)
func recieves address to parameter, can change original parameter value
3 Local Variable:
only accessible within a specific part of a program
4 Automatic Variable
allocates memory upon entering the variable’s block, and automatically un-allocates on exit. only activate/deactivates the memory.
main(){auto int a;} vs func(){int a;}
5 Static Variable
Preserves value even if out of scope. doesn’t need to reinitialize in case of new scope.
6 String in C
character sequence terminated with null character ‘\0’. stored as array of characters
char str[]=”bob”;
char str[4] = {‘b’, ‘o’, ‘b’, ‘\0’};
7 Pointer:
int *pointer = x
pointer == &x //address of x
*pointer == x //value of x
8 Dynamic Memory Allocation
Changing data structure size during runtime.
malloc(), calloc(), free(), realloc()
https://www.geeksforgeeks.org/dynamic-memory-allocation-in-c-using-malloc-calloc-free-and-realloc/
does not include local variables; they are assigned automatically by the program within its scope,
dynamic memory allocation is the programmer’s responsibility, including use of free() to avoid loss of data
9 Function Arguments
passed parameters to functions, either by value or by referece.
arguments are copied to the program stack at run time, then read by the function
10 Dangling Pointer
situation when pointer points to a variable that is out of scope, or the variable’s memory gets deallocated.
use static variables or assign NULL to the pointer to prevent errors
11 NULL
C NULL: special reserved pointer value that does not refer to any valid data object
12 Interface
collection of functions that describe the bahaviour of an object. program won’t know what to do with them yet, but you can define them later.
struct DrawableInterafce
{
void (* draw)( Object );
void (* rotate)( Object, double );
int (* get_heigth)( Object );
int (* get_width)( Object );
};
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/interface
13 Stack Variable
char *buff[500];
allocating data in stack is easy and fast, but limited, and deleted when you leave the scope. good for small local values.
local variables, arrays
14 Data Variable
containers for storing data values.
static, global variables
15 Heap “Variable”
char *buff = (char *)malloc(500);
memory used for global variables. supports dynamic memory allocation, but not tightly or automatically managed by the cpu. manually handled by the programmer, slower, and can resize variables (stack variables cannot be resized).