Preprocessor Directories and Memory Allocation: Session 06
Preprocessor Directories and Memory Allocation: Session 06
1
Session Objectives
2
Session Topics
3
The Preprocessor
A preprocessor is a facility provided for writing portable
programs,easier program modifications and easier debugging.
A preprocessor processes the source code program before it
passes through the compiler.
The preprocessor is a part of the compiler.It is implemented as
an integral part of a Standard C Compiler.
It is a separate program which transforms C source code
containing preprocessor directives into source code with the
directives removed.
It works on a line-by-line basis, so the end of a line means
something special to it.
4
Features of Preprocessors
File Inclusions
-- #include directive
• Substitution facilities
-- Manifests
-- Macros
Conditional Compilation
-- #if
-- #else
5
Directives
• Directives are preprocessor control lines that
control the preprocessor facilities.
• They start with the symbol ‘#’.
• The directives can be placed anywhere in a
program but are most often placed at the
beginning of a program, before main().
6
File Inclusion Directives
7
#include
#include “file_name”
• This command would search the directory that contains the
source file.
• If the search fails in the home directory it searches the
implementation defined locations.
• This command would look for the file in the current
directory as well as the specified list of directories as
mentioned in the include search path that might have been
set up.
8
#include
#include <file_name>
9
Substitution Facilities
There are two types of Substitution Facilities
available.They are:
• Manifests
Manifest is defined as #define NAME value
Example: #define MAX 10
• Macros
Macro is defined as
#define NAME(arg) expression
Example:#define SQR(x) ((x) * (x))
10
Macro
11
#define
Macro template
Macro definition
#define VALUE 25
main()
{ Macro expansion
int j;
for(j=1;j<=VALUE;j++)
printf(“%d”,i);
}
NOTE:In a macro call the preprocessor replaces the
macro template with its macro expansion.
12
Differences:Macros & Functions
Macros Functions
• They are expanded at pre- • They are expanded at compile
compile time. time.
• They are expanded by the • They are parsed by the
preprocessor. compiler.
• They do not follow any rules.It
• They follow all the rules
is merely a replacement.
enforced on functions.
• Expressions passed as
arguments can be evaluated • Expressions passed as
more than once. arguments are evaluated only
• Code used by macros cannot be once.
used for debugging. • Code used by functions can be
used for debugging.
13
Conditional Compilation
A section of source code may be compiled
conditionally using the conditional compilation
facilities.
The directives used for this purpose are:
-- #if
-- #elseif
-- #else
-- #endif
• These directives behave much like the if-else or if-
else-if control structure.
14
#undef, #ifdef, #ifndef, #endif
15
Pseudo Code
16
#if, #else,# elif
The #if directive can be used to test whether an expression evaluates to
a nonzero value or not.
If the result of the expression is nonzero, then the subsequent lines up
to a #else, #elif or #endif are compiled.
main()
{
#if VALUE <=5
statement 1;
statement 2;
#else
statement 3;
statement 4;
#endif
}
17
Pseudo Code
main()
{
#if VALUE == 5
statement 1;
#elif
statement 2;
#elif
statement 3;
#else
statement 4;
#endif
}
18
Stringizing Operator ‘#’
19
An Example: ‘#’
#define STRING(x,y) #x”developed by”#y
main()
{
char s[]=STRING(PROGRAM,ARUN);
printf(“%s\n”,s);
}
Output:
PROGRAM developed by ARUN
20
Token Pasting Operator:’##’
A notation ‘##’ used in a macro definition
concatenates the two tokens on either side of the
symbol ## into one token.
If the concatenation results in an invalid token, the
result is undefined.
The notation ## is called as Token Pasting
Operator.
21
An Example: ‘##’
Output:
3.140000
22
Advantages of Preprocessors
23
Variable Length Argument List
24
Macros used in VLAL
25
An Example:VLAL
#include<stdio.h>
#include<stdarg.h>
double average(int num,…) {
va_list arguments;
double sum=0;int x;
va_start(arguments,num);
for(x=0;x<num;x++) {
sum = sum + va_arg(arguments,double); }
va_end(arguments);
return sum/num; }
void main() {
printf(“%f\n”,average(3,12.2,22.3,4.5));
printf(“%f\n”,average(5,3.3,2.2,1.1,5.5,3.3)); }
26
Dynamic Memory Allocation
27
Static Memory Allocation
28
Memory Map of a C Program
Automatic
Stack
Variables
.
Free Memory Heap Memory
.
Global/Static
Variables RAM
C Program
BIOS ROM
29
malloc()
30
malloc():Syntax
ptr is a pointer
variable of type
data_type
ptr = (data_type*)malloc(size);
31
malloc():Return Value
32
malloc():An Example
void main()
{
char *str;
if(str=(char *)malloc(10)==NULL)
{
printf(“Out of memory\n”);
exit(1);
}
strcpy( str,”Hello”);
printf(“String is %s\n”,str);
free(str);
}
33
calloc()
34
calloc():Syntax
ptr is a pointer
variable of type
data_type
ptr = (data_type*)calloc(n,size);
35
calloc():Return Value
36
calloc():An Example
void main()
{
char *str = NULL;
str=(char *)calloc(10,sizeof(char));
if(str==NULL)
{
printf(“Out of memory\n”);
exit(1);
}
strcpy( str,”Hello”);
printf(“String is %s\n”,str);
free(str); }
37
realloc()
• This function is used to alter the size of the
previously allocated space which is allocated
either by using malloc or calloc functions.
• This function guarantees that reallocating the
memory will not destroy the original contents of
memory.
• The contents of the old block will be copied into a
newly allocated space and so,this function
guarantees that the earlier contents are not lost.
38
realloc():Syntax
The address of the
newly allocated
memory after
reallocation
ptr = (data_type*)realloc(ptr,size);
39
realloc():Return Value
40
void main() realloc():An Example
{
char *str;
str=(char *)malloc(10);
strcpy( str,”Embedded”);
printf(“Address of String %s is %d\n”,str,str);
str=(char *)realloc(str,40);
strcpy( str,”System Design”);
printf(“Address of String %s is %d\n”,str,str);
free(str);
}
41
free()
42
free():Syntax and Return Value
ptr is a pointer to a
memory block which
has already been
created
free(ptr);
43
Differences:malloc() & calloc()
malloc() calloc()
The syntax of malloc is The syntax of calloc is
ptr=(data_type*)malloc(size); ptr=(data_type*)calloc(n,size);
Allocates a contiguous block of Allocates multiple blocks of
memory of specified size. memory,each block with the
Allocated space will not be same size.
initialized. Each byte of allocated space is
Time efficiency is higher than initialized to zero.
calloc(). It is more expensive in time
efficiency because of zero
initialization.
44
Memory Leakage
main()
{
int a;
a=(int*)malloc(sizeof(int));
*a=10;
10
a=(int*)malloc(sizeof(int));
20
*a=20;
}
• Allocation of memory is done twice.In this case, a contains the
address of the most recently allocated memory.
• The earlier allocated memory remains inaccessable.
• The problem where in memory is reserved but not accessible to any
application is called MEMORY LEAKAGE.
45
Dangling Pointer
main()
{
int *a;
a=(int*)malloc(sizeof(a));
20
*a=20;
free(a); ?
……;
……;
}
46
Dangling Pointer
• After de-allocating the memory for the variable using the free() function,the
memory location pointing to by it is returned to the availability list.
• The pointer variable can be used, but the contents pointing to that cannot be used.
• The pointer variable does not contain a valid address and is called as a Dangling
Pointer.
• We should store a NULL immediately after executing the free() function.
• Any pointer pointing to a destroyed object or which does not contain a valid
address is called a Dangling Pointer.
• All un-initialized local pointer variables in a function do not contain valid
addresses and are all considered as Dangling Pointers.
• It is a very good programming practice to initialize all dangling pointers to NULL
immediately after freeing the location allocated earlier.
47
Summary
48
Thank You!
49