Malloc and calloc function, Operating System

Assignment Help:

Note that the parameter for scanf doesn't need the address operators & because name is an address. However the variable name has no defined space. This can cause problems within C and we really should define the space .Within C we can allocate space by means of character array or system functions namely malloc, free and calloc.If we declare a character array, it also reserves the space required for that string (After all a string is a collection i.e. array of characters). Therefore the program becomes

    #include stdio.h
    void main()
    {
    char prompt;
    /*Author : Mr James Mc Carren  
    Company: Staffordshire University  
     Date: 26th August 2012  
    Version 1.0  
    Function : String storing using static storage       
     Modifications:   none*/
    char name[20];
    scanf("%s",name);
    printf("your name is %s \n\r",name);  
    printf("Press and key to exit \n\r");
    scanf("\n%c",&prompt);
    }
This allocates 20 spaces to the string name, however if we wish to input a 50 character  string , we would need to re-compile the program , an alternative is to use Malloc/Free .Malloc checks during run time whether there is enough  space left and if there is it allocated it while Free  releases the space allocated to a variable .The syntax for malloc/Free is  
 
     variable = ( char *)malloc(char size);
     free(variable);
 
Therefore the program becomes
 
    #include stdio.h
    #include stdlib.h
    void main()
    {
    char prompt;
   
     Date: 26th August 2012 
    Version 1.0 
    Function : String storing using dynamic allocation       
     Modifications:   none*/
      char *name;
      name = (char *)malloc(20);
      if (name == NULL) 
      {
      printf("Cannot allocate memory\n\r");
      exit(1);
      }
      scanf("%s",name);
      printf("your name is %s \n\r",name); 

      free(name);  
    printf("Press and key to exit \n\r");
    scanf("\n%c",&prompt);
    }

  Because Malloc allocates memory dynamically, we must place a trap routine to detect when the microprocessor cannot allocate memory (i.e. memory full), in this case malloc returns a NULL character back as the address of the variable i.e. This routine would detect the NULL character and exit the program .Note that malloced space is similar to a variable in that its life span is automatically terminated when a free command is used or the function where it was allocated ends no matter what you make read - this is not the case in C++  
 
      if (name == NULL) 
      {
      printf("Cannot allocate memory\n\r");
      exit(1);
      }
 
Therefore we could allocate a scratch pad of memory using malloc and we can poke around in that
area e.g.
 
  #include
    #include
    void main()
    { 
    char prompt;  
  
     Date: 26th August 2012
    Version 1.0 
Function : Dynamic allocation  of windows pointr area i.e segmented memory     
     Modifications:   none*/
 
      char *name;
      unsigned long int address;
      unsigned char *add;
      unsigned char data;
      name = (char *)malloc(0x4000);
      if (name == NULL) 
      {
      printf("Cannot allocate memory\n\r");
      exit(1);
      }
printf("Your segment area is %08lx to %08lx\n\r",name,name+(unsigned long int)0x4000);
printf("Please enter in the address  you want to read in the segment area\n\r");
scanf("%lx",&address);
add = (unsigned char *)address;
data = *add;
printf("The data at the address %08lx is %02x\n\r",address,data); 
printf("Press and key to exit \n\r");
scanf("\n%c",&prompt);
}


Related Discussions:- Malloc and calloc function

Memory Management Problems, Sir i want formulas in Memory management starti...

Sir i want formulas in Memory management starting from paging till the demang paging in a easy manner for solving problems in competitive exams

Explain first fit algorithm, FIRST FIT ALGORITHM Scanning a list along...

FIRST FIT ALGORITHM Scanning a list along with if a free hole is available with necessary space for the process to allocate it allocates the process in that space. The first s

What are the multithreading models, What are the multithreading models? ...

What are the multithreading models? There are three models:- a) Many-to-One model b) One-to-One model c) Many-to-Many model

Explain kernel activity for ults, KERNEL ACTIVITY FOR ULTS: The kernel...

KERNEL ACTIVITY FOR ULTS: The kernel isn't aware of thread activity but it is still managing process activity. When a thread compose a system call the whole process will be bl

What is co-operative process, What is co-operative process? A process i...

What is co-operative process? A process is co-operating if it can influence or be affected by the other processes implementing in the system. Any process that share data with o

Define how to manage the interaction of client and server, Managing the int...

Managing the interaction of client and server  Among the differences between traditional application programs and client server ones is that developers must create a mechanism

Synchronization, As we already know, threadsmust ensure consistency; otherw...

As we already know, threadsmust ensure consistency; otherwise, race conditions (non-deterministic results) might happen. Now consider the "too much milk problem": two people share

Define a daemon process , A daemon is a process that removes itself from t...

A daemon is a process that removes itself from the terminal and disconnected, executes, in the background, waiting for requests and responding to them. It may also be described as

Explain internal file structure, Internal file structure All disk I/O i...

Internal file structure All disk I/O is performed in units of single block, and all blocks are the similar size. It is unlikely that the physical record size will exactly match

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd