- C is used if at least one of the following matters:
- Speed
- Memory
- Low-level features (moving the stack pointer, etc).
-
Level of abstraction |
Languages |
Directly Manipulate memory |
Assembly (X86,mips) |
Access to Memory |
C,c++ |
Memory Managed |
Java,c#,scheme/lisp,ml |
- It ' s A memory world
Figure:processors read from Memory,do things, and write to memory.
- The heap is a chunk of memory for the C program to use.
- Can think of it as a giant array.
- Access hep Using special pointer syntax.
- The whole program, HS access to the heap depending, on the operating system allows.
- Memory layout:process Context
- accessing Memory in C:pointers
- pointers is memory addresses.
- Every variable has a memory address.
- It's all about tracking which addresses is still in use.
- Revisiting C Memory:stack vs. heap
- The C compiler lays out memory corresponding to functions (Arguments,variables) on the stack.
- C allows the programmer to allocate additional memory on the heap.
|
Stack |
Heap |
Memory is allocated Memory is deallocated Addresses is assigned |
Upon entering function Upon function return Statically |
With malloc With free Dynamically |
- Dynamic Allocation and deallocation
- Allocation
int *p = malloc(sizeof(int));
- Deallocation
- Memory errors
- How can produce memory errors
- Program accesses memory it shouldn ' t (not yet allocated, not yet freed, past end of heap block, inaccessible parts of the Stack).
- Dangerous use of unitialized values.
- Memory leaks
- Bad frees.
- Manifestations of memory errors
- "Junk" values
- Segmentation Falult-program crashes.
- Tools for programming with memory
- GDB: the GNU Project Debugger
- Start your program, specifying things that might affect behavior.
- Make your program stop on breakpoints.
- Examine variables etc.at program points.
- Change things in your program while debugging.
- Valgrind: A Memory profiling tool
- A GPL System for debugging and profilling Linux programs.
- Tool Suite includes Memcheck, Cachegrind, Callgrind, massif, and Helgrind.
- Review:when to use pointers
- When you had to allocate the memory on heap.
- When passing a parameter whose value, the other function-to-change.
- Also for efficiency-to avoid copying data structures.
- A closer Look at the GCC compilation process
- Preprocessor
- Translation of # directives.
- Translates all macros (#DEFINE's) into inline C code.
- Takes #include files and inserts thm into the code.
- Get redefinition Error if structs etc. is defined more than once!
- Use #ifndef directive to define things only if they has not been defined.
#ifndef _HEADER_NAME
#define _HEADER_NAME
/* Header code here. */
#endif
- Parsing and translation
- Translates to assembly, performing optimizations.
- Assembler
- Translates assembly to machine instructions.
- Linking
- Static. For each function called by the program, the assembly to this function is included directly in the executable, allowing Fu Ction calls to directly address code.
- Dynamic. Function calls call a Procedure Linkage Table, which contains the proper addresses of the mapped memory.
Some Helpful Compiler Options
Enforcements and warnings |
-ansi -pedantic -wall -W |
tells compiler to enforce ANSI C standards. More pedantic ANSI with warnings. Shows all warnings. Show some warnings (return without a value, etc.). |
Compilation/output
|
-C -S -E -O [File] |
Compile and assemble,but do not link. Stop after compilation; Do not assemble. Stop after preprocessing; Do not compile. Put the output binary in [file]. |
Profiling |
-pg |
Compile with profiling information |
From for notes (Wiz)
Introduction to C Memory Management and C + + object-oriented programming