0% found this document useful (0 votes)
76 views6 pages

Tips of Embedded C

Here are the key differences between the type qualifiers in C: - const prevents modification of the qualified object - volatile indicates the value may change unexpectedly and prevents optimizations that assume no changes - restrict qualifies a pointer to indicate the only pointer that can reference the object, allowing more optimizations So in summary: - const prevents writes - volatile prevents caching and assumes changes - restrict enables optimizations by restricting aliasing They provide different restrictions and guarantees about how the qualified object can be accessed and optimized. Const prevents modification, volatile prevents assumptions of no changes, and restrict enables caching by restricting aliasing.

Uploaded by

mahamd saied
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views6 pages

Tips of Embedded C

Here are the key differences between the type qualifiers in C: - const prevents modification of the qualified object - volatile indicates the value may change unexpectedly and prevents optimizations that assume no changes - restrict qualifies a pointer to indicate the only pointer that can reference the object, allowing more optimizations So in summary: - const prevents writes - volatile prevents caching and assumes changes - restrict enables optimizations by restricting aliasing They provide different restrictions and guarantees about how the qualified object can be accessed and optimized. Const prevents modification, volatile prevents assumptions of no changes, and restrict enables caching by restricting aliasing.

Uploaded by

mahamd saied
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

TIPS of EmbeddedC language

What are Lvalues and Rvalues?


An object is a contiguous region of memory storage. An lvalue (pronounced: L value) is
an expression that refers to such an object. The original definition of lvalue referred to
"an object that can appear on the left-hand side of an assignment." However, const
objects are lvalues, and yet they cannot appear on the left-hand side of an assignment. An
expression that can appear in the right-hand side of an expression (but not in the left-hand
side of an expression) is an rvalue. For example:

#include <string>
using namespace std;
int& f();

void func()
{
int n;
char buf[3];
n = 5; // n is an lvalue; 5 is an rvalue
buf[0] = 'a'; //buf[0] is an lvalue, 'a' is an rvalue
string s1 = "a", s2 = "b", s3 = "c"; // "a", "b", "c" are rvalues
s1 = // lvalue
s2 +s3; //s2 and s3 are lvalues that are implicitly converted to rvalues
s1 =
string("z"); // temporaries are rvalues
int * p = new int; // p is an lvalue; 'new int' is an rvalue
f() = 0; // a function call that returns a reference is an lvalue
s1.size(); // otherwise, a function call is an rvalue expression
}

An lvalue can appear in a context that requires an rvalue; in this case, the lvalue is
implicitly converted to an rvalue. An rvalue cannot be converted to an lvalue. Therefore,
it is possible to use every lvalue expression in the example as an rvalue, but not vice
versa.
How is a signed negative number stored in memory?
A:
In most intel architectures, signed negative numbers are stored in memory as two's
Complement.
0101 = +5
1011 = -5

Which is the best way to write Loops?

Q: Is Count Down_to_Zero Loop better than Count_Up_Loops?


A: Always, count Down to Zero loops are better.
This is because,at loop termination, comparison to Zero can be optimized by complier.
(Eg using SUBS)
Else, At the end of the loop there is ADD and CMP.

What is loop unrolling?


A: Small loops can be unrolled for higher performance, with the disadvantage of
increased code size. When a loop is unrolled, a loop counter needs to be updated less
often and
fewer branches are executed. If the loop iterates only a few times, it can be fully unrolled,
so that the loop overhead completely disappears.
eg:
int countbit1(uint n)
{ int bits = 0;
while (n != 0)
{
if (n & 1) bits++;
n &gt;&gt;= 1;
}
return bits;
}

int countbit2(uint n)
{ int bits = 0;
while (n != 0)
{
if (n & 1) bits++;
if (n & 2) bits++;
if (n & 4) bits++;
if (n & 8) bits++;
n &gt;&gt;= 4;
}
return bits;
}

