preface

As embedded software engineer , When you change jobs, you usually get one first C/C++ Interview questions . Here is a summary of what you or your friends have met C/C++ Interview questions , For your reference .

example

1, Zero comparison
//bool Zero value comparison of type bool flag; if(flag){ if(!flag){ } } //float Zero value comparison of type float x;
const float EPSINON = 0.00001 if((x >= -EPSINON) && (x <= EPSINON)) // Zero value comparison of pointer types
char *p; if(p == NULL){ if(p != NULL){ } }
2,sizeof

sizeof Used to calculate the memory size of a variable or type , Return in bytes ;
char str[] = "Hello"; char *p = str; int n = 10; sizeof(str) =
6;//Hello occupy 5 Bytes , The ending character occupies one byte sizeof(p) = 4;// commonly 32 Bit machine , Pointer type variable accounts for 4 Bytes sizeof(n) =
4;// commonly 32 Bit machine ,int Proportion of type variable 4 Bytes void Func(char str[100]){ sizeof(str) =
4;// When a function is not called , Parameter does not allocate memory ; When a function is called , Parameter allocated memory , Until the function is finished
// BI ; Transitive str[100] It's an array , Therefore, the parameters of the incoming function can be understood as str[100] Address of , // So the 4 Bytes } void *p =
malloc(100); sizeof(p) = 4;// Same as pointer type explanation
3, Definition of different types of variables
int a;// An integer int *a;// A pointer to an integer int **a;// A pointer to a pointer type , Its pointer points to an integer int
a[10];// One has 10 individual int Array of type elements int *a[10];// One has 10 Points int Array of pointer type elements of type int
(*a)[10];// An array pointer , Point to have 10 individual int Array of type elements int
(*a)(int);// A function pointer , This function has a int Type and returns a int Type data int
(*a[10])(int);// One has 10 Array of pointers , The pointer points to a function , This function has a int Type and returns a int // Type data
4,

5,

6,

7,

8,

9,

10,

11,

12,

13, keyword volatile What does it mean ? And give three different examples .

A, One is defined as volatile The variable of means that this variable may be changed unexpectedly , such , The compiler will not assume the value of this variable . Exactly , The optimizer must carefully reread the value of this variable every time it uses it , Instead of using a backup stored in a register .

B, example

1) Hardware register of parallel device ( as : Status register )

2) Non automatic variables to be accessed in an interrupt service subroutine

3) Variables shared by several tasks in multithreaded applications

14,

 

15, In the header file ifndef/define/endif To prevent the header file from being repeatedly referenced .

16,#include <filename.h> and #include "filename.h" What's the difference? ?

#include <filename.h> Tell the compiler , Priority search from standard library filename.h;

#include "filename.h" Tell the compiler , Prioritize search from user's work path filename.h;

17,const What's the role ?

(1)const Can modify constant

(2) cover const Variable decorated as read-only , Parameters of function , The return value is protected , Can prevent accidental changes , Can improve the robustness of the program .

18, Briefly explain the key words static and volatile The role of ?

(1)static When modifying a local variable , Life domain is global , Scope is local ;

static Decorate a C When global variables in the source file , Life domain is global , Scope reduction only applies to this document ;

static When modifying a function , Life domain is global , Scope reduction only applies to this document ;

(2)volatile Indicates that the variable being decorated is variable , Warn compiler to read from memory address of variable every time , Instead of reading a backup register ;

(3)const and volatile You can modify a variable at the same time , for example : Read only status register ;

const and static Cannot modify a variable at the same time , because C++ Compiler in implementation const To ensure that the function cannot modify the state of an instance of a class , An implicit parameter is added to the function const
this *. But when a member is static When , This function does not this Pointer's . That is to say, at this time static Usage and static It's conflicting .

19, The difference between linked list and array ?

(1) In memory , An array is a continuous area , The linked list is a random discrete region .

(2) Find read data block of array , Because it's continuous , If you know a data address, you can find the corresponding data ; But the efficiency of data insertion and deletion is low , High requirements for memory space , There must be enough continuous space , May cause memory waste .

(3) High efficiency of data insertion and deletion of linked list , High memory utilization and flexible expansion ; But it is slow to find and read data , You must use traversal to find data .

20, How to understand the process of interruption ?

The process of interruption can be divided into sequence 4 Parts : Push , Take vector , Update register and exception return ;

