C Language Memory topics

Source: Internet
Author: User

Embedded Software Engineer Learning Route Zhu Youpeng Teacher's quotations: learning, such as walking the Night road, many people in the heart not panic.

1, Embedded Foundation Preparatory

2. Arm Bare Metal Complete

3. Advanced topics in C language

4. Uboot and System transplant

5. Linux application programming and network programming

6, the Linux Drive development actual combat

----------------------------------------------------

Memory this big topic


C: The C language compiler helps us manage direct memory addresses. The memory is accessed through the variable name provided by the compiler.

Large chunks of memory are requested to release memory through the API (malloc free).

Java/c#: Do not directly manipulate memory, operate memory through virtual machine, manage, free memory. Virtual machines reclaim memory at a cost.


C is like eating at home and washing your own dishes.

Java/c# and other high-level language like, eat outside, eat and go, but pay a certain price.


A variable in the C language actually corresponds to a unit of memory.


Unit of memory unit size

-----------------

Bit bit 1bit

Bytes Byte 8bit

Half Word General 16bit

Word General 32bit

------------------

The CPU only knows the memory address, and the address and space are the two aspects of the memory unit.


The memory address is in bytes.

------------------------------------------------------------------------------------------

Data type


C Language Basic data type char short int long float double

int integer (integer type, which is the same as the data bit width of the CPU itself)

32bit CPU = = Integer type is 32bit = = int 32bit


Data types are used to define variables, which need to be stored and operated in memory. So the data type must match the memory to get the best performance, otherwise it might not work or be inefficient.


It is best to use int to define variables in a 32-bit system because it is efficient. The reason lies in the 32-bit system. It is also 32bit with memory, such hardware configuration is inherently suitable for defining the 32bit int type variable, the best efficiency. can also define 8bit char or 16bit short, the actual access efficiency is not high.


In a 32bit environment, the BOOL type variable is defined (only 1 bits in actual order), and the bool is implemented with INT

The compiler helped to allocate an int to store the bool, which actually wasted 31bit of memory, but was highly efficient.


Question: Do you want to save memory for the actual programming or to run with more efficiency? Look at the details.

Writing programs now is critical to efficiency and user experience. (Memory becomes cheap, doesn't care)


Memory alignment

Defines an int 4 bytes

0 1 2 3 aligned access

1 2 3 4/2 3 4 5/3 4 5 6 non-aligned access

The alignment of memory access is not a logical problem, a hardware issue. 32-bit memory 0 1 2 3 logic is relevant, hardware appropriate, high efficiency.

The alignment access is very good with the hardware, the efficiency is very high;

Non-aligned access because the hardware itself does not match, the efficiency is not high

Because of compatibility issues, general hardware also provides non-aligned access, but is inefficient


----------------------------------------------------------------------------

C language encapsulation of memory addresses (using variable names to access memory, meaning of data types, meaning of function names)


Contiguous array of spaces

First Address

First element

First address of the first element

int a;//compiler requests an int type of memory lattice (length 4 bytes, address OK, symbol A is bound to this lattice)

A = 5;//5 thrown into a bound lattice of symbol a

A + = 4;//9


The essential meaning of this type of data in C is that it represents the length and parsing method of a memory lattice.


The data type determines the meaning of the length:

We have a memory address, originally this address only represents the length of 1 bytes, give him a type, let him have the length. This way the number that represents the memory address can start with a contiguous n-byte memory lattice from this number.


The data type determines the meaning of the parsing method: There is a memory address that gives the memory address a different type to specify the parsing method of the binary number in this memory cell lattice.


(int) 0x30000000 4 bytes Together storage is an int

(int *) 0; 0 Address resolution of data-int* type (0 save a pointer, point to int)

(float *) 0;

(short) 0;

(char) 0;//0 address save a char variable


function is the encapsulation of a piece of code, the essence of the function name is the first address of this section of code.

The essence of a function name is a memory address.

Indirect access to memory with pointers

Type is just a length stipulation and parsing method for the memory represented by the following number or symbol (memory address)

Arrays manage memory and variables, except that the notation is interpreted differently

int A; The compiler assigns a 4-byte length to a, and binds the first address and the symbol A.

The int b[10];//compiler assigns a 40-byte length to B, the first element of the first address and the B binding.

-------------------------------------------------------------------------------

Structure of memory management

Data structure is the study of how to organize (in memory) how to process

Array subscript access, compiler helps compute memory address

A[0] a[2] = a[0] + 2*4

Arrays-Advantages: Arrays are relatively simple, access is subscript, random access

Disadvantage: The element type must be the same, the size must be defined when given, once determined can not be changed.

Structure invention solves the first flaw in an array: all element types in an array must be the same


Object-oriented implementation of nested pointers in structures.


The C language is process-oriented, but the C language writes out that the Linux system is object-oriented.


Using object-oriented languages like C + + Java to achieve object-oriented simplicity, because the language itself helps us do a lot of things, but using C to achieve object-oriented is cumbersome.

struct A

{

int age;

void (*func) (void);//function pointers, functions that point to void func (void)

};

You can implement object-oriented by using such a structure. This structure, which contains the function pointers, is similar to the object-oriented class. A variable in a struct is similar to a member variable of class. A function pointer is similar to a member method of class.

-------------------------------------------------------------------------------------------

Stack is a data structure, C language uses the stack to save local variables, management memory)

Stack management memory features (small memory, automation)

Local variables in the C language are implemented using stacks.

When a local variable is defined, the compiler allocates a space in the stack, the movement of the stack pointer, and the memory allocation automatically (the stack completes itself)

function exits, local variables perish, out of the stack.

C language, define local variables, if uninitialized, value random, stack memory reuse (dirty, last run out of 0)


Display initialization

int a = 15;//local variable definition, initialize

The C language compiler automatically turns this line into

int A; Defined

A = 15;//Assignment


Stack constraints (the size of the predetermined stack is not flexible, fear of overflow)

Stacks are of a size. Stack memory size is not well set. Too small afraid of overflow, too big afraid of wasting memory.

C language Definition Local variables can not be too much, too large, recursive problem to recursive convergence point.

-------------------------------------------------------------------------------------------

Heap Stack (stack)

Heap memory management (ready to apply, release, size block arbitrarily)

Heap memory, providing APIs (malloc and free) to consumers (user processes)


Requires large memory capacity and needs to be reused and released (data structure implementation-linked list)

Heap Management Memory Features

1 Unlimited capacity

2 application, Release manual malloc free, no release-memory loss, memory leak

The memory in the heap manager still belongs to this process, but the process itself again this memory is not used, and then will go to apply the memory that is called eating memory


The most serious program bug in C + + memory leak

java/c# memory management, only need to use, do not need to release

Man 3 Manual


void free (void *ptr);

Heap Memory Request

ReAlloc, Calloc, malloc


void *malloc (size_t size);

void *calloc (size_t nmemb,size_t size);//How many units

void *realloc (void *ptr,size_t size);//change the size of the original application space


Array definition size cannot be changed

Fake in Java-change array size

1. Re-create the size of a new array first

2. Copy the elements of the original array into the new array

3. Release the original array

4. Return the new array to the user


Heap memory request to size, once the request is complete size unchanged, if you want to change through realloc


Request 10 int elements of memory

malloc (+) malloc (10*sizeof (int.));

Calloc (10,4) calloc (10,sizeof (int));

------------------------------------------------------------------------------------------

This article from the "8342724" blog, reproduced please contact the author!

C Language Memory topics

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.