Code reading and Practice notes

Source: Internet
Author: User
Advanced C Data Types C Pointer

In C programs, pointers are typically used to: construct chained data structures

Referencing dynamically-allocated data structures

#define NEW (type) (type*) calloc (sizeof (type), 1)
[...]
node = new (struct codeword_entry)

Implement reference invocation (call by reference)

Accessing and iterating over data elements

Passing Array parameters
In C and C + + programs, pointers are used for passing arrays to functions and returning as results. In C code, when the array name is used as a function parameter, the address of the first element of the array is actually passed to the function. Thus, any modifications made to the data in the array when the function executes will affect the elements in the arrays.
Similar functions return only pointers that return arrays. So when the function produces the result in the array and returns the corresponding pointer, make sure that the array is not a local variable allocated on the stack of the function. If the array is a local variable, remember to make sure that the array is not a local variable allocated on the function stack. the way to avoid this problem is to declare such arrays as static

Reference function

Efficiency considerations as aliases for other means: referencing statically initialized data to implement variable reference semantics in global context

Represents a String

Direct access to system memory structure

Structure is also a pointer to a struct.
The two advantages of the method of taking pointers as parameters: they can work on newer C, or on older C; only one address message is passed.
Disadvantage: lack of protection of data.
One of the advantages of passing a struct as a parameter is a copy of the original data that the function processes, which is more secure than the raw data being processed directly. A collection of data elements that are generally used as a whole to return multiple data elements from a function constructs a chained data structure map the way an organization implements an abstract data type on hardware devices, network connections, and storage media

Programming in an object-oriented manner
you can aggregate data elements and function pointers into structures, simulate the fields and methods of a class, and create entities similar to classes.

struct domain{
int dom_family;
char *dom_name;
void (*dom_init) (void);
[...]
Int (*dom_rtattach) (void **,int);
int dom_rtoffset;
int dom_maxrtkey;
};

After init the struct,used like class for
(dom = Domains;dom;dom = dom, dom_next) {
if (DOM-dom_family  = = I && dom-Dom_rtattach) {
    dom-dom_rtattach (void * *) &NEP->ne_rtable[i],
            dom- Dom_rtoffset);
    break;
}
}

In fact, because different objects of the same class share their methods, the program often stores only pointers to other structures in the object structure. This structure gives pointers to the actual methods, thus achieving the purpose of sharing method pointers among different objects. -"virtual method" and "polymorphic Programming" Union in object-oriented language

Aggregates items that share the same block of storage area. At some point, only one of these items that share the zone can be accessed.
Main purpose: Efficient use of storage space

Implementing polymorphic
Polymorphic data

Enum msg_type{Call
= 0;
REPLY = 1;
};
C Data Structure dealing with large projects design and implementation technologyIn large projects, the various components of the software life cycle are often divided into smaller tasks that are often Software code baseAn integral part of it. Large-scale software development efforts must use a suitable architecture to construct the system to be created. This architecture generally specify the structure of the system, how the control is handled, and how the various components of the system are decomposed into modules. Large systems may also draw inspiration from frameworks (framework), design patterns, and domain-specific architectures to reuse their architectural ideas. The complexity of large systems can often be controlled using object-oriented design and implementation techniques. The objects are generally organized into different levels. After that, you can use inherit could be free take out public behavior, dynamic scheduling technology enables a single code body to handle many different objectsIn larger granularity, the code for large systems is often broken down into object module libraries, reusable components, and even independent process building tools

Configuration

Configuration-controlled software system that allows developers to compile, maintain, and develop a single official version of the source code
Depending on the principle of dependency isolation , configuration information is generally divided into several files. Configuration information that affects how the system compiles (for example, the installation directory, the tool name and path, whether there is an executable file that includes the debug regal, the optimization level of the application, the library used and the target file), is often expressed in the form of a variable definition in the project's production file.
such as revision control

We can think of the system's source code as an extension in space and time in two directions. Code, organized into files and directories, occupies space, while the same code evolves over time.
Revision Control-Version control testing

Well-designed projects, all or part of the test system in advance to provide appropriate measures. These measures may be part of a deliberate plan to validate the operation of the system, or the remnants of informal testing activities implemented by the system's developers in the process of implementing the system.
The simplest test code is a statement that generates a log (logging) or debug (debugging) output. The location of the trace statement is generally an important part of the algorithm's operation. Coding Specifications and conventions

[...]
A software system is not a knowledge code. As a result, many coding guidelines extend to other areas of the development process, including the organization of documentation, build, and release processes.
At the very least, many guidelines prescribe standard documents and the format in which they are written. Document