(1) Push : Save site , Data bus in turn xPSR,PC,LR,R12 and R3~R0 Stack pressing ;

(2) Take vector : Instruction bus takes service function entry address from vector table ;

(3) Update register : Update stack pointer SP,PSR,PC(PC Point to service function entry address ),LR;

(4) Exception return : Restore site ( Data out of stack ), to update NVIC register ;

Tips:LR(R14) Is store return address , So when a subroutine of level 1 depth is called back , Can be read directly LR Instead of reading memory ; When multiple levels of depth, the return address of the previous level needs to be pushed into the stack ;

21, Can the interrupt service function have input parameters and return values ? Why? ?

Interrupt service function cannot have input parameter and function return . Because interrupt is an electrical signal generated automatically by hardware peripherals , Called by hardware itself , No program to pass parameters to hardware , Hardware will not receive parameters . Moreover, the faster the interrupt processing is, the better to ensure the real-time performance in the real-time operating system , as Linux Hard interrupt and soft interrupt mechanism of the system .

22, How to understand semaphores , Mutex semaphore ?

(1) Semaphore is a sign used to indicate the occupancy of a resource .

Function of semaphore :A, Synchronize tasks ;B, Manage multiple shared resources ;

(2) Mutex semaphore is a value only 0 and 1 Binary semaphores in two cases , Realize mutually exclusive access to resources .

Functions of mutually exclusive semaphores :A, Prevent task priority reversals ;

23, In a nutshell ARM Processor architecture and C51 Differences in processor architecture ?

(1)ARM yes 32 position RISC( Reduced instruction set ) General term of microprocessor architecture ;C51 yes 8 position CISC( Complex instruction set ) General term of microprocessor architecture ;

(2)ARM Adopt the core structure of von Neumann or Harvard , as Cortex-M3 It's a Harvard structure ;C51 Mechanism kernel with von Neumann ;

24, In a nutshell ,UCOS II and UCOS III The difference between ?

(1)UCOS II only 0~63 Priority , And the priority cannot be repeated ;UCOS III Allow several tasks to use the same priority , In the same priority , Support time slice scheduling .

(2)UCOS III Allow users to dynamically configure real-time operating system kernel resources during program running , Avoid the problem of insufficient resources in programming .

(3)UCOS II Maximum support 256 Tasks ;UCOS III Support any task , Semaphore , Message list and memory block capacity , Limited to users only CPU Available RAM capacity ;

25, In a nutshell ,UCOS II Medium message queue , Difference between message mailbox and semaphore ?

(1) When synchronizing behavior with semaphores , Only synchronous time information can be provided , Unable to provide content information . If the controlled party requires the content information of the controlled party , You can use message mailboxes or message queues .

(2) But there is only one message in the message mailbox , So when using message mailbox for task synchronization , A condition needs to be met : Messages are always generated slower than messages are consumed , That is, the controlled task is always waiting for messages , Otherwise, the message will be lost .

(3) In case that message generation speed may be faster than message consumption speed , You can use a message queue that is more powerful than a message mailbox , Because message queues can hold multiple messages , So message queuing can effectively solve the problem of temporary message stacking . However, the use of message queuing still needs to meet one condition : The average message generation rate is lower than the average message consumption rate , Otherwise, any longer message queue will overflow .

26, In a nutshell , Reentrant and non reentrant functions ?

(1) Reentrant function : Reentry means that this function can be entered repeatedly , Can be called in parallel , Can be interrupted , It only uses data variables on its own stack , It does not depend on the task environment , In multi task scheduling process , It's safe , Don't worry about data errors ;

(2) Non reentrant means non parallel scheduling , Otherwise, unexpected results will be produced , Global variables are commonly used within these functions , static state (static) Data structure of , Used malloc perhaps free function , Standards used I/O Functions, etc ;

(3) It can be understood that they are mutually exclusive , What is not a non reentrant function is a reentrant function ;

Technology
©2019-2020 Toolsou All rights reserved,
JS How to operate java Realize the function of grabbing red packets C Language programming to find a student's grade The United Nations 《 Glory of Kings 》 Please go to the studio : To save the earth Dialogue between apple and Nissan suspended ,Apple Car How's it going ?CSS architecture design China's longest high speed rail officially opened ! The fastest way to finish the race 30.5 hour First knowledge MySQL Comprehensive review ( dried food )2021 year 1 Monthly programmer salary statistics , average 14915 element How to use it quickly html and css Write static page