UNIT 5 High
UNIT 5 High
((DATA STRUCTURE))
Static, Automatic, External Control & Based Storage. Implementation, Block structure
1.INTRODUCTION
The study of data structures helps to understand the basic concepts involved in organizing and storing
data as well as the relationship among the data sets. This in turn helps to determine the way information is
stored, retrieved and modified in a computer’s memory.
Data structure is a branch of computer science. The study of data structure helps you to understand how
data is organized and how data flow is managed to increase efficiency of any process or program. Data
structure is the structural representation of logical relationship between data elements. This means that a
data structure organizes data items based on the relationship between the data elements. Example: A
house can be identified by the house name, location, number of floors and so on. These structured set of
variables depend on each other to identify the exact house. Similarly, data structure is a structured set of
variables that are linked to each other, which forms the basic component of a system.
3.IMPLEMENTATION
Data structures are generally based on the ability of a computer to fetch and store data at any place in its
memory, specified by a pointer—a bit string, representing a memory address, that can be itself stored in
memory and manipulated by the program. Thus, the array and record data structures are based on computing
the addresses of data items with arithmetic operations, while the linked data structures are based on storing
addresses of data items within the structure itself.
The implementation of a data structure usually requires writing a set of procedures that create and manipulate
instances of that structure. The efficiency of a data structure cannot be analyzed separately from those
operations. This observation motivates the theoretical concept of an abstract data type, a data structure that is
defined indirectly by the operations that may be performed on it, and the mathematical properties of those
operations (including their space and time cost).
Examples-An array is a number of elements in a specific order, typically all of the same type (depending on
the language, individual elements may either all be forced to be the same type, or may be of almost any type).
Elements are accessed using an integer index to specify which element is required. Typical implementations
allocate contiguous memory words for the elements of arrays (but this is not always a necessity). Arrays may
be fixed-length or resizable.
4.RECURSION
Recursion in data structure is when a function calls itself indirectly or directly, and the function
calling itself is known as a recursive function. It's generally used when the answer to a larger
issue could be depicted in terms of smaller problems.
In recursion in data structure, a method or a function has the capability to decode an issue. In the
process of recursion, a problem is resolved by transforming it into small variations of itself. In
this procedure, the function can call itself either directly or indirectly. Based on the differences in
call recursion in data structure can be categorized into different categories. You will get to know
about these categories later in the blog.
Properties of Recursion
Advantages of recursion
1. Clarity and simplicity: Recursion can make code more readable and easier
to understand. Recursive functions can be easier to read than iterative
functions when solving certain types of problems, such as those that
involve tree or graph structures.
2. Reducing code duplication: Recursive functions can help reduce code
duplication by allowing a function to be defined once and called multiple
times with different parameters.
3. Solving complex problems: Recursion can be a powerful technique for
solving complex problems, particularly those that involve dividing a
problem into smaller sub problems. For example, recursive algorithms are
commonly used in sorting, searching, and traversing data structures like
trees and graphs.
4. Memory efficiency: Recursive algorithms can be more memory-efficient
than iterative algorithms when working with certain types of data
structures. For example, when traversing a binary tree, a recursive
algorithm may only need to keep track of the current node and the two
sub trees, while an iterative algorithm may need to keep track of a stack of
nodes.
5. Flexibility: Recursive functions can be more flexible than iterative functions
because they can handle inputs of varying sizes without needing to know
the exact number of iterations required. This makes them particularly
useful for dealing with problems that have a variable or unknown input
size.
Disadvantages of recursion
1. Performance Overhead: Recursive algorithms may have a higher
performance overhead compared to iterative solutions. This is because
each recursive call creates a new stack frame, which takes up additional
memory and CPU resources. Recursion may also cause stack overflow
errors if the recursion depth becomes too deep.
2. Difficult to Understand and Debug: Recursive algorithms can be difficult to
understand and debug because they rely on multiple function calls, which
can make the code more complex and harder to follow.
3. Memory Consumption: Recursive algorithms may consume a large amount
of memory if the recursion depth is very deep. This can lead to memory-
related issues, such as running out of memory or causing the system to
slow down.
4. Limited Scalability: Recursive algorithms may not scale well for very large
input sizes because the recursion depth can become too deep and lead to
performance and memory issues.
5. Tail Recursion Optimization: In some programming languages, tail
recursion optimization is not supported, which means that tail recursive
functions can be slow and may cause stack overflow errors
Syntax
jump-statement:
return expressionopt ;
As a good engineering practice, always specify a return type for your functions. If a
return value isn't required, declare the function to have void return type. If a return type
isn't specified, the C compiler assumes a default return type of int.
The compiler may issue a warning diagnostic message about unreachable code if it finds
any statements placed after the return statement.
6.Storage Classes
the storage class in the C language for determining the visibility, lifetime, initial value, and
memory location of any given variable. The storage classes define the visibility (scope) and the
lifetime of any function/ variable within a C program. These classes precede the type that they
are going to modify.
There are four different types of storage classes that we use in the C language:
It is also known as the auto storage class, and it acts as the default storage class for all the
variables that are local in nature.
For example,
int mount;
Look at the example that we used above- it defines two of the variables in the very same storage
class. One can use ‘auto’ only within the functions- or the local variables.
• The allocation of memory for these variables occurs automatically during the runtime.
• The scope of an automatic variable is limited to that block in which we are defining them.
• The visibility of these variables is also limited to that block in which we are defining
them.
• The initialization of these variables is, by default, a garbage value.
• The memory that is assigned to an automatic variable gets free when it exits from a
block.
• We use the keyword auto to define the automatic variables.
• Any local variable that exists in the C language is, by default, automatic in nature.
Let us look at an example,
#include <stdio.h>
int main()
int p; //auto
char q;
float r;
printf(“%d %c %f”,p,q,r); // to print the initial default value of the automatic variables p, q, and
r.
return 0;
#include <stdio.h>
int main()
int p = 10,i;
printf(“%d “,++p);
int p = 20;
for (i=0;i<3;i++)
printf(“%d “,p); // 20 will be printed here 3 times because it is the given local value of p
printf(“%d “,p); // 11 will be printed here since the scope of p = 20 has finally ended.
11 20 20 20 11
It is also known as the extern storage class, and we use it for giving a reference of any global
variable which is visible to all the files present in a program. When using the extern storage
class, we cannot initialize the variable. However, note that it points to the name of the variable at
any storage location that we have already defined (previously).
Whenever we have multiple numbers of files while we are trying to define any global variable or
a function (that will be used in various other files too), then we will use the extern in another file
for providing the reference of the defined function or variable. In simpler words, we use the
entern for declaring a function or a global variable in another file.
The most common use of the extern modifier is when we have two or more than two files that
share a similar global variable or function.
For example:
main.c (File 1) –
#include <stdio.h>
int count ;
main() {
count = 38;
write_variable();
support.c (File 2) –
#include <stdio.h>
void write_variable(void) {
In this example, we have used the extern keyword for declaring the variable available in the
second file. On the other hand, its definition actually resides in the main.c (that is its first file).
Now, the compilation of both of these files will be as follows:
This step will generate a program a.out that is executable in nature. When we perform the
execution of this program, we will get a result as follows:
• We use the external storage class for conveying to the compiler that the variable that has
been defined as the extern has been declared using an external linkage that exists
elsewhere in any program.
• One can only perform the initialization of an external variable globally. In other words,
one cannot perform the initialization of an external variable within any given method or
block.
• The variables that are declared as extern have no allocation of memory. It only has a
declaration, and it intends to specify that the given variable has been declared elsewhere
in the available program.
• An external integral type’s default initial value is going to be 0, or else it is null.
• We can initialize an external variable multiple times, but we can only initialize it a single
time.
• When we declare a variable as external, the compiler will start searching for that variable
for initialization somewhere in the available program. It might be static or extern. In case
it isn’t, the compiler will ultimately generate an error (a compile-time error).
Let us look at an example,
#include <stdio.h>
int a;
int main()
extern int a; // variable a is defined globally, the memory will not be allocated to a
printf(“%d”,a);
#include <stdio.h>
int main()
extern int x; // The compiler will start searching here if a variable x has been defined and
initialized in the program somewhere or not.
printf(“%d”,x);
}
int x = 20;
20
extern int x;
int x = 10;
#include <stdio.h>
int main()
printf(“%d”,x);
int x = 20; // the compiler will generate an error at this line in the output
Compile-time error
This type of storage class gives an instruction to a compiler to keep the given local variable
around during the program’s lifetime- instead of creating it and then destroying it every time it
comes into a scope and goes out of it. Thus, when we make a local variable static, it allows the
variable to maintain the values that are available between various function calls.
We may also apply a static modifier to a global variable. When we do so, it will cause the scope
of the variable to be restricted to that file in which its declaration happened.
In a C programming language, when we use the static on a global variable, it will cause all the
objects present in a class to share a single copy of that given member.
For example,
#include <stdio.h>
/* declaration of function */
void func(void);
/* a global variable */
main() {
while(count–) {
return 0;
/* definition of function */
x++;
The compilation and execution of this code mentioned above will produce the result as follows:
When the above code is compiled and executed, it produces the following result −
#include<stdio.h>
static float f;
static int i;
static char c;
void main ()
0.000000 0 0 (null)
#include<stdio.h>
void sum()
x++;
y++;
void main()
int a;
sum(); // Given static variables will hold their values between the various function calls.
20 34
21 35
22 36
We use the register storage class for defining the local variables that must be stored in any
register, and not in a RAM. It means that the maximum size of this variable is equal to that of the
register size (it is usually one word). Also, we cannot apply the ‘&’ unary operator to it because
it has no memory location.
For example,
We must only use the register in the case of those variables which require quick access, such as
the counters. We must also note that defining a register doesn’t mean that this variable would be
stored in any register. It rather means that this variable MIGHT or might not be stored in a
register. It totally depends on the hardware and also the restrictions of implementation.
• Those variables that we define as the register have their memory allocation into the CPU
registers. It depends totally upon the size of the memory that remains in the CPU.
• Its access time is comparatively much faster than that of the automatic variables.
• One cannot dereference a register variable. In other words, one cannot make use of the
‘&’ operator in the case of a register variable.
• The default initial value of any given register local value will always be 0.
• We use the register keyword for the variable that must be stored in a CPU register.
However, whether a variable must be stored in a register or not is always going to be the
choice of the compiler.
• One can easily store the pointers in a register. It means that any register is capable of
storing the given variable’s address.
• We cannot store a variable into a register since we can’t really utilize more than one
storage specifier for the very same variable.
Internal and external controls both influence the way a company regulates its business
practices. Internal controls are the steps an organization takes to manage its own
operations and create consistent outcomes in the workplace. These can include overall
company values and best practices for accomplishing goals. For example, a company
that values environmental sustainability uses that characteristic as an internal control
when choosing business partners, suppliers and production methods.
Some internal controls are based on external controls. For example, if the government
passes a law setting limits on certain financial transactions, a company may proactively
hire an internal auditor to review its financial policies.
Related: Guide to Safety Rules in the Workplace (With Tips To Maintain a Safe
Environment)
You may also consider working in the field of regulatory affairs, where you create the
external controls that influence how businesses operate. Both the people who create
regulations and those who uphold internal compliance to external controls have
important roles in creating industries with high standards for consumers and
professionals.
YesNo
One of the most direct types of external controls is government regulations and laws
that influence how companies can operate. They're also highly effective because they
have immediate consequences for businesses that don't comply. When an organization
doesn't follow a government regulation for business operations, it may need to pay an
expensive fine or follow an operational penalty. Government regulations cover any
aspect of organizational governance, from employment practices to how the company
advertises its products. Some examples of external government controls include:
• Laws limiting the locations where oil companies can extract resources
• Regulations regarding how companies can use and share data from
customers
• Tax code explaining how much businesses owe on different types of
company earnings
Industry standards
Information releases
When companies release information, such as yearly financial reports, this information
can act as an external control. Even the knowledge that information is eventually going
to be public can encourage company leaders to make more responsible, ethical choices
about their business. This can include sharing information within the company,
submitting details to shareholders or releasing reports to the public. For example, some
government regulations require businesses to release certain financial information to
the public to uphold financial accountability.
Changes in leadership
If one company purchases another, a merger occurs or a business simply hires a new
person in a leadership position, this can increase awareness of the organization's
internal governance structure. In especially large, public companies, a change in
leadership can also attract attention from the public and media to the company's
business practices. Before a change in leadership, current company leaders may make
adjustments to the governance structure to improve compliance or simply make the
business more organized for the successor.
Related: How To Make a Change in Leadership Announcement
External audits
External controls can occur between business partners and departments through
external audits. One company may hire an external auditor to review the business
practices of a potential business partner before agreeing to a business relationship. This
type of external control is common between retailers and suppliers or companies that
want to ensure that they're doing business with companies that have ethical and
sustainable governance practices.
Media relations
Public perception and media coverage can also influence an organization's internal
policies and operational procedures. Investigative journalists may report on an issue
within a company and use pressure from the public to encourage company leaders to
change their policies. Social values and changing opinions can also cause businesses
to adapt their values and adjust their practices so they can appeal to their target
audience.
Interrupts:
When an interrupt occurs, it is the responsibility of the interrupt dispatching software
to save the context of the processor such that an ISR written in a high-level language
(typically C) can be invoked without damaging the state of the task that was
interrupted. In general, this results in the saving of registers which are NOT preserved
across subroutine calls as well as any special interrupt state. A port should define the
CPU_Interrupt_frame structure so that application code can examine the saved state.
typedef struct {
unsigned32 not_preserved_register_1;
unsigned32 special_interrupt_register;
} CPU_Interrupt_frame;
Pointers:
Pointer is used to points the address of the value stored anywhere in the computer memory.
To obtain the value stored at the location is known as dereferencing the pointer. Pointer
improves the performance for repetitive process such as:
o Traversing String
o Lookup Tables
o Control Tables
o Tree Structures
Pointer Details
o Pointer arithmetic: There are four arithmetic operators that can be used in
pointers: ++, --, +, -
o Array of pointers: You can define arrays to hold a number of pointers.
o Pointer to pointer: C allows you to have pointer on a pointer and so on.
o Passing pointers to functions in C: Passing an argument by reference or by
address enable the passed argument to be changed in the calling function by the
called function.
o Return pointer from functions in C: C allows a function to return a pointer to
the local variable, static variable and dynamically allocated memory as well.
Interpreter
An interpreter is a program that directly executes the instructions in a
high-level language, without converting it into machine code. In
programming, we can execute a program in two ways. Firstly, through
compilation and secondly, through an interpreter. The common way is
to use a compiler.
Strategies of an Interpreter
It can work in three ways: