Saturday 31 December 2016

Breif description about static storage class


The static storage class is very important in C language so i'm going to discuss it in brief 
just have a glance below and i have compared it with an example::
The features of static storage class are as following:
StorageMemory
Keywordstatic
Default initial valueZero
ScopeLocal to the block, in which the variable is defined
LifeValue of the variable persists between different function calls.
#include<stdio.h>
void add();
int main()
{
add();
add();
add();
add();
return 0;
}
void add()
{
 static int i=1;
 printf("\n%d",i);
 i=i+1;
}
OUTPUT : 1 2 3

 static variable do not disappear when the function is no longer active. There value persist. If control comes back to the same function again , the static variables have the same values they had last time around.
Note:
if the storage class is static, then the statement static int i = 1 is executed only once, irrespective of how many times the same function is called.

Storage classes in C



Storage Classes in C

In C language, each variable has a storage class which decides scope, visibility and lifetime of that variable. The following storage classes are most oftenly used in C programming,
  1. Automatic variables
  2. External variables
  3. Static variables
  4. Register variables

Storage Classes are used to describe about the features of a variable/function. These features basically include the scope, visibility and life-time which help us to trace the existence of a particular variable during the runtime of a program.
C language uses 4 storage classes, namely:
auto: This is the default storage class for all the variables declared inside a function or a block. Hence, the keyword auto is rarely used while writing programs in C language. Auto variables can be only accessed within the block/function they have been declared and not outside them (which defines their scope). Of course, these can be accessed within nested blocks within the parent block/function in which the auto variable was declared. However, they can be accessed outside their scope as well using the concept of pointers given here by pointing to the very exact memory location where the variables resides. They are assigned a garbage value by default whenever they are declared.

extern: Extern storage class simply tells us that the variable is defined elsewhere and not within the same block where it is used. Basically, the value is assigned to it in a different block and this can be overwritten/changed in a different block as well. So an extern variable is nothing but a global variable initialized with a legal value where it is declared in order to be used elsewhere. It can be accessed within any function/block. Also, a normal global variable can be made extern as well by placing the ‘extern’ keyword before its declaration/definition in any function/block. This basically signifies that we are not initializing a new variable but instead we are using/accessing the global variable only. The main purpose of using extern variables is that they can be accessed between two different files which are part of a large program.
First File: main.c
#include <stdio.h>
 
int count ;
extern void write_extern();
 
main() {

   count = 5;
   write_extern();
}
Second File: support.c
#include <stdio.h>
 
extern int count;
 
void write_extern(void) {
   printf("count is %d\n", count);
}
Here, extern is being used to declare count in the second file, where as it has its definition in the first file, main.c. Now, compile these two files as follows −
$gcc main.c support.c
It will produce the executable program a.out. When this program is executed, it produces the following result −
count is 5
static: This storage class is used to declare static variables which are popularly used while writing programs in C language. Static variables have a property of preserving their value even after they are out of their scope! Hence, static variables preserve the value of their last use in their scope. So we can say that they are initialised only once and exist till the termination of the program. Thus, no new memory is allocated because they are not re-declared. Their scope is local to the function to which they were defined. Global static variables can be accessed anywhere in the program. By default, they are assigned the value 0 by the compiler.

void test();   //Function declaration (discussed in next topic)
 
main()
{
 test();
 test();
 test();
}
void test()
{
 static int a = 0;        //Static variable
 a = a+1;
 printf("%d\t",a);
}
output :
1 2 3
register: This storage class declares register variables which have the same functionality as that of the auto variables. The only difference is that the compiler tries to store these variables in the register of the microprocessor if a free register is available. This makes the use of register variables to be much faster than that of the variables stored in the memory during the runtime of the program. If a free register is not available, these are then stored in the memory only. Usually few variables which are to be accessed very frequently in a program are declared with the register keyword which improves the running time of the program. An important and interesting point to be noted here is that we cannot obtain the address of a register variable using pointers.
To specify the storage class for a variable, the following syntax is to be followed:
Syntax:
storage_class var_data_type var_name; 

Sunday 18 December 2016

Today  I would like to share a very interesting topic related to future gadgets and its hell breathtaking,Let's take a glance.
Imagination is more important than intellect… This was the conclusion Albert Einstein arrived to, one of the most important genius ever. Designers agree with this statement, it’s extremely hard nowadays to imagine new and new gadgets or at least new features for the existent products.Much more, there are so many competent designers that it is practically impossible to create something without being already sketched by other individuals. In spite of this context, every day a new and original gadget is created and amazes people. 

Future of Mobile Search for Diet

Future of Mobile Search for Diet

Apple Black Hole, the holographic phone for the year

Apple Black Hole, the holographic phone for the year

Glassy Glassy phones

Glassy Glassy phones

Ringen watch

Ringen watch

Siafu: Metamophing Computer Interface

Siafu: Metamophing Computer Interface

Blue Bee

Blue Bee

Stealthy Kayak

Stealthy Kayak

WristPC

WristPC

Tharula Electric Bike By Diana Lumbasyo

Tharula Electric Bike By Diana Lumbasyo

The Intelligent Power Strip

The Intelligent Power Strip

Light Bulb Radio

Light Bulb Radio

REC

REC

Solaris watch

Solaris watch

The Piggy Bank

The Piggy Bank

Flexible Mobile

Flexible Mobile

The Smartphone Bicycle

The Smartphone Bicycle

Time Flies

Time Flies

Chiuet table

 Chiuet table

Fitness@Work

Fitness@Work

The Coral Reef Light

The Coral Reef Light

A man’s iron

A man's iron

The Smart Finger

The Smart Finger

AXIO camera

AXIO camera

The Mercator

The Mercator

MiCoffee

MiCoffee

The Phalanx

 The Phalanx

The Terrace Chair

The Terrace Chair

Shrieking Ball

Shrieking Ball

Water Stone Faucet

Water Stone Faucet

F.O. Clock

F.O. Clock

Sunday 14 August 2016

Engineering Student From Kerala Built A Working Ironman Suit In Just Rs 50,000!

If you are a fan of superheroes, it is highly likely that you may have fancied having your own Ironman suit.

So did Vimal Govind Manikandan, an engineering student from Kerala. But unlike most others, he has an Ironman suit of his own, because Vimal decided to make one for himself.
And it is no ordinary 'costume'. Vimal's suit is fully functional, weighs around 100 kg, and can lift up to 150kg thanks to battery-powered pressurised air chambers. But sorry folks, it can't fly).
Even though Vimal has no plans to turn himself into a superhero, he says the suit has future potential.
"Actually the future of this product is mainly in defence, industrial weight lifting, material handling etc," he said.
Vimal says he was inspired by Hollywood movies to build the robot, especially the suits in the Avengers movie. In fact, this isn’t even his first exosuit. His team built their first prototype back in 2015, which was much larger but mechanically-powered.
The young engineer and his team are now working on improving the prototype, especially to fix its walking ability, which Vimal admits, is restricted now.

Wednesday 3 August 2016


Memory Layout of C Programs


A typical memory representation of C program consists of following sections.
1. Text segment
2. Initialized data segment
3. Uninitialized data segment
4. Stack
5. Heap

A typical memory layout of a running process
1. Text Segment:
A text segment , also known as a code segment or simply as text, is one of the sections of a program in an object file or in memory, which contains executable instructions.
As a memory region, a text segment may be placed below the heap or stack in order to prevent heaps and stack overflows from overwriting it.
Usually, the text segment is sharable so that only a single copy needs to be in memory for frequently executed programs, such as text editors, the C compiler, the shells, and so on. Also, the text segment is often read-only, to prevent a program from accidentally modifying its instructions.
2. Initialized Data Segment:
Initialized data segment, usually called simply the Data Segment. A data segment is a portion of virtual address space of a program, which contains the global variables and static variables that are initialized by the programmer.
Note that, data segment is not read-only, since the values of the variables can be altered at run time.
This segment can be further classified into initialized read-only area and initialized read-write area.
For instance the global string defined by char s[] = “hello world” in C and a C statement like int debug=1 outside the main (i.e. global) would be stored in initialized read-write area. And a global C statement like const char* string = “hello world” makes the string literal “hello world” to be stored in initialized read-only area and the character pointer variable string in initialized read-write area.
Ex: static int i = 10 will be stored in data segment and global int i = 10 will also be stored in data segment
3. Uninitialized Data Segment:
Uninitialized data segment, often called the “bss” segment, named after an ancient assembler operator that stood for “block started by symbol.” Data in this segment is initialized by the kernel to arithmetic 0 before the program starts executing
uninitialized data starts at the end of the data segment and contains all global variables and static variables that are initialized to zero or do not have explicit initialization in source code.
For instance a variable declared static int i; would be contained in the BSS segment.
For instance a global variable declared int j; would be contained in the BSS segment.
4. Stack:
The stack area traditionally adjoined the heap area and grew the opposite direction; when the stack pointer met the heap pointer, free memory was exhausted. (With modern large address spaces and virtual memory techniques they may be placed almost anywhere, but they still typically grow opposite directions.)
The stack area contains the program stack, a LIFO structure, typically located in the higher parts of memory. On the standard PC x86 computer architecture it grows toward address zero; on some other architectures it grows the opposite direction. A “stack pointer” register tracks the top of the stack; it is adjusted each time a value is “pushed” onto the stack. The set of values pushed for one function call is termed a “stack frame”; A stack frame consists at minimum of a return address.
Stack, where automatic variables are stored, along with information that is saved each time a function is called. Each time a function is called, the address of where to return to and certain information about the caller’s environment, such as some of the machine registers, are saved on the stack. The newly called function then allocates room on the stack for its automatic and temporary variables. This is how recursive functions in C can work. Each time a recursive function calls itself, a new stack frame is used, so one set of variables doesn’t interfere with the variables from another instance of the function.
5. Heap:
Heap is the segment where dynamic memory allocation usually takes place.
The heap area begins at the end of the BSS segment and grows to larger addresses from there.The Heap area is managed by malloc, realloc, and free, which may use the brk and sbrk system calls to adjust its size (note that the use of brk/sbrk and a single “heap area” is not required to fulfill the contract of malloc/realloc/free; they may also be implemented using mmap to reserve potentially non-contiguous regions of virtual memory into the process’ virtual address space). The Heap area is shared by all shared libraries and dynamically loaded modules in a process.
Memory Management in C

There are two ways in which memory can be allocated in C:

•           by declaring variables
•           by explicitly requesting space from C

We have discussed variable declaration in other lectures, but here we will describe requesting dynamic memory allocation and memory management.

C provides several functions for memory allocation and management:

•           malloc and calloc, to reserve space
•          realloc, to move a reserved block of memory to another allocation of different dimensions
•          free, to release space back to C

These functions can be found in the stdlib library

What happens when a pointer is declared?

Whenever a pointer is declared, all that happens is that C allocates space for the pointer.

For example,

char *p;

allocates 4 consecutive bytes in memory which are associated with the variable p. p’s type is declared to be of pointer to char. However, the memory location occupied by p is not initialised, so it may contain garbage.

It is often a good  idea to initialise the pointer at the time it is declared, to reduce the chances of a random value in p to be used as a memory address:

char *p = NULL;

At some stage during your program you may wish p to point to the location of some string

A common error is to simply copy the required string into p:

strcpy(p, “Hello”);

Often, this will result in a “Segmentation Fault”. Worse yet, the copy may actually succeed.

//a.c

#include <stdio.h>

main() {
 char *p;
 char *q = NULL;

 printf("Address of p = %u\n", p);
 strcpy(p, "Hello");
 printf("%s\n", p);
 printf("About to copy \"Goodbye\" to q\n");
 strcpy(q, "Goodbye");
 printf("String copied\n");
 printf("%s\n", q);
}

When p and q are declared, their memory locations contain garbage. However, the garbage value in p happens to correspond to a memory location that is not write protected by another process. So the strcpy is permitted.

By initialising q to NULL, we are ensuring that we cannot use q incorrectly. Trying to copy the string “Goodbye” into location 0 (NULL) results in a run-time Bus Error, and a program crash.

So, how can we use memory properly?

Before we can use a pointer, it must be pointing to a valid area of memory. We can use malloc to request a pointer to a block of memory (calloc to request an array of zero-value initialised blocks).


void *malloc(size_t byteSize)

void *calloc(size_t numElems, size_t byteSize)


//b.c

#include <stdio.h>
#include <stdlib.h>

main() {
 char *q = NULL;

 printf("Requesting space for \"Goodbye\"\n");

 q = (char *)malloc(strlen("Goodbye")+1);

 printf("About to copy \"Goodbye\" to q at address %u\n", q);
 strcpy(q, "Goodbye");
 printf("String copied\n");
 printf("%s\n", q);
}

How do we know if the memory allocation has been successful?


Malloc (and calloc) will return a non-NULL value if the request for space has been successful, and NULL if it fails. Using the result of malloc (or calloc) after it has failed to locate memory WILL result in a run-time program crash.

//c.c

#include <stdio.h>
#include <stdlib.h>

main() {
 char *q = NULL;

 printf("Requesting space for \"Goodbye\"\n");

 q = (char *)malloc(strlen("Goodbye")+1);

 if (!q) {
  perror("Failed to allocate space because");
  exit(1);
 }

 printf("About to copy \"Goodbye\" to q at address %u\n", q);
 strcpy(q, "Goodbye");
 printf("String copied\n");
 printf("%s\n", q);
}

The same applies to reading data from a file. If you use fscanf, etc., you must ensue that there is enough space to store the input, because the functions won’t

 

Freeing space


When space is allocated using the alloc family of functions, the space is allocated permanently until the program terminates, or it is freed.

Local variables are destroyed when their enclosing function terminates. Although the values are not necessarily overwritten, C may allocate the space to some other requesting process

//d.c

#include <stdio.h>

char *foo(char *);

main() {
 char *a = NULL;
 char *b = NULL;
 a = foo("Hi there, Chris");
 b = foo("Goodbye");

 printf("From main: %s %s\n", a, b);
}

char *foo(char *p) {
  char q[strlen(p)+1];
  strcpy(q, p);
  printf("From q: the string is %s\n", q);
  return q;
}

In this example, q is a variable local to foo. A string created in main is passed to foo and copy to q. The address of q is returned to main, where there is an attempt to preserve and use the strings. The result is disastrous.


//e.c

#include <stdio.h>
#include <stdlib.h>

char *foo(char *);

main() {
 char *a = NULL;
 char *b = NULL;
 a = foo("Hi there, Chris");
 b = foo("Goodbye");

 printf("From main: %s %s\n", a, b);
}

char *foo(char *p) {
  char *q = (char *)malloc(strlen(p)+1);
  strcpy(q, p);
  printf("From foo: the string is %s\n", q);
  return q;
}

In this example, however, the space is requested legitimately, and, although q, a local variable holding an address of the string, is destroyed when foo terminates, the string itself is preserved and can be used safely in the calling function.

The correct way to release the space is to use free().


//f.c

#include <stdio.h>
#include <stdlib.h>

char *foo(char *);

main() {
 char *a = NULL;
 char *b = NULL;
 a = foo("Hi there, Chris");
 free(a);
 b = foo("Goodbye");
 free(b); 
 printf("From main: %s %s\n", a, b);
}

char *foo(char *p) {
  char *q = (char *)malloc(strlen(p)+1);
  strcpy(q, p);
  printf("From foo: the string is %s\n", q);
  return q;
}

If free(b) is omitted, then “Goodbye” can be seen to be written to the location of “Hi there, Chris”.

The free function has the following syntax.

void free(void *ptr)

What happens when the amount of space allocated turns out to be too small?


void *realloc(void *oldptr, size_t newsize)

You’ve been accepting characters from the keyboard into some previously allocated bytes, but the user keeps typing characters and is going to overflow the memory allocation… what do you do?

What you’d want to do, of course, is keep track of the number of characters being written, and when you’re almost out of space, request a larger block from C, copy the old string into the new location, and free the space associated with the old string – which is practically what the realloc function does.


//g.c

#include <stdio.h>
#include <stdlib.h>

char *readline(char *, int *);
char *allocmem(char *, int);

main() {
 char *p = NULL;
 int max = 10;
 p = (char *)malloc(max);
if (!p) {
  perror("Memory allocation error 1");
  exit(1);
 }
 *p = ‘\0’;
 p = readline(p, &max);
 printf("User input\n%s\n", p);
}

