Embedded development interview questions and answers

1. Similarities and differences between malloc/free and new/delete

Memory map:

Same point:

        Both malloc and new open up heap memory space, and free and delete both release heap memory space

difference:

(1) malloc and free are functions, new/delete are operators

(2) When malloc opens up space, it needs to manually calculate the size of the space, and when new opens up space, it automatically calculates the size of the space

(3) The return value type of the malloc function is void*, which requires mandatory type conversion when used. New is followed by the type of space, and no mandatory type conversion is required.

(4) When the malloc function opens up space, it does not initialize the space. When new opens up space, it can initialize and assign values ​​to the space.

(5) The malloc function can only be used for basic data types, not for custom types (except structures). new can be used not only for basic data types, but also for custom types.

2. The difference between sizeof and strlen 

(1) sizeof is an operator, strlen is a function

(2) sizeof can use a type function as a parameter, strlen asks you to calculate char*, it must end with "/0" as its calculation end sign

(3) sizeof is calculated during compilation, and it calculates the size of the memory; strlen is calculated during operation, indicating the length of the string, not the size of the memory

3. The difference between struct and union

(1) The members of the union share the same address space, so the byte size of the union variable is equal to the largest byte among the members

(2) Assigning values ​​to different members will affect the results of reading values ​​​​of other members

4. The difference between array name and pointer

(1) The pointer saves the address, and the array saves the data

(2) The name of the array represents the first address of the entire array, and also the first address of the first member of the array

(3) Pointers access data indirectly through addresses, and arrays access data through subscript offsets

(4) The pointer is the pointer address value, the stored byte size, and the calculation of the array is the entire space of the array, that is, the memory size occupied by all data 

 5. The method of emptying the array

(1) bzero function

(2) memset function

6. The difference between synchronous IO and asynchronous IO

(1) In synchronous IO, when an IO is executed, the application must wait until the IO is executed.

(2) In asynchronous IO, IO operations and applications can run simultaneously, improving system performance and improving IO traffic

(3) In synchronous IO, the thread starts an IO operation, and then immediately enters the waiting state, and cannot wake up until the IO operation is completed to continue execution. In asynchronous file IO, the thread sends an IO request to the kernel, and then continues to process other things. After the kernel completes the IO request, it will notify the thread IO to complete the operation

 7. Can local variables have the same name as global variables?

It can pass when compiling, and when actually outputting, according to the principle of proximity, the value of the local variable is output first. Its essence is because the life cycle is different from the scope. If you want to output the value of the global variable, the output statement must be outside the scope of the local variable, so that you can call the value of the global variable without calling the local variable.

8. Red-black tree 

A red-black tree is a self-balancing binary search tree, and each node on the tree follows:

(1) Each node has red or black

(2) The root of the tree is always black, and red nodes cannot have red parent nodes or red child nodes

(2) There are the same number of black nodes on each path from the node to the NULL node

9. Use the preprocessing directive #define to declare a constant that indicates how many seconds in a year? (ignoring the leap year issue)

#define SECONDS_PER_YEAR (60 * 60 * 24 * 365)UL 

have to be aware of is

1. Since the macro definition is a preprocessing instruction, not a statement, it cannot end with a semicolon when making a macro definition.

2. Preprocessing will only perform simple substitutions, and will not calculate the value of the expression, so pay attention to the use of parentheses, and directly write how to calculate how many seconds in a year, instead of calculating the actual value.

3. Realize that this expression will overflow an integer number of a 16-bit machine, so the long integer symbol L is used to tell the compiler that this constant is a long integer number.  
4. UL: Indicates unsigned long integer

10. Write a standard macro MIN that takes two arguments and returns the larger one;

#define MIN(x,y) ((x) > (y) ? (x) : (y))

There are two points to note about macro definitions:

1. Whether the data type has the risk of overflow (such as the number of seconds in a year);

2. Each parameter of a macro with parameters must be enclosed in parentheses.

11. What is the difference between serial port and parallel port?

