Linux C language: pointers and memory

1. The computer data representation

Unit: bytes (byte)
1byte = 'bit. 8

Calculated using binary
display to decimal
programming hexadecimal

2. Memory Management

Q: 32-bit system maximum use 4G memory?
A: 32-bit system address bus is 32 bits, i.e. a 32-bit address space;
32 means: a number of memory allocated to only 32 bits;
32 address bus 2 is 32 states;
2 32 = 2 10 * 2 10 * 2 10 *. 4 = 1K * 1K * 1K * 1K. 4 = 1M * * =. 4 4G

64-bit system can manage 2 64 (* 4G 4G) memory
addresses from 0x0000 0000 0000 0000 FFFF FFFF 0xFFFF FFFF to
any address can store a byte is eight binary

Here Insert Picture Description

Wherein the code in the code segments;
global variables, constants, static variables in the data segment;
local variable on the stack;

Essence 3 variables and pointers

What is the nature of the variables?
It is just a code name of the variable, representing a memory address space
variable nature of memory is
C language can not be the direct operation of a memory

Pointer variable is stored is the memory address of
the essence pointer is the address

4 operating system for memory management

32bit Operating Systems: pointer 4-byte
64bit operating system: Pointer 8 bytes
long address, no matter what type of points, accounting for the number of bytes is the same, pointing to a function address is the same.

5 function stack and the data memory section

The maximum allocated first stack address, assigned downwardly from the stack;

Stack features: advanced after:
Here Insert Picture Description
As shown, main function first calls and then quadrate function calls, and finally into the rect function, and finally into the uppermost rect function.

Different different functions with the same name inside the static variable address, independent of each other;
added:
1, static storage variables when the variables are generally defined on sub-set storage unit and remains unchanged until the end of the process. (Static)
2, and the dynamic variables are stored during program execution, it is used when allocating the storage unit, immediately released after use. (Common int / char, etc., so that the stack)

6. function pointer with a pointer to the data access

int (*pquadrate)(int a)=&quadrate;

The pquadrate declare a function pointer is (int a parameter that returns an int value) function. Parameter is int a, int type of the return value is.

int s = (*pquadrate)(a);

gdb debugging time to run to the line when you can also press s entry into the function description function pointer can also call a function that is often used as a callback function to use.
* pquadrate go pointer in the specific value (i.e. function calls), behind that a (previously declared int a = 3) is used when calling the function parameters;

p * pa
if pa is a pointer to the stack memory, the heap memory, data memory, a specific value of the address is output,
if pa is the code memory pointer, for the calling function;

Published 30 original articles · won praise 36 · views 687

Guess you like

Origin blog.csdn.net/qq_42745340/article/details/103871560