Memory, stack, heap

The general application memory space has the following areas:

Stack: Automatically allocated by the operating system to hold the values of function parameters, local variables, etc., used to maintain the context of function calls

Heap: Usually allocated and released by the programmer, or otherwise reclaimed by the operating system at the end of the program to hold the area of memory dynamically allocated by the application

Executable image: The memory image that stores the executable file, loaded by the loader to read or map the memory of the executable file

Tenure: A tenure is not a single memory area, but a general name for the memory area that is protected from access. For example, in C language, invalid Pointers are assigned to 0 (NULL). Therefore, the 0 address cannot access data effectively under normal circumstances

The stack

A Stack holds the maintenance information needed for a function call. It is often called a Stack Frame or an active Record, and generally contains the following aspects:

The return address and arguments of the function

Temporary variables: Includes non-static local variables of a function and other temporary variables automatically generated by the compiler

Save context: Includes registers that need to remain unchanged before and after a function call

The heap

Heap allocation algorithm:

Free List

Bitmap

Object pooling

“Segment fault” or “Invalid operation, this memory address cannot be read/write”

Typical error caused by invalid pointer dereference. This error occurs when a pointer points to a memory address that is not allowed to be read or written, but the program tries to read or write to that address using the pointer.

Common reasons:

Initialize the pointer to NULL, and then use the pointer without giving it a reasonable value

Instead of initializing the pointer in the stack, the value of the pointer will generally be a random number, and then you start using the pointer directly

Compile the link

Each platform file format

Compile link procedure

(1) Precompile (the precompiler processes precompile instructions such as #include and #define, and generates.i or.ii files)

(2) Compilation (compiler conducts lexical analysis, grammar analysis, semantic analysis, intermediate code generation, object code generation, optimization, and.s file generation)

(3) assembly (the assembler translates the assembly code into machine code and generates.o files)

(4) link (connector for address and space allocation, symbol resolution, relocation, generate.out file)

The current version of GCC combines precompilation and compilation in one step, precompiling the compiler cc1, assembler AS, and connector LD

MSVC compilation environment, compiler CL, connector Link, executable file viewer Dumpbin

The target file

The files generated by the compiler after compiling source code are called object files. The object file is structurally a compiled executable file format that has not yet gone through the linking process, where some symbols or addresses have not yet been adjusted.

Executables (.exe for Windows and ELF for Linux), dynamic link libraries (.dll for Windows and.so for Linux), static link libraries (.lib for Windows and Linux . A) Are stored in executable file format (Pe-COFF for Windows, ELF for Linux).

Target file format

Windows PE (Portable Executable), or PE-COff,.obj format

Linux ELF (Executable Linkable Format),.O Format

Intel/Microsoft OMF (Object Module Format)

Unix format of A. out

COM format for MS-DOS

PE and ELF are variants of the Common File Format (COFF)

Target file storage structure

Link interface ———— symbol

In linking, object files are actually references to addresses between object files, that is, the addresses of functions and variables. We refer to functions and variables collectively as symbols, and the function or variable Name is the Symbol Name.

The following Symbol Table:

This is the end of today’s sharing, you must learn C++ yo ~

For those of you who are ready to learn C/C++ programming, if you want to improve your core programming skills, you might as well start now!

Wechat official account: C language programming Learning base

Organize and share (years of learning source code, project actual combat video, project notes, basic introduction tutorial)