Technical question embedded c Flashcards

1
Q

What is the segmentation fault error in C

A

Segmentation fault is a runtime error, which may occur due to some causes (listed below) when the program is running properly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are some common causes for the segmentation fault error in C

A
  • Usages of the dereferenced pointer (i.e. a pointer which may not have a valid address/memory location to point).
  • If you are trying to access a memory area which is read-only. In that case, the program may return segmentation fault error.
  • It may also occur when you try to free a memory (using a pointer), which is already freed.
    Segmentation fault is the reason to generate stack overflow error in C.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is ‘stack overflow’ error in C

A

This error may occur if if a pointer exceeds the stack limitations (boundaries).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Why do you think stack overflow transpires in the first place

A

There could be many potential reasons behind the occurrence of the stack overflow. For instance, redundant arguments and clumsy application of recursion could be the leading proponents. Apart from that, it may occur due to the construction of an outsize local array and the presence of nested function calls.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why do we use ‘volatile’ keyboard in C

A

“volatile” is used to prevent the compiler to optimize any variable. When any variable is used frequently, the compiler optimizes it and keeps the variables in his memory (there are some specific memory blocks (registers), from there variable is accessibility is fast) to serve its value faster to the program.

Therefore, a “volatile” is used to prevent the compiler for any type of optimization on the variable. Volatile variables are used with those variables which are used to communicate with the computer hardware, signals, handlers etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Why do we use ‘volatile’ keyboard in C

A

“volatile” is used to prevent the compiler to optimize any variable. When any variable is used frequently, the compiler optimizes it and keeps the variables in his memory (there are some specific memory blocks (registers), from there variable is accessibility is fast) to serve its value faster to the program.

Therefore, a “volatile” is used to prevent the compiler for any type of optimization on the variable. Volatile variables are used with those variables which are used to communicate with the computer hardware, signals, handlers etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to use a variable in a source file which is defined in another source file

A

“extern” keyboard can be used to declare a variable which allows accessing the variable in another file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How will you protect a character pointer by some accidentally modification with the pointer address?

A

Constant character pointer (const char*) prevents the unnecessary modifications with the pointer address in the string.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why do we use ‘static’ variable in C

A
  • A static variable does not redeclare that means if it is declared in a function it will not redeclare on each function call, therefore, static is able to maintain the value.
  • Its scope is local but it is live until the end of the program.
  • Generally, it is used to count something, for example, there is function openBakAccount() which calls every time when a new account opens in the bank. Then, to count the total number of the opened account, we can declare a static variable in the function and can increase it on each function call.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Can you highlight the differences between CISC and RISC

A

There are many differences between the two. Reduced Instruction Set Computer, or RISC, does not consist of a memory unit unlike the CISC, or Complex Instruction Set Computer. Secondly, RISC is a relatively faster processor than CISC in terms of calculations. Also, RISC ensures a simple decoding of operations unlike a CISC processor. Lastly, RISC has a very low execution time compared to CISC.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What do you understand by a function pointer?

A

As the name suggests, it is a pointer that points to a function instead of a variable. And this is precisely where a function pointer stands apart from the class of other pointers. Technically speaking, a function pointer is one which stores the address of a particular function in order to be availed of by the concerned program through function invoking.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What do you understand by the term structure padding?

A

Padding is essentially the induction of redundant bytes into a particular structure. More specifically, it is the function of the compiler to embed some extra bytes between the units of either a structure or a union. This is known as the process of padding. Basically, structure padding is used for the purpose of data type orientation. Consequently, structure padding ends up augmenting the performance quotient of the processor.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Can you tell us something about the dangling pointers in embedded C

A

Dangling pointers are clearly aberrations that arise due to the obliteration of a referencing object. It is known as dangling because the pointer is essentially pointing to a ghost memory, one that is not at its disposal. Consequently, it gives rise to what is known as the segmentation fault.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the function of the keyword const? Give an example.

A

So far as the keyword const is concerned, it is basically used to make a variable read-only type. For example: const int iData=0; During compilation, the keyword const chiefly acts as an indication to the compiler that the value of the declaring object is not subject to further alteration. So, it stands that the keyword is incapable of reassigning at runtime.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Why should we use the keyword const in the first place

A

Indeed, const has many essential applications throughout the course of a program. For instance, it is of great use in a call by reference function argument. Secondly, const is particularly useful when you do not seek to change the value of an initialized variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the chief functionality of realloc

A

As the titular indication suggests, it is chiefly used to resize the allocation of memory. Technically, it takes in two arguments. The first one is the pointer to the formerly allocated memory while the second one is the reference to the new size.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Can parameter be both const and volatile

A

Yes. Example is read only status register. Volatile cause can change unexpectedly. Const cause program should not change it

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is an inline function

A
  • Inline function is a feature to increase execution time of program and reduce function calling overhead.
  • With inline, compiler replaces the function call statement with function code itself. This compile does not have to jump to another location to execute the function.
  • Disadvantage includes increases the executable size due to code expansion.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Is Count Down_to_Zero Loop better than Count_Up_Loops

A
  • Count down to zero loops are better.
  • Reason behind this is that at loop termination, comparison to zero can be optimized by the compiler.
  • Most processors have instruction for comparing to zero. So they don’t need to load the loop variable and the maximum value, subtract them and then compare to zero. That is why count down to zero loop is better
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Can structures be passed to the functions by value

A
  • Passing structure by its value to a function is possible, but not a good programming practice.
  • First of all, if we pass the structure by value and the function changes some of those values, then the value change is not reflected in caller function.
  • Also, if the structure is big, then passing the structure by value means copying the whole structure to the function argument stack which can slow the program by a significant amount.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What is the size of character, integer, integer pointer, character pointer

A
  • The size of character is 1 byte.
  • Size of integer is 4 bytes.
  • Size of integer pointer and character is 8 bytes on 64 bit machine and 4 bytes on 32 bit machine.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What is a NULL pointer and what is its use

A

The NULL is a macro defined in C. Null pointer actually means a pointer that does not point to any valid location. We define a pointer to be null when we want to make sure that the pointer does not point to any valid location and not to use that pointer to change anything. If we don’t use null pointer, then we cannot verify whether this pointer points to any valid location or not.

23
Q

What is a void pointer and what is its use

A

The void pointer means that it points to a variable that can be of any type. Other pointers points to a specific type of variable while void pointer is a somewhat generic pointer and can be pointed to any data type, be it standard data type(int, char etc) or user define data type (structure, union etc.). We can pass any kind of pointer and reference it as a void pointer.

24
Q

What is ISR

A

An ISR(Interrupt Service Routine) is an interrupt handler, a callback subroutine which is called when a interrupt is encountered.

25
Q

What is the return type of ISR

A

ISR does not return anything. An ISR returns nothing because there is no caller in the code to read the returned values.

26
Q

How to reduce interrupt latency?

A

Interrupt latency can be minimized by writing short ISR routine and by not delaying interrupts for more time.

27
Q

Can we use any function inside ISR

A

We can use function inside ISR as long as that function is not invoked from other portion of the code.

28
Q

Can we use printf inside ISR

A

Printf function in ISR is not supported because printf function is not reentrant, thread safe and uses dynamic memory allocation which takes a lot of time and can affect the speed of an ISR up to a great extent.

29
Q

Can we put a breakpoint inside ISR

A

Putting a break point inside ISR is not a good idea because debugging will take some time and a difference of half or more second will lead to different behavior of hardware. To debug ISR, definitive logs are better.

30
Q

Can static variables be declared in a header file

A

A static variable cannot be declared without defining it. A static variable can be defined in the header file. But doing so, the result will be having a private copy of that variable in each source file which includes the header file. So it will be wise not to declare a static variable in header file, unless you are dealing with a different scenario.

31
Q

Can include files be nested

A

Yes. Include files can be nested any number of times. But you have to make sure that you are not including the same file twice. There is no limit to how many header files that can be included. But the number can be compiler dependent, since including multiple header files may cause your computer to run out of stack memory.

32
Q

What is the difference between hard real-time and soft real-time OS

A

A Hard real-time system strictly adheres to the deadline associated with the task. If the system fails to meet the deadline, even once, the system is considered to have failed. In case of a soft real-time system, missing a deadline is acceptable. In this type of system, a critical real-time task gets priority over other tasks and retains that priority until it completes.

33
Q

What type of scheduling is there in RTOS

A

RTOS uses pre-emptive scheduling. In pre-emptive scheduling, the higher priority task can interrupt a running process and the interrupted process will be resumed later.

34
Q

What is priority inversion

A

If two tasks share a resource, the one with higher priority will run first. However, if the lower-priority task is using the shared resource when the higher-priority task becomes ready, then the higher-priority task must wait for the lower-priority task to finish. In this scenario, even though the task has higher priority it needs to wait for the completion of the lower-priority task with the shared resource. This is called priority inversion.

