You are on page 1of 5

Const:

The const keyword specifies that a variable's value is constant and tells the compiler to prevent the
programmer from modifying it.

const int x = 10;

Volatile:
The volatile keyword is intended to prevent the compiler from applying any optimizations on objects
that can change in ways that cannot be determined by the compiler.

Objects declared as volatile are omitted from optimization because their values can be changed by
code outside the scope of current code at any time.

Sr. Key Volatile Memory Non-Volatile Memory


No.

Data Data is present till power supply Data remains even after power supply is
1
Retention is present. not present.

Persistenc Volatile memory data is not Non-volatile memory data is permanent.


2
e permanent.

Speed Volatile memory is faster than Non-volatile memory access is slower.


3
non-volatile memory.

Example RAM is an example of Volatile ROM is an example of Non-Volatile


4
Memory. Memory.

Data Data Transfer is easy in Volatile Data Transfer is difficult in Non-Volatile


5
Transfer Memory. Memory.

CPU CPU can access data stored on Data to be copied from Non-Volatile
6 Access Volatile memory. memory to Volatile memory so that CPU
can access its data.

Storage Volatile memory less storage Non-Volatile memory like HDD has very
7
capacity. high storage capacity.

Impact Volatile memory such as RAM is Non-volatile memory has no impact on


8 high impact on system's system's performance.
performance.

Cost Volatile memory is costly per unit Non-volatile memory is cheap per unit size.
9
size.
Static Function:
A static function in C is a function that has a scope that is limited to its object file. This means
that the static function is only visible in its object file. A function can be declared as static
function by placing the static keyword before the function name.

Contents of first_file.c
static void staticFunc(void)

   printf("Inside the static function staticFunc() ");

Contents of second_file.c
int main()

   staticFunc();

   return 0;

Static variables
A Static variable is able to retain its value between different function calls. The static variable is only
initialized once, if it is not initialized, then it is automatically initialized to 0. Here is how to declare a
static variable.
#include<stdio.h>

void func_1();
int a, b = 10;

int main()
{
func_1();
func_1();
func_1();

// signal to operating system everything works fine


return 0;
}

void func_1()
{
int a = 1;
static int b = 100;
printf("a = %d\n", a);
printf("b = %d\n\n", b);
a++;
b++;
}

Static global
A global static variable is only available in the translation unit (i.e. source file) the variable is in.

If global variable is to be visible within only one .c file, you should declare it static.
A global static variable is one that can only be accessed in the file where it is created. This variable is said
to have file scope. In C, the preprocessor directive #define was used to create a variable with a constant
value.

Static local
A variable is a variable, whose lifetime doesn't stop with a function call where it is declared. It extends
until the lifetime of a complete program. All function calls share the same copy of local static variables.

Structure vs union
Both structure and union are user-defined data types in C programming that hold multiple members of
different data types. Structures are used when we need to store distinct values for all the members in a
unique memory location while unions help manage memory efficiently.

Structure
It is a user defined datatype. It is used to combine different types of data into a single type. It can have
multiple members and structure variables. The keyword “struct” is used to define structures in C
language. Structure members can be accessed by using dot(.) operator.

Here is the syntax of structures in C language,

struct structure_name {

   member definition;

} structure_variables;

Structure Pointer: The structure pointer points to the address of a memory block where the


Structure is being stored. Like a pointer that tells the address of another variable of any data type (int,
char, float) in memory. And here, we use a structure pointer which tells the address of a structure in
memory by pointing pointer variable ptr to the structure variable.

Union
IT is also a user defined datatype. All the members of union share the same memory location. Size of
union is decided by the size of largest member of union. If you want to use same memory location for
two or more members, union is the best for that.

Unions are similar to the structure. Union variables are created in same manner as structure variables.
The keyword “union” is used to define unions in C language.

Here is the syntax of unions in C language,

union union_name {

   member definition;

} union_variables;

Stack overflow
A stack overflow is a type of buffer overflow error that occurs when a computer program tries to use
more memory space in the call stack than has been allocated to that stack. The call stack, also referred
to as the stack segment, is a fixed-sized buffer that stores local function variables and return address
data during program execution.

What causes stack overflow?


One of the most common causes of a stack overflow is the recursive function, a type of function that
repeatedly calls itself in an attempt to carry out specific logic. Each time the function calls itself, it uses
up more of the stack memory. If the function runs too many times, it can eat up all the available
memory, resulting in a stack overflow.

Stack overflow errors can also occur if too much data is assigned to the variables in the stack frame.
Array variables are particularly susceptible to stack overflow errors, especially if no logic has been
implemented to prevent excess data from being written to the array.

When a stack overflow occurs, the excess data can corrupt other variables and address data, effectively
changing variable values and overwriting return addresses. In some cases, this will cause the program to
crash. 

Memory leak
A memory leak is an unintentional form of memory consumption whereby the developer fails to free an
allocated block of memory when no longer needed. The consequences of such an issue depend on the
application itself.

Memory leak occurs when programmers create a memory in heap and forget to delete it. 

The consequences of memory leak is that it reduces the performance of the computer by reducing the
amount of available memory. Eventually, in the worst case, too much of the available memory may
become allocated and all or part of the system or device stops working correctly, the application fails, or
the system slows down vastly .
/* Function /* Function
with memory without // C Program to check whether the memory is
leak */ memory leak // freed or not
#include */  
<stdlib.h> #include #include <iostream>
  <stdlib.h> #include <cstdlib>
void f()
void f() using namespace std;
{
{
int *ptr =  
   int *ptr
(int *) int main()
= (int *) malloc(size {
malloc(size of(int));   int* ptr;
of(int)); /* Do some   ptr = (int*) malloc(sizeof(int));
  work */    
   /* Do free(ptr);
some work   if (ptr == NULL)
return;
*/     cout << "Memory Is Insuffficient\n";
}
  else
    {
   return;     free(ptr);
/* Return     cout << "Memory Freed\n";
without   }
freeing }
ptr*/
}

You might also like