Introduction to C Memory Management and C++ Object-Oriented Programming
- C is used when 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
- 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 what the operating system allows.
- Memory layout: process context
- Accessing memory in C: pointers
- Pointers are memory addresses.
- Every variable has a memory address.
- It‘s all about tracking which addresses are 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 are assigned | Upon entering function Upon function return Statically | With malloc With free Dynamically |
- Dynamic allocation and deallocation
- Allocation
- int *p = malloc(sizeof(int));
- Deallocation
- free(p);
- Memory errors
- How you 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 have to allocate memory on heap.
- When passing a parameter whose value you want to allow 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. are defined more than once!
- Use #ifndef directive to define things only if they have 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 that function is included directly in the executable, allowing fuction calls to directly address code.
- Dynamic. Function calls call a Procedure Linkage Table, which contains the proper addresses of the mapped memory.
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 |
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。