当前位置:网站首页>Classic interview questions and answers for embedded engineers

Classic interview questions and answers for embedded engineers

2022-06-24 23:34:00 mialo163

Last week , Go to a company in Shenzhen to do ARM Development company interview ,HR Asked me to make a paper , It's full of C Programming , I'm happy in my heart , Because these questions are basically seen in the programmer interview Dictionary . Then I went back to school , Search online , So these questions are The embedded Classic interview questions for engineers , You can find it on many websites . Now post him , Attach online answers , Share with you , Because these questions are so classic .

The preprocessor (Preprocessor)

1 . With preprocessing instructions #define Declare a constant , To indicate 1 How many seconds in the year ( Ignore leap year problems )
      #define SECONDS_PER_YEAR (60 * 60 * 24 * 365)UL
I want to see a few things here :
1) #define Basic knowledge of grammar ( for example : Can't end with a semicolon , The use of brackets , wait )
2) Know that the preprocessor will calculate the value of the constant expression for you , So write directly how you calculate the number of seconds in a year instead of the actual value , It's clearer without cost .
3) Realize that this expression will make a 16 Integer overflow of bit machine - So we need to use the long integer symbol L, Tell the compiler that this constant is a long integer .
4) If you use... In your expression UL( Represents an unsigned long integer ), So you have a good starting point . remember , First impressions are important .

2 . Write a " standard " macro MIN , This macro takes two arguments and returns the smaller one .
      #define MIN(A,B) ((A) <= (B) ? (A) : (B))
This test is designed for the following purposes :
1) identification #define Basic knowledge of macro application . This is very important . Because in    The embedded (inline) The operator Become standard C Part of the previous , Macros are the only way to easily generate embedded code , For embedded systems , In order to achieve the required performance , Embedding code is often a must .
2) Knowledge of triple conditional operators . This operator exists C Language The reason for this is that it enables the compiler to produce better results than if-then-else Better code , It's important to understand this usage .
3) Know how to use parentheses to enclose parameters carefully in macros
4) I also use this question to start discussing the side effects of macros , for example : What happens when you write the following code ?
      least = MIN(*p++, b);

3. Preprocessor identification #error What is the purpose ?
       If you don't know the answer , See the references 1. This is a useful question to distinguish between a normal guy and a nerd . Only nerds can read C An appendix to a language textbook to find answers to questions like this . Of course, if you're not looking for a nerd , So it's better for candidates to hope they don't know the answer .

Dead cycle (Infinite loops)

4. Infinite loops are often used in embedded systems , How do you use C Write a dead cycle ?
There are several solutions to this problem . My first choice is :

while(1)
{

}

Some programmers prefer the following solution :

for(;;)
{

}

       I'm embarrassed by this implementation , Because this grammar doesn't express exactly what's going on . If a candidate gives this as a solution , I'm going to use this as an opportunity to explore the fundamentals of what they do . If their basic answer is :" I was taught to do this , But I never thought why ." It's going to leave a bad impression on me .

The third plan is to use goto
Loop:
...
goto Loop;
If the candidate gives the above scheme , This means that he is an assembly language programmer ( This may be a good thing ) Or he wants to enter a new field BASIC/FORTRAN The programmer .

Data statement (Data declarations)

5. With variable a Give the following definition
a) An integer number (An integer)
b) A pointer to an integer ( A pointer to an integer)
c) A pointer to a pointer , It points to an integer ( A pointer to a pointer to an intege)r
d) One has 10 An array of integers ( An array of 10 integers)
e) One has 10 An array of pointers , The pointer points to an integer .(An array of 10 pointers to integers)
f) One point has 10 A pointer to a group of integers ( A pointer to an array of 10 integers)
g) A pointer to a function , This function has an integer parameter and returns an integer (A pointer to a function that takes an integer as an argument and returns an integer)
h) One has 10 An array of pointers , The pointer points to a function , This function has an integer parameter and returns an integer ( An array of ten pointers to functions that take an integer argument and return an integer )