How does, taking the address of local variable result in unoptimized code?
A: The most powerful optimization for compliler is register allocation. That is it operates the
variable from register, than memory. Generally local variables are allocated in registers.
However if we take the address of a local variable, compiler will not allocate the variable to
register.

How does global variable result in unoptimized code?


A: For the same reason as above, compiler will never put the global variable into register. So its
bad.
Which is better a char, short or int type for optimization?
A: Where possible, it is best to avoid using char and short as local variables. For the types
char and short the compiler needs to reduce the size of the local variable to 8 or 16 bits
after each assignment. This is called sign-extending for signed variables and zeroextending
for unsigned variables. It is implemented by shifting the register left by 24 or 16
bits, followed by a signed or unsigned shift right by the same amount, taking two
instructions (zero-extension of an unsigned char takes one instruction).
These shifts can be avoided by using int and unsigned int for local variables. This
is particularly important for calculations which first load data into local variables and then
process the data inside the local variables. Even if data is input and output as 8- or 16-bit
quantities, it is worth considering processing them as 32-bit quantities.

1. Can structures be passed to the functions by value?


Yes , will be waste of memory because call by value mean the function will allocate
locale memory in stack to save this local variables which in this case be the hole structure
The best way is to pass by address
2. Why cannot arrays be passed by values to functions?
When a array is passed to a function, the array is internally changed to a 'pointer'. And
pointers are always passed by reference.
3. Advantages and disadvantages of using macro and inline functions?
Advantage: Macros and Inline functions are efficient than calling a normal function. The
times spend in calling the function is saved in case of macros and inline functions as
these are included directly into the code.

Disadvantage: Macros and inline functions increased the size of executable code.

Difference in inline functions and macro


1) Macro is expanded by preprocessor and inline function are expanded by compiler.
2) Expressions passed as arguments to inline functions are evaluated only once while
_expression passed as argument to inline functions are evaluated more than once.

4. What happens when recursion functions are declared inline?


inline function's property says whenever it will called, it will copy the complete
definition of that function. Recursive function declared as inline creates the burden on the
compiler's execution.
The size of the stack may/may not be overflow if the function size is big.
5. Scope of static variables?
if we want the value to be extent throughout the life of a program, we can define the local
variable as "static."

23. Which way of writing infinite loops is more efficient than others? there are 3ways.
While(1)
{
statements
}

or

do
{
statements
}
while(1);
or
for( ; ; )

27. What is interrupt latency?


In real-time operating systems, interrupt latency is the time between the generation of an
interrupt by a device and the servicing of the device which generated the interrupt.
What are the different storage classes in C?
There are four type of storage classes in C. These are used
to store variables. These are Extern, Static, Register and
Auto.
1. Auto is the default class assigned to any variable.
eg. if we define int x=10; then it means auto int x=10
2. register and static differ in only one grounds that
register is there in the cpu and static in the main memory.
3. extern is global and can be accessed by any program and
anywhere.

What are the different qualifiers in C?

Type qualifiers can be characterized by the restrictions they impose on access and
cacheing.

* const

No writes through this lvalue. In the absence of this qualifier, writes may occur
through this lvalue.
* volatile

No cacheing through this lvalue: each operation in the abstract semantics must be
performed (that is, no cacheing assumptions may be made, since the location is not
guaranteed to contain any previous value). In the absence of this qualifier, the contents of
the designated location may be assumed to be unchanged except for possible aliasing.
* restrict

Objects referenced through a restrict-qualified pointer have a special association with


that pointer. All references to that object must directly or indirectly use the value of this
pointer. In the absence of this qualifier, other pointers can alias this object. Cacheing the
value in an object designated through a restrict-qualified pointer is safe at the beginning
of the block in which the pointer is declared, because no pre-existing aliases may also be
used to reference that object. The cached value must be restored to the object by the end
of the block, where pre-existing aliases again become available. New aliases may be
formed within the block, but these must all depend on the value of the restrict-qualified
pointer, so that they can be identified and adjusted to refer to the cached value. For a
restrict-qualified pointer at file scope, the block is the body of each function in the file.

You might also like