Tips on C Programming:
- Anomaly hander:
- input check with constraints - e.g. if it’s NULL or exceeds max_length, or does not exist…
- return value. Or always use a variable to store the return value and check if any error occurs, return the error code, or exit.
- Keep the whole program well-structured. No too-long functions.
- use
strcmp
rather than ==
to compare strings - the former compares the C-string
object content while the latter compares the char pointers
- use
memcpy
instead of strcpy
since they might be binary data
- Is it an error or a warning? — warnings are mostly okay, especially for the memory check.
Basic Concepts
Memory Leak happens when programmers allocate memory in a heap and forget to release it. The act of allocation here could be a malloc()
or other functions that implicitly allocate some memory blocks. The consequence is that available memory could be less and less and programs begin to perform worse or even make mistakes. (Fortunately, the OS keep track of all the memory allocated by a process and can free them as that process terminates, yet it’s still vital to free manually in programs.)
P1: Jumbo Shell (terminal)
P2: Measurement (function timing)
Test 2: Virtual/ Logical Memory
P3: Ants & Aardvarks (threading & synchronization)
Test3: Combination
P4: Jumbo Filesystem (synchronizing vars with real DISK fs)
Test4: Memory Management, FS
Test5: VM, FS, and Reliability