The answer is :
a) int a; // An integer
b) int *a; // A pointer to an integer
c) int **a; // A pointer to a pointer to an integer
d) int a[10]; // An array of 10 integers
e) int *a[10]; // An array of 10 pointers to integers
f) int (*a)[10]; // A pointer to an array of 10 integers
g) int (*a)(int); // A pointer to a function a that takes an integer argument and returns an integer
h) int (*a[10])(int); // An array of 10 pointers to functions that take an integer argument and return an integer

       It's often claimed that there are a few questions that you have to turn a book to answer , I agree with that . When I write this article , To make sure the grammar is correct , I did check the book . But when I was interviewed , I expect to be asked this question ( Or similar questions ). Because during the interview period , I'm sure I know the answer to this question . If the candidate doesn't know all the answers ( Or at least most of the answers ), So there is no preparation for this interview , If the interviewer is not prepared for the interview , So why can he prepare ?

Static

6. keyword static What is the role of ?
       Few people can answer this simple question completely . stay C In language , keyword static There are three obvious effects :
1) In function body , A variable declared as static maintains its value as the function is called .
2) In module ( But outside the function ), A variable declared static can be accessed by the functions used within the module , But it can't be accessed by other functions outside the module . It's a local global variable .
3) In module , A function declared as the first mock exam can only be called by other functions in this module . That's it , This function is restricted to the local scope of the module in which it is declared .

       Most candidates can answer the first part correctly , One part can answer the second part correctly , Few people can understand the third part . This is a serious weakness of a candidate , Because he obviously doesn't understand the benefits and importance of localizing data and code scope .

Const

7. keyword const What's the meaning of ?
       As soon as I hear the interviewee say :"const It means constant ", I knew I was dealing with an amateur . last year Dan Saks It has been fully summarized in his article const All uses of , therefore ESP( translator :Embedded Systems Programming) Every reader should be familiar with const What can and cannot be done . If you've never read that article , As long as you can say const signify " read-only " That's all right. . Although the answer is not complete , But I accept it as a correct answer .( If you want more detailed answers , Read it carefully Saks The article of .)
       If the candidate answers the question correctly , I'm going to ask him an additional question :
       What do the following statements mean ?

const int a;
int const a;
const int *a;
int * const a;
int const * a const;

/******/
       The first two have the same function ,a Is a constant integer . The third means a Is a pointer to a constant integer ( That is to say , Integers are immutable , But the pointer can ). The fourth meaning is a Is a constant pointer to an integer ( in other words , The integer that the pointer points to is modifiable , But the pointer is immutable ). The last one means a Is a constant pointer to a constant integer ( in other words , The integer that the pointer points to is immutable , At the same time, the pointer is also immutable ). If the examinee can answer these questions correctly , Then he left a good impression on me . By the way , Maybe you might ask , Even without keywords const, It's still easy to write programs that function correctly , So why should I value keywords so much const Well ? I also have the following reasons :
1) keyword const The purpose of this is to convey very useful information to people who read your code , actually , The purpose of declaring a parameter as a constant is to tell the user the application purpose of the parameter . If you've spent a lot of time cleaning up garbage left by others , You'll soon learn to appreciate this extra information .( Of course , Know how to use const Programmers rarely leave garbage for others to clean up .)
2) By giving the optimizer some additional information , Use keywords const Maybe it can produce more compact code .
3) Use keywords properly const It allows the compiler to naturally protect parameters that you don't want to change , Prevent it from being accidentally modified by code . In short , This can reduce bug Appearance .

Volatile

8. keyword volatile What's the meaning of ? And give three different examples .
       One definition is volatile The variable is that it can be changed unexpectedly , such , The compiler will not assume the value of this variable . To be precise, it means , The optimizer must carefully reread the value of this variable every time it is used , Instead of using a backup stored in a register . Here is volatile Several examples of variables :
1) Hardware registers for parallel devices ( Such as : Status register )
2) A non automatic variable that will be accessed in an interrupt service subroutine (Non-automatic variables)
3) A variable shared by several tasks in a multithreaded application

       People who can't answer this question will not be hired . I think it's a distinction C The most basic problem for programmers and embedded system programmers . Embedded guys often work with hardware 、 interrupt 、RTOS Wait a minute to deal with , All this requires the use of volatile Variable . Don't know volatile The content of will bring disaster .
       Suppose the interviewee answers the question correctly ( Um. , I wonder if it will be like this ), I'm going to dig into , Let's see if this guy understands volatile Total importance .
1) A parameter can be either const It can also be volatile Do you ? Explain why .
2) A pointer can be volatile Do you ? Explain why .
3) What's wrong with the following function :

int square(volatile int *ptr)
{
        return *ptr * *ptr;
}

Here's the answer :
1) Yes . One example is a read-only status register . It is volatile Because it can be changed unexpectedly . It is const Because the program should not try to modify it .
2) Yes . Although it's not very common . An example is when a middle service subroutine modifies a to point to a buffer The pointer of .
3) This code is a little abnormal . The purpose of this code is to return the pointer *ptr Point to the square of the value , however , because *ptr Point to one volatile Type parameter , The compiler will produce code similar to the following :

int square(volatile int *ptr)
{
    int a,b;
    a = *ptr;
    b = *ptr;
    return a * b;
}

because *ptr The value of can be changed unexpectedly , therefore a and b It could be different . result , This code may not be the square you expect ! The correct code is as follows :

long square(volatile int *ptr)
{
    int a;
    a = *ptr;
    return a * a;
}

Bit operation (Bit manipulation)

9. Embedded systems always require users to perform bit operations on variables or registers . Given an integer variable a, Write two pieces of code , The first setting a Of bit 3, The second one is to remove a Of bit 3. In the above two operations , To keep the other bits unchanged .
       There are three basic reactions to this question
1) I don't know how to start . The quilt has never done any embedded system work .
2) use bit fields.Bit fields It's being thrown into C The dead end of language , It ensures that your code is not portable between different compilers , It also ensures that your code is not reusable . Unfortunately, I have recently seen Infineon For its more complex communication chip driver , It uses bit fields So it's totally useless to me , Because my compiler does it in other ways bit fields Of . Morally speaking : Never let a non embedded guy stick to the edge of the actual hardware .
3) use #defines and bit masks operation . This is a highly portable approach , It's the method that should be used . The best solution is as follows :

#define BIT3 (0x1 << 3)
static int a;

void set_bit3(void)
{
    a |= BIT3;
}
void clear_bit3(void)
{
    a &= ~BIT3;
}

       Some people like to define a mask for setting and clearing values, as well as some explanatory constants , It's also acceptable . I'd like to see a few points : Explanatory constant 、|= and &=~ operation .

Access fixed memory locations (Accessing fixed memory locations)

10. Embedded systems often require programmers to access a specific memory location . In a project , Set an absolute address to 0x67a9 The value of the integer variable of is 0xaa66. The compiler is a pure ANSI compiler . Write code to do this .
       This question tests whether you know to cast an integer in order to access an absolute address (typecast) It's legal for a pointer . The way this problem is implemented varies with your personal style . The typical similar code is as follows :
    int *ptr;
    ptr = (int *)0x67a9;
    *ptr = 0xaa55;

  A more obscure approach is:
A more obscure method is :

    *(int * const)(0x67a9) = 0xaa55;

Even if your taste is closer to the second option , But I suggest you use the first option in your interview .

interrupt (Interrupts)

11. Interrupt is an important part of embedded system , This has led many compiler developers to provide an extension — Let the standard C Support interrupt . The representative fact is that , A new keyword has been created __interrupt. The following code uses __interrupt Keyword to define an interrupt service subroutine (ISR), Please comment on this code .

__interrupt double compute_area (double radius)
{
    double area = PI * radius * radius;
    printf("\nArea = %f", area);
    return area;
}

There are too many errors in this function , So that people do not know where to start :
1) ISR Cannot return a value . If you don't understand this , Then you won't be hired .
2) ISR Can't pass parameters . If you don't see that , Your chances of being hired are equal to the first one .
3) In many processors / In the compiler , Floating point is usually non reentrant . Some processors / The compiler needs to stack the registers at the top , Some processors / The compiler just doesn't allow ISR Do floating point operations in . Besides ,ISR It should be short and efficient , stay ISR It's not wise to do floating point operations in .
4) It's in line with the third point ,printf() There are always reentry and performance issues . If you lose the third and fourth points , I won't be too hard on you . Needless to say , If you can get the last two , So your employment prospects are getting brighter and brighter .

