程序代做CS代考 CSCI 4061 – cscodehelp代写

CSCI 4061
Discussion week 7
5

Overview
¡ñ Dynamic Memory Management
¡ñ Memory Bugs ¡ñ lint
¡ñ Exercise/Quiz

Dynamic Memory Management
¡ñ Memory allocated statically is placed on the stack
¡ð Without call to malloc
¡ð Does not persist after returning from function calls
¡ñ Memory allocated dynamically is placed on the heap
¡ð Allocated using malloc
¡ð Persists after returns from function calls
¡ð If not freed, can lead to memory leaks

free()
¡ñ Deallocates memory on the heap so it may be used again int foo() {
char *str = (char *) malloc(80);
strcpy(str, ¡°Hello World!¡±);
printf(¡°str = %s, address = %u
¡±, str, str);
free(str);
return 0;
}

Memory Bugs
¡ñ Memory Leak
¡ð Dynamically allocated memory is not reclaimed using free()
¡ð If the function in which this occurs is called many times, the system may run out of
available memory for new allocation
¡ñ Stack Smashing
¡ð A function performs a write operation outside the bounds of its own stack space
¡ð This can corrupt the stack of another function call
¡ð May allow for arbitrary code execution (security problem)

Viewing Memory Usage with ¡®top¡¯
¡ñ The ¡®top¡¯ command will show processor and memory usage for currently running applications
¡ñ You can use this to determine if a running program has a memory leak
¡ð If the memory usage plateaus, then there is no leak
¡ð If the memory usage continues to grow during execution, there is likely a memory leak

Buffer Overflow for Stack Smashing
void foo(char* input) {
char buffer[10];
strcpy(buffer, input);
}
¡ñ The bounds of input are not checked, so the call to strcpy() may overrun the space allocated for buffer and write into other functions¡¯ stack space

lint
¡ñ Static analysis tool for debugging programs
¡ñ Attempts to find issues like the memory bugs described above
¡ñ Looks directly at source code without executing

Today¡¯s Task
¡ñ
¡ñ
¡ð
¡ð
¡ö
¡ö
¡ö ¡ð

Leave a Reply

Your email address will not be published. Required fields are marked *