char *readline(char *p, int *max) {
 char c;
 int count = strlen(p);
 while ((c = getchar()) != EOF) {
  if (count == (*max-1)) {
   *(p+(*max-1)) = '\0';
   *max += 10;
   p = allocmem(p, *max);
   if (!p) {
    perror("Memory allocation error 2");
    exit(1);
   }
  }
  count+=1;
  strncat(p, &c, 1);
 }
 return p;
}

char *allocmem(char *p, int max) {
  char *q = NULL;
  q =  (char *)realloc(p, max);
  if (!q) {
   perror("hi!");
   exit(1);
  }
  return q;
}

Memory Management and Data Structures


Reading keyboard text and keeping each line input in a linked list…

/*h.c. The program reads lines of input, and stores each line in a linked list. Eventually the list is printed */

#include <stdio.h>
#include <stdlib.h>

struct lineList {
 char *line;// a line of input
 struct lineList *nextLine; // pointer to the next line
};

// global variable pointing to head of the linked list
struct lineList *theHead = NULL;

char *readline(char *, int *, struct lineList *);
char *allocmem(char *, int);
struct lineList *makeElem(char *, struct lineList *);
void printList(struct lineList *);

main() {
 char *p = NULL;
 struct lineList *head = NULL;
 int max = 10; // initial size of input array
 extern struct lineList *theHead;

 p = (char *)malloc(max); // request space for the input array
 if (!p) {
  perror("Memory allocation error 1");
  exit(1);
 }
 *p = '\0'; // we use strlen later, so initialise input array
 p = readline(p, &max, head); // read all the input data
 printList(theHead); // print all the input data
}

char *readline(char *p, int *max, struct lineList *elem) {
 … // some code has been removed

  if (c == '\n') { // if a newline is encountered in the input
   elem = makeElem(p, elem); // copy the input line (p) to an element
   free(p); // we’re going to resize the input array p, to save space
   *max = 10; // set max to 10 again (same as in main())
   p = (char *)malloc(*max); // request space for the input array
// NB: the lines from free to here could have been replaced by
// p = (char *)realloc(p, 10);
// check that the request was successful (code not shown)

   *p = '\0'; // initialise array so string functions will work
   count = 0; // reset count
   continue;
  }


struct lineList *makeElem(char *p, struct lineList *elem) {
// add an element to the linked list
//
 struct lineList *temp = elem;
 struct lineList *head = elem;
 extern struct lineList *theHead;

 if (!head) { // if the linked list hasn’t been created yet
//request space for it
  head = (struct lineList *)malloc(sizeof(struct lineList));
  if (!head) {
   perror("Couldn't allocate space for head");
   exit(3);
  }
  theHead = head; // set the global variable
//request space to the input line
  head->line = (char *)malloc(strlen(p));
  if (!head->line) {
   printf("Couldn't allocate space for %s because", p);
   perror("");
   exit(4);
  }
// copy the input line to the element
  strcpy(head->line, p);
  head->nextLine = NULL; // set the pointer to next element to NULL
  return head;
 }
// otherwise, if the linked list exists already
// look for the last element in the list
 while (elem) {
  temp = elem;
  elem = temp->nextLine;
 }

// create a new element, storing its address in the old last element
 temp->nextLine = (struct lineList *)malloc(sizeof(struct lineList));
 if (!temp->nextLine) {
  perror("Failed to allocate list head");
  exit(2);
 }
// request space to store the input line in the element
 temp->nextLine->line = (char *)malloc(strlen(p));
 if (!temp->nextLine->line) {
   printf("Couldn't allocate space for %s because", p);
   perror("");
   exit(4);
  }
//copy the input line to the new element
 strcpy(temp->nextLine->line, p);
 temp->nextLine->nextLine = NULL;
 return head;
}


// print the lines in the linked list, starting from the first element
void printList(struct lineList *head) {
 struct lineList *curr = head;

// loop while the address of the element is not NULL
// NULL indicates the end of the linked list
 while (curr) {
  printf("%s\n", curr->line);
  curr = curr->nextLine;
 }
}