The serial interface is referred to as serial port for short, also known as serial communication interface or serial communication interface (commonly referred to as COM interface), which is an extended interface using serial communication. Serial interface (SerialInterface) refers to the sequential transmission of data one by one. Its characteristic is that the communication line is simple, as long as a pair of transmission lines can realize two-way communication (you can directly use the telephone line as the transmission line), thus greatly reducing the cost, especially Suitable for long-distance communication, but the transmission speed is slower. The parallel port is 8 bits transmitted side by side.

Parallel port generally refers to parallel interface. Parallel interface refers to an interface standard that uses parallel transmission to transmit data. The width of the data can be from 1 to 128 bits or wider, the most commonly used is 8 bits, and 8 data bits can be transmitted at a time through the interface.

12. Give the following definition with the variable a

a) an integer

int a;

b) A pointer to an integer, which points to an integer

int *a;

c) a pointer to a pointer,

int **a;

d) An array of 10 integers

int a[10];

e) An array with 10 pointers pointing to an integer.

int *a[10];

f) A pointer to an array of 10 integers

int (*a)[10];

13. What are the functions of the keywords extern and static?

external:

Extern is placed before variable and function declarations, indicating that the variable or function has been defined in another file, prompting the compiler to look for it from other files during compilation.

static:

(1) When modifying variables, the static local variables modified by static are only initialized once, and the life cycle of the local variables is extended, and they are not released until the end of the program.
(2) When static modifies a global variable, the global variable can only be accessed in this file, and cannot be accessed in other files, even if it is an external declaration of extern.
(3) Static modifies a function, then this function can only be called in this file, and cannot be called by other files. Static modified variables are stored in the static variable area of ​​the global data area, including global static variables and local static variables, all of which allocate memory in the global data area. It is automatically initialized to 0 during initialization.

14. What is the difference between heap and stack in C language?

1. The storage content is different

Stack: When a function is called, each parameter (local variable) in the function is stored in the stack. At the bottom of the stack is the next instruction after the function call.

Heap: Generally, one byte is used to store the size of the heap at the head of the heap. The specific content in the heap is arranged by the programmer.

2. Different management methods

Stack: The system automatically allocates space, and the system automatically releases space. For example, declare a local variable "int b" in a function. The system automatically creates space for b in the stack, and the stack space is automatically released when the corresponding lifetime ends.

Heap: The programmer needs to manually apply and release manually, and specify the size. In the C language, the malloc function is applied, the free function is released, and the new and delete are implemented in C++.

3. The size of the space is different

Stack: Get less space. Under Windows, the general size is 1M or 2M. When the remaining stack space is insufficient, the allocation failure overflows.

Heap: The obtained space is related to the effective virtual memory of the system, which is more flexible and larger.

4. Whether it can produce fragments is different

Stack: no fragmentation, continuous space.

Heap: The storage method of linked list is used, which will generate fragments.

5. Different growth directions

Stack: A data structure that extends to low addresses and is a continuous area of ​​memory.

Heap: A data structure that expands to high addresses and is a discontinuous memory area. This is because the system uses a linked list to store free memory addresses, which are naturally discontinuous, and the traversal direction of the linked list is from low address to high address.

6. Different allocation methods

Stack: There are 2 allocation methods - static allocation and dynamic allocation. Static is implemented by the compiler, such as local variables; dynamic is implemented by the alloca function, and the compiler will release.

Heap: All are dynamically allocated, there is no statically allocated heap.

7. Different distribution efficiency

Stack: automatically allocated by the system, faster. But the programmer is out of control.

Heap: The memory allocated by new is generally slow and prone to memory fragmentation, but it is convenient to use.

15. Briefly describe the steps of using interrupts in linux device drivers?

1. Add interrupt device node information in the device tree

like:

key_int_node {
compatible = "key3";
interrupt-parent = <&gpx1>;
interrupts = <2 4>;
};

2. Get the interrupt number

//根据路径获取设备树中的节点
struct device_node *of_find_node_by_path(const char *path);
参数:
参数1:
const char *path:设备树中节点路径
返回值:
获取到的节点

3. request_irq, application interruption

4.free_irq, release interrupt

16. In the Linux device driver, what is the mechanism for interrupting the lower half?

tasklet, softirq, work queue

17. How to transform a character device driver into a platform driver, and explain the matching process?

Guess you like

Origin blog.csdn.net/qq_53676406/article/details/129081107