35
Q

What is priority inheritance

A

Priority inheritance is a solution to the priority inversion problem. The process waiting for any resource which has a resource lock will have the maximum priority. This is priority inheritance. When one or more high priority jobs are blocked by a job, the original priority assignment is ignored and execution of critical section will be assigned to the job with the highest priority in this elevated scenario. The job returns to the original priority level soon after executing the critical section.

36
Q

What is a semaphore

A

Semaphore is actually a variable or abstract data type which controls access to a common resource by multiple processes.

37
Q

Types of Semaphores

A
  • Binary semaphore – It can have only two values (0 and 1). The semaphore value is set to 1 by the process in charge, when the resource is available.
  • Counting semaphore – It can have value greater than one. It is used to control access to a pool of resources.
38
Q

What is spin lock

A

If a resource is locked, a thread that wants to access that resource may repetitively check whether the resource is available. During that time, the thread may loop and check the resource without doing any useful work. Suck a lock is termed as spin lock.

39
Q

In platforms with significant constraints on memory size, is it more preferable to allocate memory statically or dynamically?

A

It’s preferable to use static memory allocation on platforms with memory sizes in the low kilobytes and below. This is because data overhead, CPU overhead, and memory fragmentation can be significant issues when using dynamic memory allocation.

40
Q

Which parameters decide the size of data type for a processor

A

Actually, compiler is the one responsible for size of the data type. But it is true as long as OS allows that. If it is not allowable by OS, OS can force the size.

41
Q

Significance of watchdog timer in Embedded Systems

A

The watchdog timer is a timing device with a predefined time interval. During that interval, some event may occur or else the device generates a time out signal. It is used to reset to the original state whenever some inappropriate events take place which can result in system malfunction. It is usually operated by counter devices.

42
Q

Why ++n executes faster than n+1

A

The expression ++n requires a single machine instruction such as INR to carry out the increment operation. In case of n+1, apart from INR, other instructions are required to load the value of n. That is why ++n is faster.

43
Q

When should we use register modifier

A

The register modifier is used when a variable is expected to be heavily used and keeping it in the CPU’s registers will make the access faster.

44
Q

What is NVIC in ARM Cortex

A

The Nested Vector Interrupt Controller (NVIC) in the Cortex-M processor family is an example of an interrupt controller with extremely flexible interrupt priority management. It enables programmable priority levels, automatic nested interrupt support, along with support for multiple interrupt masking, whilst still being very easy to use by the programmer.

45
Q

Can we change the interrupt priority level of Cortex-M processor family

A

Yes, we can.

46
Q

What is the start-up code

A

A start-up code is called prior to the main function, it creates a basic platform for the application. It is a small block of code that is written in assembly language.

47
Q

Are integers signed or unsigned

A

According to C standard, an integer data type is by default signed. So if you create an integer variable, it can store both positive and negative value.

48
Q

What do you mean by enumeration in C

A

An enum in C is a user-defined data type. It consists set of named constant integers. Using the enum keyword, we can declare an enumeration type by using the enumeration tag (optional) and a list of named integer.

49
Q

What is the endianness

A

The endianness is the order of bytes to store data in memory and it also describes the order of byte transmission over a digital link.

50
Q

What is the difference between malloc and calloc

A

A malloc and calloc are memory management functions. They are used to allocate memory dynamically. Basically, there is no actual difference between calloc and malloc except that the memory that is allocated by calloc is initialized with 0.

51
Q

What is the return value of malloc (0)

A

If the size of the requested space is zero, the behavior will be implementation-defined. The return value of the malloc could be a null pointer or it shows the behavior of that size is some nonzero value. It is suggested by the standard to not use the pointer to access an object that is returned by the malloc while the size is zero.

52
Q

Where can the function pointers be used

A

There are a lot of places, where the function pointers can be used. Generally, function pointers are used in the implementation of the callback function, finite state machine and to provide the feature of polymorphism in C language …etc.

53
Q

What is void or generic pointers in C

A

A void pointer in c is called a generic pointer, it has no associated data type. It can store the address of any type of object and it can be type-casted to any type.

54
Q

What is virtual memory

A

The virtual memory is the part of memory management techniques and it creates an illusion that the system has a sufficient amount of memory. In other words, you can say that virtual memory is a layer of indirection.