Last article looked at the development of c&&c++ language standards, this article then analyzes the embedded programming in the application of C + + language; in fact, based on the ARM processor, for C + + support is in place (using the gcc/g++ compiler tool), compared with the process-oriented C language programming, Object-oriented C + + implementation is more abstract; The articles in this series are all centered around the Ti da Vinci Processor:
A, C + + overview
1. Why do I choose c + + language in embedded development?
(1) The characteristics of process-oriented programming
C language Features: C language is gradually perfected in the process of practice
• No deliberate design process
• There are many "grey areas" when used
......
• Excessive residue characteristics of low-level language
• Direct use of pointers for memory operations
......
Process-oriented programming features:
Process programming Oriented: Data structure + algorithm
• Mainly solve the problem of scientific computing, user needs simple fixed
• Features: Analyze the steps needed to solve the problem
Using functions to implement each step
Call the function to solve the problem in turn
• Problem: Poor reusability of software
Poor software maintainability
Built software does not meet users ' needs
(2) The characteristics of object-oriented programming
Object-oriented Programming features:
Object-oriented Programming: Building software models from the real world
• Direct mapping of things in the real world to programs that directly meet user needs
• Features: Direct analysis of the various entities involved in user requirements
Describe entities in the real world in code
Associating entities in code to work around problems
• Advantages: Built software to adapt to the changing needs of users
Take advantage of process-oriented approaches to avoid their disadvantages
C + + language features: Efficient object-oriented language, and can be compatible with existing code
Second, from C to C + + upgrade
1. Declaration definition
C++:c++ more emphasis on language practicality, all variables can be redefined when needed
For example: for (int i = 0; i < 2; i++)
C Language: Variables must be defined at the start of the scope
2.register Upgrade of Keywords
Often accessed variables we can use the register as a register variable, request the compiler as far as possible in the CPU in the internal register, saving the CPU time to crawl data from memory, thereby improving the operational efficiency.
C Language: Register can only modify local variables, can not modify global variables and functions;
The register variable cannot be obtained by taking the address of the Register modifier;
The register-decorated variable must be a data type that the CPU can accept.
C + +: Still support the Register keyword in C + +
C + + compiler has its own optimization methods, do not use the register can also do optimization
Address of the register variable in C + + compiler Discovery program needs to register
Register is not valid for variable declaration when the address of the variable is
3.const keywords
c++:c++ compiler's handling of const constants
• Put constants in the symbol table when you encounter a constant declaration
• Use constants in the compilation process to replace the values in the symbol table
• If an extern or & operator is found to be used for const during compilation, the corresponding constant
Allocating storage space
Note: the C + + compiler may allocate space for a const constant, but will not use its storage space
The value.
C Language: C language in the const modifier variable, the value of the space is variable, but not by variable name to modify the corresponding value of the space.
4. Memory allocation and release
(1) Dynamic memory allocation in C + +
· Dynamic memory request via new keyword in C + +
· C + + Dynamic memory requests are based on the type
Delete keyword for memory release
Variable Shen Qing:
Type *pointer = new type;
......
Delete pointer;
Array Shen Qing:
Type *pointer = new type[];
......
delete[] pointer;
(2) The difference between new and malloc
The New keyword is part of C + +, malloc is a function provided by the C library
new memory allocations in specific types, malloc can only be memory in bytes
Distribution
new can be initialized when applying for a single type variable, malloc does not have memory initialization special
Of
This section is used in Sys/bois in TI DaVinci Processors, reference sys BIOS (ti-rtos kernel) v6.41 user manual. pdf
5. Reference vs pointer
(1) Reference
• References are aliases for a variable
• Define a generic format for a reference: