Starting from today, I will try to make a summary of popular embedded system interview questions. I will start from C/C++ puzzles and go all the way to general questions, and answer most of them. Some answers may not be that accurate but they will give you a general idea of how to answer the questions.
C vs Embedded C vs C++
C is a language widely used in embedded system. The embedded C usually needs to be very small in size since embedded system usually have tight memory constraints.
C++ is object-oriented extension of C. Compare to C it integrates a lot of useful libraries to handle various data structures.
C compilation steps
Preprocessing, compiling, assembly, linking
Know about “static, global/extern, auto, register, const and volatile” very well. When do you use static keyword for a global variable?
Static is a keyword used to keep a variable that is global across the objects of the same class.
The extern keyword may be applied to a global variable, function, or template declaration. It specifies that the symbol has external linkage.
The auto keyword indicates a default storage class. Commonly used in C++.
Register is a hint to the compiler that the variable will be heavily used and that it is recommended to be kept in a processor register if possible.
The const keyword specifies that a variable's value is constant and tells the compiler to prevent the programmer from modifying it.
The volatile keyword tells the compiler to refrain from caching it since the value can be updated very frequently, e.g. from external devices, services, etc.
Use of volatile keyword?
Widely used when the variable’s value keeps changing, e.g. a register value from external devices.
Know about union and enum.
A union is a special class type that can hold only one of its non-static data members at a time.
An enumeration is a distinct type whose value is restricted to a range of values.
What is the difference between Structure and union? Where do we use union? Structs and Unions and their padding.
A union is a type consisting of a sequence of members whose storage overlaps, as opposed to struct, which is a type consisting of a sequence of members whose storage is allocated in an ordered sequence.
The union is only as big as necessary to hold its largest member (additional unnamed trailing padding may also be added). The other members are allocated in the same bytes as part of that largest member.
Struct padding is compiler dependent. Also, even if you use the same compiler but declare members in different orders the results could still be different.
Know about basic pointers concepts. Double Pointers. Pointer aliasing, Multiple indirection.
Double pointer is a pointer to pointer. Can be used for arrays of objects.
Pointer aliasing is a hidden kind of data dependency that can occur in C, C++, or any other language that uses pointers for array addresses in arithmetic operations. Pointer aliasing in C++ inhibits vectorization and other optimizations, and, hence, performance.
C permits the pointer to point to another pointer. This creates many layers of pointer and therefore called as multiple indirection.
Function pointers and Callback functions
A function pointer is a variable that stores the address of a function that can later be called through that function pointer. See below for an example.
A callback is a callable accepted by a class or function, used to customize the current logic depending on that callback.
Char pointer, int pointer, etc. and their sizes?
Pointing to char, int, etc. Size is 32-bit for 32-bit machine, and 64-bit for 64-bit machine.
Null point, void point and their uses?
Null pointer is an empty pointer which points to nothing. Can used as a place holder for variable value.
Void pointer is a pointer points to some data of type “void”. Can be used for type conversion.
malloc vs calloc vs realloc.
Dynamic allocate memory, allocate contiguous memory, and re-allocation with the original garbage values.
File operations in Linux
Operations open/close with permission.
Typecasting
Converting an expression of a given type into another type is known as type-casting.
Inline function. Difference between Inline and macro.
Inline functions are handled by compiler to be expanded into their corresponding assembly code.
Macros are handled in preprocessing phase and should be discouraged.
Preprocessor directives.
Preprocessor directives, such as #define and #ifdef, are typically used to make source programs easy to change and easy to compile in different execution environments.
Bit manipulation - Set, Get, Clear, Toggle, Shift, Display Bits
Use operations such as &, |, ^, etc.
Bit Fields in C
See below for an example of 1 bit within a 32-bit int.
Difference between heap and stack? Write a function to figure out if stack grows up or down.
Every time when we made an object it always creates in Heap-space and the referencing information to these objects are always stored in Stack-memory.
Stack can grow upward or downward depending on the compiler. See below for a program to figure this out.
Memory leak
This happens when users do not free their memory after execution of a program.