Code example (Code examples)

 


12 . What is the output of the following code , Why? ?

void foo(void)
{
    unsigned int a = 6;
    int b = -20;
    (a+b > 6) ? puts("> 6") : puts("<= 6");
}
       This question tests whether you know C The principle of integer automatic conversion in language , I find that some developers know very little about these things . No matter how , The answer to this unsigned integer problem is that the output is ">6". The reason is that when there are signed and unsigned types in expressions, all operands are automatically converted to unsigned types . therefore -20 It becomes a very large positive integer , So the result of this expression is greater than 6. This is very important for embedded systems that should frequently use unsigned data types . If you get the question wrong , You're on the verge of not getting the job .

13. Comment on the following code snippet :

unsigned int zero = 0;
unsigned int compzero = 0xFFFF;
/*1''s complement of zero */

For one int Type is not 16 Bit processor for say , The above code is incorrect . It should be written as follows :

unsigned int compzero = ~0;

       This question can really reveal whether the candidates understand the importance of word length . In my experience , Good embedded programmers understand the details of hardware and its limitations very accurately , However PC Computer programs often regard hardware as an unavoidable trouble .
       At this stage , The candidates are either downcast or full of confidence . If obviously the candidate is not very good , So that's the end of the test . But if it's obvious that the candidate is doing well , So I'll throw out the following additional question , These problems are relatively difficult , I think only very good candidates can do well . Ask these questions , I hope to see more ways for candidates to deal with problems , Not the answer . No matter how , Think of it as entertainment ...

Dynamic memory allocation (Dynamic memory allocation)

14. Although not as common as non embedded computers , Embedded system still has slave heap (heap) The process of dynamically allocating memory in . So in the embedded system , What are the possible problems with dynamically allocating memory ?
       here , I expect candidates to mention memory fragmentation , The problem of debris collection , Variable holding time and so on . The theme is already in ESP It's been widely discussed in magazines ( Mainly P.J. Plauger, His explanation goes far beyond any explanation I can mention here ), So look back at these magazines ! After putting the candidate into a false sense of security , I've got a little show like this :
       What is the output of the following code snippet , Why? ?

char *ptr;
if ((ptr = (char *)malloc(0)) == NULL)
    puts("Got a null pointer");
else
    puts("Got a valid pointer");

       This is an interesting question . Recently, one of my colleagues inadvertently put 0 The value is passed to the function malloc, After getting a legal pointer , I just thought of this question . This is the code above , The output of the code is "Got a valid pointer". I use this to start talking about a problem like this , See if the interviewee thinks that the library routine is the right thing to do . It's important to get the right answer , But the solution and the basic principles of your decision are more important .

Typedef

15 Typedef stay C Languages are often used to declare synonyms for an existing data type . You can do something similar with a preprocessor . for example , Consider the following example :

#define dPS struct s *
typedef struct s * tPS;

       The intention in both cases is to define dPS and tPS As a pointing structure s The pointer . Which is better ?( If any ) Why? ?
It's a very delicate question , Anyone who answers this question right ( Just cause ) It should be congratulated . The answer is :typedef Better . Consider the following example :

dPS p1,p2;
tPS p3,p4;

The first extension is

struct s * p1, p2;
.
The above code definition p1 It's a finger pointing to the structure ,p2 For a practical structure , This may not be what you want . The second example correctly defines p3 and p4 Two pointers .

Obscure grammar

16 . C Language agrees with some shocking structures , Is the following structure legal , If it's something it does ?

int a = 5, b = 7, c;
c = a+++b;

       This question will be a happy ending to the test . Believe it or not , The above example is completely grammatical . The problem is how the compiler handles it ? Low level compilers actually argue about this issue , According to the principle of maximum handling , The compiler should be able to handle as many legitimate uses as possible . therefore , The code above is processed as :

c = a++ + b;

       therefore , After this code line a = 6, b = 7, c = 12.
       If you know the answer , Or guess the right answer , well done . If you don't know the answer , I don't take this as a problem either . I found that the biggest advantage of this problem is that it is about coding style , Code readability , Code modifiability is a good topic

 

原网站

版权声明
本文为[mialo163]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211119255905.html

随机推荐