Reading an hour of code is just about reading a minute. Document Document Type

List some of the most representative:
System Specification Documentation: detailed description of the system's objectives, system functional requirements, management and technical constraints, and cost and schedule elements. The system's specification documents enable you to understand the actual operating environment of the code you are reading.
Software Requirements Specification: provides a high-level description of the user's needs and overall system architecture, and details the functional and non-functional requirements of the system, such as data processing, external interfaces, logical patterns of the database, and various constraints on the design. is the benchmark for reading and evaluating code
Design Specifications: General description of the system's architecture, data and code structure, and the interface between the different modules. Object-oriented design outlines the basic types of systems and how they are exposed. Detailed design specifications typically also include specific information about each module (or class), such as the specific processing tasks it performs, the interfaces provided, and the relationships with other modules or classes. It also describes the data structure used, the applicable database schema, and so on. Roadmap for cognitive code structure, guidelines for reading specific code
System Test Specification: includes test plan, specific test procedure, and actual test results. Each test process details the module it is targeting and the data used by the test case.
many user documentation: feature descriptions, installation instructions, introductory guides, reference manuals, and administrator's manuals. And it's often the only document we're likely to get ... When exposed to an unknown system, functional descriptions and user guides provide important background information to better understand the context in which the reading code is located. From the user reference manual, we can quickly get background knowledge of the application's appearance and logic. You can learn more about the interface, file format, and error messages for your code from the Administrator's Manual. Problems with Documents

The documentation may provide inappropriate information, misleading our understanding of the source code. System Architecture

It is not easy to understand the architecture of the system from the source code of the system. However, after identifying important architectural elements, it is easier to navigate the entire system, understand the structure and properties of the system, and plan additions, modifications, and refactoring activities. This is because, once the system's architectural row features are extracted, we immediately share a set of semantically rich vocabularies with the system's creators. In addition, understanding of the system architecture can help us understand the types of interactions, communication patterns, and code structures. structure of the system

Common, important structures can be categorized into a few disparate architectural types: Centralized repository (centralized repository), data flow (Data-flow), Object-oriented (object-oriented), or hierarchical (layered) architectures.

Centralized repository:Web Apps
Object-oriented: built on objects that maintain their state and interact with each other. The architecture of a system is defined by the relationships between different classes or objects, and by how objects interact. UML to express the model of the system.

This level of care is the control flow and data flow in the system. When analyzing the system architecture, you can focus on: The source code is scheduled in the directory static or dynamic process calls Namespace,package class and interface inheritance relationships internal classes and nested procedures the navigation in heterogeneous data structures and object associations

slices: Consider a program slice as a program that can affect the value of a variable. (Dashplayer modification principle, my understanding) Control Model

Event-driven systems
In many systems, control decisions are done by responding to externally generated events. (embedded development, trigger of relational database)
There are many ways to implement this:

Event broadcast to a series of event listeners

for (int i = 0;i < List.length;i + +) {
((ContainerListener) list[i]). ContainerEvent (event);

Hardware interrupts

Built around the event loop, which continuously queries the event and performs the appropriate processing

while (Xtapppending (appctx))
xtappprocessevent (Appctx,xtimall);

Class-based event processing brings two distinct benefits to your application:
* Events can host additional information in the form of attributes, eliminating untyped, common event arguments
* Inheritance enables an event hierarchy that can drive event dispatch policies. I don't understand.

Below through the code rationale (anyway, I do not understand these two words)

Public abstract class Argoevent extends EventObject implements argoeventtypes{
    protected int _eventtype = 0;
    Public argoevent (int eventtype,object src) {
        super (SRC);
        _eventtype = EventType;
    }
}
The Argoevent class above extends the Java eventobject,argoevent and is then extended by the Argomoduleevent and Argonotationevent classes.

System Manager
Systems that require concurrent execution of multiple processes, often using System Manager control models. A single system component acts as a centralized manager and is responsible for initiating, stopping, and coordinating the execution of other system processes and tasks.

State change
element Encapsulation

Modules and namespaces
Object is a run-time entity
Paradigm (data structure or algorithm)
Abstract data types
libraries are organized collections of modules
Processes and Filters (filters are a special process)
Software Components (typical self-contained units in program composition) (JavaBeans) schema Reuse

Many systems follow the established architecture at design time. Frame
Because of the reusable architecture, the structure of proportion-custom applications is more complex because it satisfies a wide range of applications.

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.