Technical question embedded c Flashcards
What is the segmentation fault error in C
Segmentation fault is a runtime error, which may occur due to some causes (listed below) when the program is running properly.
What are some common causes for the segmentation fault error in C
- 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.
What is ‘stack overflow’ error in C
This error may occur if if a pointer exceeds the stack limitations (boundaries).
Why do you think stack overflow transpires in the first place
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.
Why do we use ‘volatile’ keyboard in C
“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.
Why do we use ‘volatile’ keyboard in C
“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 to use a variable in a source file which is defined in another source file
“extern” keyboard can be used to declare a variable which allows accessing the variable in another file.
How will you protect a character pointer by some accidentally modification with the pointer address?
Constant character pointer (const char*) prevents the unnecessary modifications with the pointer address in the string.
Why do we use ‘static’ variable in C
- 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.
Can you highlight the differences between CISC and RISC
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.
What do you understand by a function pointer?
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.
What do you understand by the term structure padding?
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.
Can you tell us something about the dangling pointers in embedded C
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.
What is the function of the keyword const? Give an example.
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.
Why should we use the keyword const in the first place
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.
What is the chief functionality of realloc
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.
Can parameter be both const and volatile
Yes. Example is read only status register. Volatile cause can change unexpectedly. Const cause program should not change it
What is an inline function
- 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.
Is Count Down_to_Zero Loop better than Count_Up_Loops
- 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
Can structures be passed to the functions by value
- 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.
What is the size of character, integer, integer pointer, character pointer
- 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.